fix atoi and list_remove_if

This commit is contained in:
2025-08-06 19:15:09 +02:00
parent 49501469c8
commit 2a65c14032
3 changed files with 39 additions and 27 deletions

View File

@ -9,26 +9,33 @@ whitespaces: db " ", 0x0c, 0x0a, 0x0d, 0x09, 0x0b, 0
section .text
; checks if a char is in the invalid_chars
; get index of char in string or returns -1 if not found
; char in dil
; str in rsi
get_char_index:
xor rax, rax
push rsi
get_char_index_loop:
push rsi ; keep start index in stack
test dil, dil
je .error
.get_char_index_loop:
cmp dil, [rsi] ;is char invalid
jz get_char_index_found
jz .get_char_index_found
mov al, [rsi] ; is end of string
test al, al
lea rsi, [rsi + 1]
jne get_char_index_loop
jne .get_char_index_loop
.error
mov rax, -1 ;not found and at the end
pop rsi
ret
get_char_index_found:
pop rax ;return index of the found char
.get_char_index_found:
pop rax ;return index of the found char (compare to top of stack)
sub rsi, rax
mov rax, rsi
ret
@ -113,9 +120,9 @@ base_ok:
ft_atoi_base:
xor rax, rax ;check if there is a null pointer in rdi or rsi
test rdi, rdi
jz final_number
jz return
test rsi, rsi
jz final_number
jz return
push rdi ; is base valid
push rsi
@ -126,10 +133,8 @@ ft_atoi_base:
test rax, rax
jne whitespace_skip ; if 0 , base is not valid
xor rax, rax
ret
whitespace_skip_inc:
lea rdi, [rdi + 1]
whitespace_skip:
@ -160,6 +165,7 @@ plus_minus:
cmp al, '+'
jz plus_minus_inc
push rdx ; keep invert sign setting in stack
push rdi
@ -169,12 +175,10 @@ plus_minus:
pop rsi
pop rdi
mov rbx, rax
mov rbx, rax ; keep the length of the base in rbx
xor rax, rax
push rdx
number_loop:
mov dl, [rdi]
test dl, dl
@ -191,7 +195,7 @@ number_loop:
mov rdx, rax ; save return value in rdx
pop rax
cmp rdx, -1
cmp rdx, -1 ; if the char is not in the base, return the number
jz final_number
push rdx ; imul sets rdx to 0
@ -211,4 +215,5 @@ final_number:
neg_final_number:
neg rax
return:
ret

View File

@ -9,8 +9,10 @@ ft_list_push_front:
push rdi
push rsi
sub rsp, 8
mov rdi, 16
call malloc wrt ..plt
add rsp, 8
pop rsi
pop rdi

View File

@ -4,25 +4,25 @@ extern free
section .text
;this function remove an element from a list, set the previous element to the next
; the element ot remove is in rax
; the element to remove is in rax
; the previouse element is in r8
; the pointer to the start of the list is in rdi
remove_element:
push rsi
mov rsi, [rax + 8]
mov rsi, [rax + 8] ; save cur->next in rsi
call call_free_elem
test r8, r8
jz first_elem
jz .first_elem ;if it's the first element, skip setting prev
mov [r8 + 8], rsi
jmp remove_element_end
mov [r8 + 8], rsi ; prev->next = rsi
jmp .remove_element_end
first_elem:
mov [rdi], rsi
.first_elem:
mov [rdi], rsi ;set list start to rsi
remove_element_end:
mov rax, rsi
.remove_element_end:
mov rax, rsi ; set cur element to rsi;
pop rsi
ret
@ -37,7 +37,9 @@ call_free_elem:
push rax
mov rdi, [rax]
call rcx
sub rsp, 8
call rcx ; call remove
add rsp, 8
pop rdi
call free wrt ..plt
@ -76,7 +78,9 @@ loop:
push r8
mov rdi, [rax]
sub rsp, 8
call rdx
add rsp, 8
test eax, eax
pop r8
@ -85,10 +89,12 @@ loop:
pop rsi
pop rdi
jz loop_end
jnz loop_end ;if call to rdx returned zero, don't remove
pop rax
sub rsp, 8
call remove_element
add rsp, 8
jmp loop
loop_end:
@ -98,6 +104,5 @@ loop_end:
mov rax, [rax + 8]
jmp loop
end:
ret