aboutsummaryrefslogtreecommitdiff
path: root/src/a2uint.s
blob: ac8a7ed379d9c499a11a288443ce016a1e0f76da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

 ; 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