Skip to content

Instantly share code, notes, and snippets.

@plainspooky
Last active January 5, 2016 17:14
Show Gist options
  • Save plainspooky/c3beef32b2b3e520ffa6 to your computer and use it in GitHub Desktop.
Save plainspooky/c3beef32b2b3e520ffa6 to your computer and use it in GitHub Desktop.
Routine to display large numbers in Z80 created by Miguel Angel Rodriguez Jodar and adapted by me to run on MSX computers
; This source code is regulated under the GNU license. Original division
; routine (c)Milos "baze" Bazelides, ( b...@stonline.sk ).
; Rest of code (c)Miguel Angel Rodriguez Jodar ( rodri...@atc.us.es )
;
; Adaptated to run in MSX from Giovanni Nunes
;
CHPUT equ 0x00a2
org 0xc000 ; The BASIC program is stored
; in 0x8000 (32768) address
; org 32768
Main proc
ld de,CADENA
otro_digito push de
call Obtener_Resto ; Get next remainder
pop de
add a,'0' ; Convert to an ASCII digit
ld (de),a ; and store it
inc de
call Check_Cero ; Is it the new dividend null?
jr nz,otro_digito ; Go to get another digit if not null
dec de ; Fix end address
; ld b,d ; And copy it to BC register
; ld c,e
;
; MSX
;
escribe ld a,(de) ; Read number
cp 0
ret z ; If A is zero, return to BASIC
call CHPUT ; write number on screen
dec de
jr escribe
;
; MSX
;
endp
Obtener_Resto proc
ld a,(NBYTES) ; A = number of bytes of the original
; number. We need number of bits, so we
; multiply it by 8.
ld l,a
ld h,0
add hl,hl
add hl,hl
add hl,hl
ex de,hl ; DE = number of bits of the original
; number
ld c,10 ; C holds the divisor.
ld hl,NUMERO ; HL points to the dividend/quotient
xor a ; Reset remainder
buc_div push hl ; This is, esencially, the same
; algorithm from Milos Bazelides, with
; the changers detailed above.
call Mult_2 ; The "old" ADD HL,HL
pop hl
rla
cp c
jr c,no_incr
sub c
call Increm ; The "old" INC L
no_incr dec de
push af
ld a,d
or e
jr z,fin_div
pop af
jr buc_div
fin_div pop af
ret
endp
Increm proc
ld hl,NUMERO
inc (hl)
ret
endp
Mult_2 proc
ld hl,NBYTES
ld b,(hl)
ld hl,NUMERO
sla (hl)
inc hl
dec b
ret z
buc_desplaza rl (hl)
inc hl
djnz buc_desplaza
ret
endp
Check_Cero proc
ld hl,NUMERO
ld a,(NBYTES)
ld b,a
check_byte ld a,(hl)
or a
jr nz,no_cero
inc hl
djnz check_byte
xor a
no_cero ret
endp
; Variables
NUMERO ds 255,255 ; 255 bytes filled with the value 255. Little
; endian notation. This is the largest number this routine can manage
; (although it's possible that it can deal with 256 byte-wide numbers
NBYTES db 255 ; how many bytes comprises our number
db 0 ; NULL byte to mark end of the converted number
CADENA equ $ ; here will be the ASCII code of the least
; significant digit of the converted number.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment