Advertisement
Guest User

Number to String

a guest
Jan 5th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;#######################
  2. ;#NUMBERTOSTRING
  3. ;#Convert number in HL to a string
  4. ;#input: hl = number to display
  5. ;#output: hl = pointer to string
  6. ;#######################
  7. numberToString:
  8.     exx
  9.         ld de,numberString   ;for the ldi in dN_b2dloop
  10.     exx
  11.     ld de,-10000    ;check how many 10,000s there are in the number
  12.     call dN_b2d
  13.     ld de,-1000     ;check how many 1,000s units there are
  14.     call dN_b2d
  15.     ld de,-100      ;hundreds
  16.     call dN_b2d
  17.     ld de,-10       ;tens
  18.     call dN_b2d
  19.     ld de,-1        ;single digits
  20.     call dN_b2d
  21.  
  22.     ld c,-1         ;this part here removes the leading 0s
  23.     ld hl,numberString  ;where the string is stored
  24.     push hl
  25.         ld a,(hl)
  26.         inc c
  27.         inc hl
  28.         cp 10           ;ten ($0A) is the value of 0 in my alphabet (i think ASCII uses $30?)
  29.         jr z,$-5        ;repeat until we find a non-zero number
  30.         ld de,numberString
  31.         ld l,e
  32.         ld h,d          ;ld hl,numberString
  33.         ld b,0          ;bc=# of leading 0s
  34.         add hl,bc       ;hl points to first non-zero character
  35.         ld a,6          ;string = 6 bytes, 5 characters + EOS byte
  36.         sub c
  37.         ld c,a          ;bc = number of non-zero bytes
  38.         ldir            ;essentially, for every 0, shift the number left one.
  39.     pop hl
  40.     ret
  41.  
  42. dN_b2d:
  43.     ld a,-1         ;if there is no carry the first run through, # = 0
  44. dN_b2dloop:
  45.     inc a           ;each iteration increase accumulator by 1
  46.     add hl,de
  47.     jr c,dN_b2dloop
  48.     or a
  49.     sbc hl,de       ;225 - 100 = 125 - 100 = 25 - 100 = -75. This adds 100 again so we can check the next digits.
  50.     exx
  51.         ld hl,numberTable   ;if you're using the ASCII chart, you can just add $30 and load that into numberString
  52.         ld b,0
  53.         ld c,a
  54.         add hl,bc       ;hl=numberTable, bc = number of units
  55.         ldi
  56.     exx
  57.     ret
  58.  
  59. ;the "ASCII" numbers of my text characters
  60. ; it's different than normal ASCII because
  61. ; i used a custom character table
  62. numberTable:
  63. ;    0  1  2  3  4  5  6  7  8  9
  64. .db 10,57,36,37,38,39,40,41,42,43
  65. numberString:
  66. .db 0,0,0,0,0,$FF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement