2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 05.06.1998
|
|
|
|
;
|
|
|
|
; int atoi (const char* s);
|
|
|
|
; long atol (const char* s);
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _atoi, _atol
|
|
|
|
.import negeax, __ctype
|
|
|
|
.importzp sreg, ptr1, ptr2, tmp1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-02-10 22:11:56 +00:00
|
|
|
.include "ctype.inc"
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Conversion routine (32 bit)
|
|
|
|
;
|
|
|
|
|
|
|
|
_atoi:
|
2013-05-09 11:56:54 +00:00
|
|
|
_atol: sta ptr1 ; Store s
|
|
|
|
stx ptr1+1
|
|
|
|
ldy #0
|
|
|
|
sty ptr2
|
|
|
|
sty ptr2+1 ; initial value (32 bit)
|
|
|
|
sty sreg
|
|
|
|
sty sreg+1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Skip whitespace
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
L1: lda (ptr1),y
|
|
|
|
tax
|
|
|
|
lda __ctype,x ; get character classification
|
|
|
|
and #CT_SPACE_TAB ; tab or space?
|
|
|
|
beq L2 ; jump if no
|
|
|
|
iny
|
|
|
|
bne L1
|
|
|
|
inc ptr1+1
|
|
|
|
bne L1 ; branch always
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Check for a sign. The character is in X
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
L2: txa ; get char
|
|
|
|
ldx #0 ; flag: positive
|
|
|
|
cmp #'+' ; ### portable?
|
|
|
|
beq L3
|
|
|
|
cmp #'-' ; ### portable?
|
|
|
|
bne L5
|
|
|
|
dex ; flag: negative
|
2000-05-28 13:40:48 +00:00
|
|
|
L3: iny
|
2013-05-09 11:56:54 +00:00
|
|
|
bne L5
|
|
|
|
inc ptr1+1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Store the sign flag and setup for conversion
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
L5: stx tmp1 ; remember sign flag
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
L6: lda (ptr1),y ; get next char
|
|
|
|
tax
|
|
|
|
lda __ctype,x ; get character classification
|
|
|
|
and #$04 ; digit?
|
|
|
|
beq L8 ; done
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Multiply ptr2 (the converted value) by 10
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr mul2 ; * 2
|
|
|
|
|
|
|
|
lda sreg+1
|
|
|
|
pha
|
|
|
|
lda sreg
|
|
|
|
pha
|
|
|
|
lda ptr2+1
|
|
|
|
pha
|
|
|
|
lda ptr2
|
|
|
|
pha ; Save value
|
|
|
|
|
|
|
|
jsr mul2 ; * 4
|
|
|
|
jsr mul2 ; * 8
|
|
|
|
|
|
|
|
clc
|
|
|
|
pla
|
|
|
|
adc ptr2
|
|
|
|
sta ptr2
|
|
|
|
pla
|
|
|
|
adc ptr2+1
|
|
|
|
sta ptr2+1
|
|
|
|
pla
|
|
|
|
adc sreg
|
|
|
|
sta sreg
|
|
|
|
pla
|
|
|
|
adc sreg+1
|
|
|
|
sta sreg+1 ; x*2 + x*8 = x*10
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Get the character back and add it
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
txa ; get char back
|
|
|
|
sec
|
|
|
|
sbc #'0' ; make numeric value
|
|
|
|
clc
|
|
|
|
adc ptr2
|
|
|
|
sta ptr2
|
|
|
|
bcc L7
|
|
|
|
inc ptr2+1
|
|
|
|
bne L7
|
|
|
|
inc sreg
|
|
|
|
bne L7
|
|
|
|
inc sreg+1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Next character
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
L7: iny
|
|
|
|
bne L6
|
|
|
|
inc ptr1+1
|
|
|
|
bne L6
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-15 21:25:44 +00:00
|
|
|
; Conversion done. Load the low 16 bit into A/X
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-15 21:25:44 +00:00
|
|
|
L8: lda ptr2
|
|
|
|
ldx ptr2+1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-15 21:25:44 +00:00
|
|
|
; Negate the value if necessary, otherwise we're done
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy tmp1 ; sign
|
|
|
|
beq L9 ; Branch if positive
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-15 21:25:44 +00:00
|
|
|
; Negate the 32 bit value in ptr2/sreg
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-15 21:25:44 +00:00
|
|
|
jmp negeax
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
;
|
|
|
|
; Helper functions
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
mul2: asl ptr2
|
|
|
|
rol ptr2+1
|
|
|
|
rol sreg
|
|
|
|
rol sreg+1 ; * 2
|
2009-09-15 21:25:44 +00:00
|
|
|
L9: rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|