Z80 Routines:Other:DispA

From WikiTI
Revision as of 14:55, 3 April 2005 by Sigma (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
; ITOA
; --------------------------------------------------------------
; Converts a signed integer value to a zero-terminated ASCII
; string representative of that value (using radix 10).
; --------------------------------------------------------------
; INPUTS:
;     HL     Value to convert (two's complement integer).
;     DE     Base address of string destination. (pointer).
; --------------------------------------------------------------
; OUTPUTS:
;     None
; --------------------------------------------------------------
; REGISTERS/MEMORY DESTROYED
; AF HL
; --------------------------------------------------------------

itoa:
   push    de
   push    bc

; Detect sign of HL.
    bit    7, h
    jr     z, _DoConvert

; HL is negative. Output '-' to string and negate HL.
    ld     a, '-'
    ld     (de), a
    inc    de

; Negate HL (using two's complement)
    xor    a
    sub    l
    ld     l, a
    ld     a, 0     ; Note that XOR A or SUB A would disturb CF
    sbc    a, h
    ld     h, a

; Convert HL to digit characters
_DoConvert:
    ld     b, 0     ; B will count character length of number
-   ld     a, 10
    call   DivHLByA ; HL = HL / A, A = remainder
    push   af
    inc    b
    ld     a, h
    or     l
    jr     nz, -

; Retrieve digits from stack
-   pop    af
    or     $30
    ld     (de), a
    inc    de
    djnz   -

; Terminate string with NULL
    xor    a
    ld     (de), a

    pop    bc
    pop    de
    ret