]> git.sur5r.net Git - cc65/blob - libsrc/common/ltoa.s
Merge pull request #659 from polluks/patch-10
[cc65] / libsrc / common / ltoa.s
1 ;
2 ; Ullrich von Bassewitz, 11.06.1998
3 ;
4 ; char* ltoa (long value, char* s, int radix);
5 ; char* ultoa (unsigned long value, char* s, int radix);
6 ;
7
8         .export         _ltoa, _ultoa
9         .import         popax, popptr1, negeax
10         .import         __hextab, __longminstr
11         .importzp       sreg, ptr1, ptr2, ptr3, tmp1
12
13         .macpack        cpu
14
15 .code
16
17 ;
18 ; Common subroutine to pop the parameters and put them into core
19 ;
20
21 dopop:  sta     tmp1            ; will loose high byte
22         jsr     popax           ; get s to ptr2
23         sta     ptr2
24         stx     ptr2+1
25         sta     ptr3            ; save for return
26         stx     ptr3+1
27         jsr     popptr1         ; get low word of value to ptr1
28         jsr     popax           ; get high word of value to sreg
29         sta     sreg
30         stx     sreg+1
31         rts
32
33 ;
34 ; ltoa
35 ;
36
37 _ltoa:  jsr     dopop           ; pop the arguments
38
39 ; We must handle $80000000 in a special way, since it is the only negative
40 ; number that has no positive 32-bit counterpart
41
42         ldx     sreg+1          ; get high byte
43         ldy     tmp1            ; get radix
44         cpy     #10
45         bne     ultoa
46         lda     sreg
47         ora     ptr1+1
48         ora     ptr1
49         bne     L2
50         cpx     #$80
51         bne     L2
52
53         ldy     #11
54 L1:     lda     __longminstr,y  ; copy -2147483648
55         sta     (ptr2),y
56         dey
57         bpl     L1
58         jmp     L10
59                   
60 ; Check if the value is negative. If so, write a - sign and negate the
61 ; number.
62
63 L2:     txa                     ; get high byte
64         bpl     ultoa
65         lda     #'-'
66
67 .if (.cpu .bitand CPU_ISET_65SC02)
68         sta     (ptr2)
69 .else        
70         ldy     #0
71         sta     (ptr2),y        ; store sign
72 .endif
73
74         inc     ptr2
75         bne     L3
76         inc     ptr2+1
77
78 L3:     lda     ptr1            ; negate val
79         ldx     ptr1+1
80
81         jsr     negeax
82         
83         sta     ptr1
84         stx     ptr1+1
85         jmp     ultoa
86
87 ;
88 ; utoa
89 ;
90
91 _ultoa: jsr     dopop           ; pop the arguments
92
93 ; Convert to string by dividing and push the result onto the stack
94
95 ultoa:  lda     #$00
96         pha                     ; sentinel
97
98 ; Divide val/tmp1 -> val, remainder in a
99
100 L5:     ldy     #32             ; 32 bit
101         lda     #0              ; remainder
102 L6:     asl     ptr1
103         rol     ptr1+1
104         rol     sreg
105         rol     sreg+1
106         rol     a
107         cmp     tmp1
108         bcc     L7
109         sbc     tmp1
110         inc     ptr1
111 L7:     dey
112         bne     L6
113
114         tay                     ; get remainder into y
115         lda     __hextab,y      ; get hex character
116         pha                     ; save char value on stack
117
118         lda     ptr1
119         ora     ptr1+1
120         ora     sreg
121         ora     sreg+1
122         bne     L5
123
124 ; Get the characters from the stack into the string
125
126         ldy     #0
127 L9:     pla
128         sta     (ptr2),y
129         beq     L10             ; jump if sentinel
130         iny
131         bne     L9              ; jump always
132
133 ; Done! Return the target string
134
135 L10:    lda     ptr3
136         ldx     ptr3+1
137         rts
138
139
140
141
142
143
144
145
146
147
148
149
150
151