
 ; replacement for atoi() that doesn't use cc65's bloated ctype.
 ; also, doesn't handle signed results (not needed).

 .export _a2uint
 .import _isnum
 .importzp ptr1, sreg

 result = sreg
 temp16 = sreg+2

_a2uint:
 sta ptr1
 stx ptr1+1

 lda #0
 sta result
 sta result+1
 tay

@chrloop:
 lda (ptr1),y ; get next character
 jsr _isnum   ; is is a digit?
 beq @done    ; Z set = non-digit

 ; multiply result by 10
 ; {
 lda result
 asl
 sta temp16
 lda result+1
 rol
 sta temp16+1 ; temp16 now result * 2

 ldx #3
@x8loop:
 asl result
 rol result+1
 dex
 bne @x8loop

 ; result now result * 8
 clc
 lda result
 adc temp16
 sta result
 lda result+1
 adc temp16+1
 sta result+1 ; result now original result * 10
 ; }

 lda (ptr1),y ; get character again
 iny          ; point to next char
 and #$0f     ; de-ASCIIfy
 clc
 adc result
 sta result
 bcc @chrloop
 inc result+1
 bne @chrloop

@done:
 lda result
 ldx result+1
 rts
