]> git.sur5r.net Git - cc65/blob - libsrc/common/atoi.s
"Inverted" time_t value handling.
[cc65] / libsrc / common / atoi.s
1 ;
2 ; Ullrich von Bassewitz, 05.06.1998
3 ;
4 ; int atoi (const char* s);
5 ; long atol (const char* s);
6 ;
7
8         .export         _atoi, _atol
9         .import         negeax, __ctype
10         .importzp       sreg, ptr1, ptr2, tmp1
11
12         .include        "ctype.inc"
13
14 ;
15 ; Conversion routine (32 bit)
16 ;
17
18 _atoi:
19 _atol:  sta     ptr1            ; Store s
20         stx     ptr1+1
21         ldy     #0
22         sty     ptr2
23         sty     ptr2+1          ; initial value (32 bit)
24         sty     sreg
25         sty     sreg+1
26
27 ; Skip whitespace
28
29 L1:     lda     (ptr1),y
30         tax
31         lda     __ctype,x       ; get character classification
32         and     #CT_SPACE_TAB   ; tab or space?
33         beq     L2              ; jump if no
34         iny
35         bne     L1
36         inc     ptr1+1
37         bne     L1              ; branch always
38
39 ; Check for a sign. The character is in X
40
41 L2:     txa                     ; get char
42         ldx     #0              ; flag: positive
43         cmp     #'+'            ; ### portable?
44         beq     L3
45         cmp     #'-'            ; ### portable?
46         bne     L5
47         dex                     ; flag: negative
48 L3:     iny
49         bne     L5
50         inc     ptr1+1
51
52 ; Store the sign flag and setup for conversion
53
54 L5:     stx     tmp1            ; remember sign flag
55
56 L6:     lda     (ptr1),y        ; get next char
57         tax
58         lda     __ctype,x       ; get character classification
59         and     #$04            ; digit?
60         beq     L8              ; done
61
62 ; Multiply ptr2 (the converted value) by 10
63
64         jsr     mul2            ; * 2
65
66         lda     sreg+1
67         pha
68         lda     sreg
69         pha
70         lda     ptr2+1
71         pha
72         lda     ptr2
73         pha                     ; Save value
74
75         jsr     mul2            ; * 4
76         jsr     mul2            ; * 8
77
78         clc
79         pla
80         adc     ptr2
81         sta     ptr2
82         pla
83         adc     ptr2+1
84         sta     ptr2+1
85         pla
86         adc     sreg
87         sta     sreg
88         pla
89         adc     sreg+1
90         sta     sreg+1          ; x*2 + x*8 = x*10
91
92 ; Get the character back and add it
93
94         txa                     ; get char back
95         sec
96         sbc     #'0'            ; make numeric value
97         clc
98         adc     ptr2
99         sta     ptr2
100         bcc     L7
101         inc     ptr2+1
102         bne     L7
103         inc     sreg
104         bne     L7
105         inc     sreg+1
106
107 ; Next character
108
109 L7:     iny
110         bne     L6
111         inc     ptr1+1
112         bne     L6
113
114 ; Conversion done. Load the low 16 bit into A/X
115
116 L8:     lda     ptr2
117         ldx     ptr2+1
118
119 ; Negate the value if necessary, otherwise we're done
120
121         ldy     tmp1            ; sign
122         beq     L9              ; Branch if positive
123
124 ; Negate the 32 bit value in ptr2/sreg
125
126         jmp     negeax
127
128 ;
129 ; Helper functions
130 ;
131
132 mul2:   asl     ptr2
133         rol     ptr2+1
134         rol     sreg
135         rol     sreg+1          ; * 2
136 L9:     rts
137
138