2 ; Ullrich von Bassewitz, 05.06.1998
4 ; int atoi (const char* s);
5 ; long atol (const char* s);
9 .import negeax, __ctype
10 .importzp sreg, ptr1, ptr2, tmp1
15 ; Conversion routine (32 bit)
19 _atol: sta ptr1 ; Store s
23 sty ptr2+1 ; initial value (32 bit)
31 lda __ctype,x ; get character classification
32 and #CT_SPACE_TAB ; tab or space?
37 bne L1 ; branch always
39 ; Check for a sign. The character is in X
42 ldx #0 ; flag: positive
43 cmp #'+' ; ### portable?
45 cmp #'-' ; ### portable?
52 ; Store the sign flag and setup for conversion
54 L5: stx tmp1 ; remember sign flag
56 L6: lda (ptr1),y ; get next char
58 lda __ctype,x ; get character classification
62 ; Multiply ptr2 (the converted value) by 10
90 sta sreg+1 ; x*2 + x*8 = x*10
92 ; Get the character back and add it
96 sbc #'0' ; make numeric value
114 ; Conversion done. Load the low 16 bit into A/X
119 ; Negate the value if necessary, otherwise we're done
122 beq L9 ; Branch if positive
124 ; Negate the 32 bit value in ptr2/sreg