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 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: get_char_index:
xor rax, rax 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 cmp dil, [rsi] ;is char invalid
jz get_char_index_found jz .get_char_index_found
mov al, [rsi] ; is end of string mov al, [rsi] ; is end of string
test al, al test al, al
lea rsi, [rsi + 1] lea rsi, [rsi + 1]
jne get_char_index_loop jne .get_char_index_loop
.error
mov rax, -1 ;not found and at the end mov rax, -1 ;not found and at the end
pop rsi pop rsi
ret ret
get_char_index_found: .get_char_index_found:
pop rax ;return index of the found char pop rax ;return index of the found char (compare to top of stack)
sub rsi, rax sub rsi, rax
mov rax, rsi mov rax, rsi
ret ret
@ -113,9 +120,9 @@ base_ok:
ft_atoi_base: ft_atoi_base:
xor rax, rax ;check if there is a null pointer in rdi or rsi xor rax, rax ;check if there is a null pointer in rdi or rsi
test rdi, rdi test rdi, rdi
jz final_number jz return
test rsi, rsi test rsi, rsi
jz final_number jz return
push rdi ; is base valid push rdi ; is base valid
push rsi push rsi
@ -126,10 +133,8 @@ ft_atoi_base:
test rax, rax test rax, rax
jne whitespace_skip ; if 0 , base is not valid jne whitespace_skip ; if 0 , base is not valid
xor rax, rax
ret ret
whitespace_skip_inc: whitespace_skip_inc:
lea rdi, [rdi + 1] lea rdi, [rdi + 1]
whitespace_skip: whitespace_skip:
@ -160,6 +165,7 @@ plus_minus:
cmp al, '+' cmp al, '+'
jz plus_minus_inc jz plus_minus_inc
push rdx ; keep invert sign setting in stack
push rdi push rdi
@ -169,12 +175,10 @@ plus_minus:
pop rsi pop rsi
pop rdi pop rdi
mov rbx, rax mov rbx, rax ; keep the length of the base in rbx
xor rax, rax xor rax, rax
push rdx
number_loop: number_loop:
mov dl, [rdi] mov dl, [rdi]
test dl, dl test dl, dl
@ -191,7 +195,7 @@ number_loop:
mov rdx, rax ; save return value in rdx mov rdx, rax ; save return value in rdx
pop rax pop rax
cmp rdx, -1 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 push rdx ; imul sets rdx to 0
@ -211,4 +215,5 @@ final_number:
neg_final_number: neg_final_number:
neg rax neg rax
return:
ret ret

View File

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

View File

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