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