Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;#######################
- ;#NUMBERTOSTRING
- ;#Convert number in HL to a string
- ;#input: hl = number to display
- ;#output: hl = pointer to string
- ;#######################
- numberToString:
- exx
- ld de,numberString ;for the ldi in dN_b2dloop
- exx
- ld de,-10000 ;check how many 10,000s there are in the number
- call dN_b2d
- ld de,-1000 ;check how many 1,000s units there are
- call dN_b2d
- ld de,-100 ;hundreds
- call dN_b2d
- ld de,-10 ;tens
- call dN_b2d
- ld de,-1 ;single digits
- call dN_b2d
- ld c,-1 ;this part here removes the leading 0s
- ld hl,numberString ;where the string is stored
- push hl
- ld a,(hl)
- inc c
- inc hl
- cp 10 ;ten ($0A) is the value of 0 in my alphabet (i think ASCII uses $30?)
- jr z,$-5 ;repeat until we find a non-zero number
- ld de,numberString
- ld l,e
- ld h,d ;ld hl,numberString
- ld b,0 ;bc=# of leading 0s
- add hl,bc ;hl points to first non-zero character
- ld a,6 ;string = 6 bytes, 5 characters + EOS byte
- sub c
- ld c,a ;bc = number of non-zero bytes
- ldir ;essentially, for every 0, shift the number left one.
- pop hl
- ret
- dN_b2d:
- ld a,-1 ;if there is no carry the first run through, # = 0
- dN_b2dloop:
- inc a ;each iteration increase accumulator by 1
- add hl,de
- jr c,dN_b2dloop
- or a
- sbc hl,de ;225 - 100 = 125 - 100 = 25 - 100 = -75. This adds 100 again so we can check the next digits.
- exx
- ld hl,numberTable ;if you're using the ASCII chart, you can just add $30 and load that into numberString
- ld b,0
- ld c,a
- add hl,bc ;hl=numberTable, bc = number of units
- ldi
- exx
- ret
- ;the "ASCII" numbers of my text characters
- ; it's different than normal ASCII because
- ; i used a custom character table
- numberTable:
- ; 0 1 2 3 4 5 6 7 8 9
- .db 10,57,36,37,38,39,40,41,42,43
- numberString:
- .db 0,0,0,0,0,$FF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement