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