aboutsummaryrefslogtreecommitdiff
path: root/ultostr.s
blob: e63fd3e0ecbb5a5e2f2d133631c695ade2ca0fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
;
; Modified from cc65's libsrc/common/ltoa.s
; Originally by Ullrich von Bassewitz, 11.06.1998

; modified version by B. Watson

; - rename ultoa => ultostr
; - got rid of ltoa (don't need it)
; - hardcode radix to 10
; - don't need __hextab since we don't do hex (saves 16 bytes)
; - inline dopop subroutine

; char* ultostr (unsigned long value, char* s);

        .export         _ultostr
        .import         popax
        .importzp       sreg, ptr1, ptr2, ptr3

radix = 10

 .ifdef CART_TARGET
  .segment "HIGHCODE"
 .else
  .code
 .endif

_ultostr:
        ; pop the arguments
        sta     ptr1
        stx     ptr1+1
        sta     sreg            ; save for return
        stx     sreg+1
        jsr     popax           ; get low word of value
        sta     ptr2
        stx     ptr2+1
        jsr     popax           ; get high word of value
        sta     ptr3
        stx     ptr3+1

; Convert to string by dividing and push the result onto the stack

ultostr:  lda     #$00
        pha                     ; sentinel

; Divide val/radix -> val, remainder in a

L5:     ldy     #32             ; 32 bit
        lda     #0              ; remainder
L6:     asl     ptr2
        rol     ptr2+1
        rol     ptr3
        rol     ptr3+1
        rol     a
        cmp     #radix
        bcc     L7
        sbc     #radix
        inc     ptr2
L7:     dey
        bne     L6

        ora #'0'                ; get ascii character
        pha                     ; save char value on stack

        lda     ptr2
        ora     ptr2+1
        ora     ptr3
        ora     ptr3+1
        bne     L5

; Get the characters from the stack into the string

        ldy     #0
L9:     pla
        sta     (ptr1),y
        beq     L10             ; jump if sentinel
        iny
        bne     L9              ; jump always

; Done! Return the target string

L10:    lda     sreg
        ldx     sreg+1
        rts