24-bit number-to-string

Here you can share sprites, code, sheets or ideas that you have
  • Author
  • Message
Offline
User avatar

chickendude

Staff Member

Topic Starter

24-bit number-to-string

Post21 May 2013, 17:24

I just wrote a 24-bit number-to-string converter today, if anyone's interested:
Code: Select all
;#######################
;#NUM2STR
;# Convert number in AHL to a string
;# input: ahl = number to display
;# output: hl = pointer to converted string
;#######################
num2str:
      ld ix,numberString      ;where we will store the result
;-10,000,000
      ld c,$67
      ld de,$6980
      call b2d                ;count how many 10,000,000s there are
;-1,000,000
      ld c,$F0
      ld de,$BDC0
      call b2d
;-100,000
      ld c,$FE
      ld de,$7960
      call b2d
;-10,000
      inc c             ;ld c,$FF
      ld de,-10000
      call b2d
;-1,000
      ld de,-1000
      call b2d
;-100
      ld de,-100
      call b2d
;-10
      ld e,-10
      call b2d
;-1
      ld e,-1
      call b2d
      ld (ix),a         ;put the terminating zero
      ld hl,numberString-1  ;where the string is stored
            inc hl
            ld a,(hl)
            cp '0'            ;if it's a 0, skip it
       jr z,$-4         ;repeat until we find a non-zero number
      or a
       jr nz,$+3        ;if all numbers are zeros, move hl back one
            dec hl            ;.. to display one zero
      ret

;#######################
;#B2D
;# Convert the units of a 24-bit binary
;#    number in AHL into decimal
;# input: ahl = number to display
;#            cde = negative value of units to check (if you
;#                      want to check 10s, cde should equal -10)
;#            ix  = string pointer to store the unit
;#######################
b2d:
      ld b,-1                 ;if there is no carry the first run through, # = 0
b2dLoop:
            inc b       ;each iteration increase accumulator by 1
            add hl,de
            adc a,c
       jr c,b2dLoop
      sbc hl,de         ;225 - 100 = 125 - 100 = 25 - 100 = -75. This adds 100 again so we can check the next digits.
      sbc a,c
      set 4,b
      set 5,b                 ;b+$30
      ld (ix),b
      inc ix
      ret
I'm not sure if using the shadow registers would be faster/more memory efficient than using ix since passing b to the shadow registers would involve either a push/pop or some tricky register juggling.

Return to Resources and Ideas

Who is online

Users browsing this forum: No registered users and 2 guests

cron