forked from fox32-arch/fox32rom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer.asm
63 lines (54 loc) · 1.24 KB
/
integer.asm
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
; integer routines
; convert ASCII string into an integer
; thanks to lua for helping with this :3
; inputs:
; r0: pointer to null-terminated string
; r1: radix (i.e. 10 for decimal, 16 for hexadecimal)
; outputs:
; r0: integer
string_to_int:
push r2
push r3
mov r3, 0
string_to_int_loop:
movz.8 r2, [r0]
inc r0
cmp r2, 0
ifz jmp string_to_int_end
; if (digit >= '0' && digit <= '9') {
; digit -= '0';
; } else if (digit >= 'A' && digit <= 'Z') {
; digit -= 'A' - 10
; } else if (digit >= 'a' && digit <= 'z') {
; digit -= 'a' - 10
; } else {
; continue;
; }
cmp r2, '0'
iflt jmp string_to_int_loop_2
cmp r2, '9'
ifgt jmp string_to_int_loop_2
sub r2, '0'
jmp string_to_int_loop_end
string_to_int_loop_2:
cmp r2, 'A'
iflt jmp string_to_int_loop_3
cmp r2, 'Z'
ifgt jmp string_to_int_loop_3
sub r2, 0x37 ; 'A' - 10
jmp string_to_int_loop_end
string_to_int_loop_3:
cmp r2, 'a'
iflt jmp string_to_int_loop
cmp r2, 'z'
ifgt jmp string_to_int_loop
sub r2, 0x57 ; 'a' - 10
string_to_int_loop_end:
mul r3, r1
add r3, r2
jmp string_to_int_loop
string_to_int_end:
mov r0, r3
pop r3
pop r2
ret