; BINTOBCD ; ; These routines format a registers into their ascii string equivalents, ie: ; the number 143 would be turned into ; .db "143", 0 ; ; Use bintobcd to format numbers from 0 to 255 and bintobcd16 to format ; numbers from 0 to 65535. Bintobcd will make a string with leading zeroes, ie: ; the number 4 would be turned into ; .db "004",0 ; ; bintobcd16 does NOT format strings with leading zeroes, ie: ; the number 353 would be turned into ; .db "353",0 ; NOT ; .db "00353",0 ; ; Use the following routines freely but please keep the documentation ; intact. ; ; ; BINTOBCD ; by Joe Pemberton ;size: 69 bytes ; ;input: ;c=number to convert ; ;returns H,L,E as HUNDREDS, TENS, ONES ;(bcdstring) contains the formatted bcd ascii string ;bc = 0 bintobcd: ld b,8 ;loop for 8 bits ld hl,0 ;h=hundreds, l=tens ld e,l ;e=ones bcdL: ld a,e cp 5 jr c,bcd1 add a,3 ld e,a bcd1: ld a,l cp 5 jr c,bcd2 add a,3 ld l,a bcd2: sla c ;shift the bit from the number off rl e ;shift the bit into the ones bit 4,e jr z,bcdb1 res 4,e scf bcdb1: rl l bit 4,l jr z,bcdb2 res 4,l scf bcdb2: rl h djnz bcdL ld a,h add a,$30 ld (bcdstring),a ld a,l add a,$30 ld (bcdstring+1),a ld a,e add a,$30 ld (bcdstring+2),a ret bcdstring: .db $00,$00,$00,$00 ;zero terminated string of the bcd number ; BINTOBCD16 ; by Joe Pemberton ;size: 97 bytes ; ;input: ;de=number to convert ; ;returns (bcd16) as TEN THOUSANDS, THOUSANDS, HUNDREDS, TENS, ONES ;(bcd16string) contains the formatted bcd ascii string ;b = 0 ;de = 0 ;destroys af, bc, de, hl, ix bintobcd16: push de ld hl,bcd16 ld de,bcd16+1 ld bc,10 ld (hl),0 ldir pop de ld b,16 bcd16L: ld hl,bcd16 push bc ld b,5 bcd16I: ld a,(hl) cp 5 jr c,bcd16a add a,3 ld (hl),a bcd16a: inc hl djnz bcd16I dec hl ;hl now points to the ONES ld b,5 sla e rl d bcd16b: rl (hl) bit 4,(hl) jr z,bcd16c res 4,(hl) scf bcd16c: dec hl djnz bcd16b pop bc djnz bcd16L inc hl ld ix,bcd16string ld bc,(5*256)+0 bcd16d: ld a,(hl) add a,$30 ld (ix+0),a cp $30 jr nz,bcd16e bit 1,c jr z,bcd16f bcd16e: inc ix set 1,c bcd16f: inc hl djnz bcd16d ret bcd16: .db 0,0,0,0,0 bcd16string: .db 0,0,0,0,0,0