-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi_base_bonus.s
115 lines (104 loc) · 1.73 KB
/
ft_atoi_base_bonus.s
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
section .text
global _ft_atoi_base
extern _ft_strlen
_ft_atoi_base: ; rdi = STR; rsi = BASE
push rdi
mov rdi, rsi ; rdi = BASE
call _ft_strlen
mov rdx, rax ; rdx = BASE SIZE
mov r13, rdi ; COPY BASE TO CHECK WHITESPACE
pop r15 ; rcx = STR
mov r10, 0
mov r14, 0
mov rax, 0
dec r13
.check_base_valid:
inc r13
mov al, byte[r13]
cmp al, ' '
je .exit
cmp al, 9
je .exit
cmp al, 10
je .exit
cmp al, 11
je .exit
cmp al, 12
je .exit
cmp al, 13
je .exit
cmp al, '+'
je .exit
cmp al, '-'
je .exit
mov r12, r13
cmp al, 0
jne .check_double_in_base
dec r15
.ignore_white_space:
inc r15
mov al, byte[r15]
cmp al, ' '
je .ignore_white_space
cmp al, 9
je .ignore_white_space
cmp al, 10
je .ignore_white_space
cmp al, 11
je .ignore_white_space
cmp al, 12
je .ignore_white_space
cmp al, 13
je .ignore_white_space
dec r15
.signs:
inc r15
cmp byte[r15], '+'
je .signs
cmp byte[r15], '-'
je .inc_sign
.find_index_loop:
mov rax, 0 ; RESET RAX && RBX to use al & bl
mov rbx, 0
mov bl, byte[rdi] ; bl = BASE SEARCHING INDEX
mov al, byte[r15] ; al = ACTUAL NUMBER IN STR
cmp al, 0 ; If str is at end, exit
je .exit
cmp bl, 0
je .exit
cmp al, bl
je .atoi_loop
inc rdi
jmp .find_index_loop
.atoi_loop:
sub rdi, rsi
mov rbx, rdi
mov rdi, rsi ; rdi = BASE
imul r10, rdx ; counter * base_size
add r10, rbx ; counter + index
inc r15
mov rdi, rsi
jmp .find_index_loop
.inc_sign:
cmp r14, 0
je .set_positive
mov r14, 0
jmp .signs
.set_positive:
mov r14, 1
jmp .signs
.return_negative:
imul rax, -1
ret
.exit:
mov rax, r10
cmp r14, 1
je .return_negative
ret
.check_double_in_base:
inc r12
cmp byte[r12], 0
je .check_base_valid
cmp byte[r12], al
je .exit
jmp .check_double_in_base