change labels from global to local in functions, align function calls in stack

This commit is contained in:
2025-08-06 20:10:05 +02:00
parent 2a65c14032
commit caa21a3fb0
11 changed files with 119 additions and 108 deletions

View File

@ -29,7 +29,7 @@ get_char_index:
lea rsi, [rsi + 1]
jne .get_char_index_loop
.error
.error:
mov rax, -1 ;not found and at the end
pop rsi
ret
@ -47,7 +47,7 @@ check_duplicate_char:
mov al ,[rdi] ; test if \0
test al, al
jz no_duplicate
jz .no_duplicate
push rdi
mov rsi, rdi
@ -62,7 +62,7 @@ check_duplicate_char:
mov rax, 1
ret
no_duplicate:
.no_duplicate:
xor rax, rax
ret
@ -76,25 +76,24 @@ check_base:
mov al, [rdi] ; check if it's an empty string
test al, al
jne chk_bs_duplicate
jne .chk_bs_duplicate
xor rax, rax
ret
chk_bs_duplicate:
.chk_bs_duplicate:
push rdi
call check_duplicate_char
pop rdi
test rax, rax
jz chk_bs_char
jz .chk_bs_char
xor rax, rax
ret
chk_bs_char:
.chk_bs_char:
mov al, [rdi] ; if rdi is \0, return 0
test al, al
jz base_ok
jz .base_ok
push rdi ; if current char is in invalid_chars
mov dil, [rdi];
@ -104,12 +103,12 @@ chk_bs_char:
cmp rax, -1 ; if not -1, error
lea rdi, [rdi + 1]
jz chk_bs_char
jz .chk_bs_char
xor rax, rax; return 0
ret
base_ok:
.base_ok:
mov rax,1
ret
@ -120,50 +119,54 @@ base_ok:
ft_atoi_base:
xor rax, rax ;check if there is a null pointer in rdi or rsi
test rdi, rdi
jz return
jz .return
test rsi, rsi
jz return
jz .return
push rdi ; is base valid
push rsi
mov rdi, rsi
sub rsp, 8
call check_base
add rsp, 8
pop rsi
pop rdi
test rax, rax
jne whitespace_skip ; if 0 , base is not valid
jne .whitespace_skip ; if 0 , base is not valid
ret
whitespace_skip_inc:
.whitespace_skip_inc:
lea rdi, [rdi + 1]
whitespace_skip:
.whitespace_skip:
push rdi
push rsi
mov dil, [rdi]
lea rsi, [rel whitespaces]
sub rsp, 8
call get_char_index
add rsp, 8
pop rsi
pop rdi
cmp rax, 0
jge whitespace_skip_inc
jge .whitespace_skip_inc
xor rdx, rdx
jmp plus_minus
jmp .plus_minus
plus_minus_invert:
.plus_minus_invert:
xor rdx, 1
plus_minus_inc:
.plus_minus_inc:
lea rdi, [rdi + 1]
plus_minus:
.plus_minus:
mov al, [rdi]
cmp al, '-'
jz plus_minus_invert
jz .plus_minus_invert
cmp al, '+'
jz plus_minus_inc
jz .plus_minus_inc
push rdx ; keep invert sign setting in stack
@ -179,16 +182,18 @@ plus_minus:
xor rax, rax
number_loop:
.number_loop:
mov dl, [rdi]
test dl, dl
jz final_number
jz .final_number
push rax
push rdi
push rsi
mov dil, dl
sub rsp, 8
call get_char_index
add rsp, 8
pop rsi
pop rdi
@ -196,7 +201,7 @@ number_loop:
pop rax
cmp rdx, -1 ; if the char is not in the base, return the number
jz final_number
jz .final_number
push rdx ; imul sets rdx to 0
imul rbx
@ -205,15 +210,15 @@ number_loop:
lea rdi, [rdi + 1]
jmp number_loop
jmp .number_loop
final_number:
.final_number:
pop rdx
test rdx, rdx
jne neg_final_number
jne .neg_final_number
ret
neg_final_number:
.neg_final_number:
neg rax
return:
.return:
ret