change labels from global to local in functions, align function calls in stack
This commit is contained in:
@ -29,7 +29,7 @@ get_char_index:
|
|||||||
lea rsi, [rsi + 1]
|
lea rsi, [rsi + 1]
|
||||||
jne .get_char_index_loop
|
jne .get_char_index_loop
|
||||||
|
|
||||||
.error
|
.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
|
||||||
@ -47,7 +47,7 @@ check_duplicate_char:
|
|||||||
|
|
||||||
mov al ,[rdi] ; test if \0
|
mov al ,[rdi] ; test if \0
|
||||||
test al, al
|
test al, al
|
||||||
jz no_duplicate
|
jz .no_duplicate
|
||||||
|
|
||||||
push rdi
|
push rdi
|
||||||
mov rsi, rdi
|
mov rsi, rdi
|
||||||
@ -62,7 +62,7 @@ check_duplicate_char:
|
|||||||
mov rax, 1
|
mov rax, 1
|
||||||
ret
|
ret
|
||||||
|
|
||||||
no_duplicate:
|
.no_duplicate:
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
ret
|
ret
|
||||||
|
|
||||||
@ -76,25 +76,24 @@ check_base:
|
|||||||
|
|
||||||
mov al, [rdi] ; check if it's an empty string
|
mov al, [rdi] ; check if it's an empty string
|
||||||
test al, al
|
test al, al
|
||||||
jne chk_bs_duplicate
|
jne .chk_bs_duplicate
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
ret
|
ret
|
||||||
|
|
||||||
chk_bs_duplicate:
|
.chk_bs_duplicate:
|
||||||
push rdi
|
push rdi
|
||||||
call check_duplicate_char
|
call check_duplicate_char
|
||||||
pop rdi
|
pop rdi
|
||||||
test rax, rax
|
test rax, rax
|
||||||
jz chk_bs_char
|
jz .chk_bs_char
|
||||||
|
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
.chk_bs_char:
|
||||||
chk_bs_char:
|
|
||||||
mov al, [rdi] ; if rdi is \0, return 0
|
mov al, [rdi] ; if rdi is \0, return 0
|
||||||
test al, al
|
test al, al
|
||||||
jz base_ok
|
jz .base_ok
|
||||||
|
|
||||||
push rdi ; if current char is in invalid_chars
|
push rdi ; if current char is in invalid_chars
|
||||||
mov dil, [rdi];
|
mov dil, [rdi];
|
||||||
@ -104,12 +103,12 @@ chk_bs_char:
|
|||||||
|
|
||||||
cmp rax, -1 ; if not -1, error
|
cmp rax, -1 ; if not -1, error
|
||||||
lea rdi, [rdi + 1]
|
lea rdi, [rdi + 1]
|
||||||
jz chk_bs_char
|
jz .chk_bs_char
|
||||||
|
|
||||||
xor rax, rax; return 0
|
xor rax, rax; return 0
|
||||||
ret
|
ret
|
||||||
|
|
||||||
base_ok:
|
.base_ok:
|
||||||
mov rax,1
|
mov rax,1
|
||||||
ret
|
ret
|
||||||
|
|
||||||
@ -120,50 +119,54 @@ 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 return
|
jz .return
|
||||||
test rsi, rsi
|
test rsi, rsi
|
||||||
jz return
|
jz .return
|
||||||
|
|
||||||
push rdi ; is base valid
|
push rdi ; is base valid
|
||||||
push rsi
|
push rsi
|
||||||
mov rdi, rsi
|
mov rdi, rsi
|
||||||
|
sub rsp, 8
|
||||||
call check_base
|
call check_base
|
||||||
|
add rsp, 8
|
||||||
pop rsi
|
pop rsi
|
||||||
pop rdi
|
pop rdi
|
||||||
|
|
||||||
test rax, rax
|
test rax, rax
|
||||||
jne whitespace_skip ; if 0 , base is not valid
|
jne .whitespace_skip ; if 0 , base is not valid
|
||||||
ret
|
ret
|
||||||
|
|
||||||
whitespace_skip_inc:
|
.whitespace_skip_inc:
|
||||||
lea rdi, [rdi + 1]
|
lea rdi, [rdi + 1]
|
||||||
whitespace_skip:
|
.whitespace_skip:
|
||||||
push rdi
|
push rdi
|
||||||
push rsi
|
push rsi
|
||||||
mov dil, [rdi]
|
mov dil, [rdi]
|
||||||
lea rsi, [rel whitespaces]
|
lea rsi, [rel whitespaces]
|
||||||
|
sub rsp, 8
|
||||||
call get_char_index
|
call get_char_index
|
||||||
|
add rsp, 8
|
||||||
pop rsi
|
pop rsi
|
||||||
pop rdi
|
pop rdi
|
||||||
|
|
||||||
cmp rax, 0
|
cmp rax, 0
|
||||||
jge whitespace_skip_inc
|
jge .whitespace_skip_inc
|
||||||
xor rdx, rdx
|
xor rdx, rdx
|
||||||
jmp plus_minus
|
jmp .plus_minus
|
||||||
|
|
||||||
plus_minus_invert:
|
.plus_minus_invert:
|
||||||
xor rdx, 1
|
xor rdx, 1
|
||||||
|
|
||||||
plus_minus_inc:
|
.plus_minus_inc:
|
||||||
lea rdi, [rdi + 1]
|
lea rdi, [rdi + 1]
|
||||||
|
|
||||||
plus_minus:
|
.plus_minus:
|
||||||
mov al, [rdi]
|
mov al, [rdi]
|
||||||
cmp al, '-'
|
cmp al, '-'
|
||||||
jz plus_minus_invert
|
jz .plus_minus_invert
|
||||||
|
|
||||||
cmp al, '+'
|
cmp al, '+'
|
||||||
jz plus_minus_inc
|
jz .plus_minus_inc
|
||||||
|
|
||||||
push rdx ; keep invert sign setting in stack
|
push rdx ; keep invert sign setting in stack
|
||||||
|
|
||||||
@ -179,16 +182,18 @@ plus_minus:
|
|||||||
|
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
|
|
||||||
number_loop:
|
.number_loop:
|
||||||
mov dl, [rdi]
|
mov dl, [rdi]
|
||||||
test dl, dl
|
test dl, dl
|
||||||
jz final_number
|
jz .final_number
|
||||||
|
|
||||||
push rax
|
push rax
|
||||||
push rdi
|
push rdi
|
||||||
push rsi
|
push rsi
|
||||||
mov dil, dl
|
mov dil, dl
|
||||||
|
sub rsp, 8
|
||||||
call get_char_index
|
call get_char_index
|
||||||
|
add rsp, 8
|
||||||
pop rsi
|
pop rsi
|
||||||
pop rdi
|
pop rdi
|
||||||
|
|
||||||
@ -196,7 +201,7 @@ number_loop:
|
|||||||
pop rax
|
pop rax
|
||||||
|
|
||||||
cmp rdx, -1 ; if the char is not in the base, return the number
|
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
|
||||||
imul rbx
|
imul rbx
|
||||||
@ -205,15 +210,15 @@ number_loop:
|
|||||||
|
|
||||||
lea rdi, [rdi + 1]
|
lea rdi, [rdi + 1]
|
||||||
|
|
||||||
jmp number_loop
|
jmp .number_loop
|
||||||
|
|
||||||
final_number:
|
.final_number:
|
||||||
pop rdx
|
pop rdx
|
||||||
test rdx, rdx
|
test rdx, rdx
|
||||||
jne neg_final_number
|
jne .neg_final_number
|
||||||
ret
|
ret
|
||||||
|
|
||||||
neg_final_number:
|
.neg_final_number:
|
||||||
neg rax
|
neg rax
|
||||||
return:
|
.return:
|
||||||
ret
|
ret
|
||||||
|
@ -5,19 +5,19 @@ section .text
|
|||||||
|
|
||||||
ft_list_push_front:
|
ft_list_push_front:
|
||||||
test rdi, rdi
|
test rdi, rdi
|
||||||
jz err
|
jz .err
|
||||||
|
|
||||||
push rdi
|
push rdi
|
||||||
push rsi
|
push rsi
|
||||||
sub rsp, 8
|
|
||||||
mov rdi, 16
|
mov rdi, 16
|
||||||
|
sub rsp, 8
|
||||||
call malloc wrt ..plt
|
call malloc wrt ..plt
|
||||||
add rsp, 8
|
add rsp, 8
|
||||||
pop rsi
|
pop rsi
|
||||||
pop rdi
|
pop rdi
|
||||||
|
|
||||||
test rax, rax ; did malloc return 0
|
test rax, rax ; did malloc return 0
|
||||||
jz err
|
jz .err
|
||||||
|
|
||||||
push rdi
|
push rdi
|
||||||
mov rdi, [rdi]
|
mov rdi, [rdi]
|
||||||
@ -27,5 +27,5 @@ ft_list_push_front:
|
|||||||
|
|
||||||
mov [rdi], rax
|
mov [rdi], rax
|
||||||
|
|
||||||
err:
|
.err:
|
||||||
ret
|
ret
|
||||||
|
@ -57,18 +57,18 @@ call_free_elem:
|
|||||||
|
|
||||||
ft_list_remove_if:
|
ft_list_remove_if:
|
||||||
test rdi, rdi
|
test rdi, rdi
|
||||||
jz end
|
jz .end
|
||||||
test rdx, rdx
|
test rdx, rdx
|
||||||
jz end
|
jz .end
|
||||||
test rcx, rcx
|
test rcx, rcx
|
||||||
jz end
|
jz .end
|
||||||
|
|
||||||
xor r8, r8
|
xor r8, r8
|
||||||
|
|
||||||
mov rax, [rdi]
|
mov rax, [rdi]
|
||||||
loop:
|
.loop:
|
||||||
test rax, rax
|
test rax, rax
|
||||||
jz end
|
jz .end
|
||||||
push rax
|
push rax
|
||||||
|
|
||||||
push rdi
|
push rdi
|
||||||
@ -89,20 +89,20 @@ loop:
|
|||||||
pop rsi
|
pop rsi
|
||||||
pop rdi
|
pop rdi
|
||||||
|
|
||||||
jnz loop_end ;if call to rdx returned zero, don't remove
|
jnz .loop_end ;if call to rdx returned zero, don't remove
|
||||||
|
|
||||||
pop rax
|
pop rax
|
||||||
sub rsp, 8
|
sub rsp, 8
|
||||||
call remove_element
|
call remove_element
|
||||||
add rsp, 8
|
add rsp, 8
|
||||||
jmp loop
|
jmp .loop
|
||||||
|
|
||||||
loop_end:
|
.loop_end:
|
||||||
|
|
||||||
pop rax
|
pop rax
|
||||||
mov r8, rax
|
mov r8, rax
|
||||||
mov rax, [rax + 8]
|
mov rax, [rax + 8]
|
||||||
jmp loop
|
jmp .loop
|
||||||
|
|
||||||
end:
|
.end:
|
||||||
ret
|
ret
|
||||||
|
@ -5,15 +5,14 @@ section .text
|
|||||||
ft_list_size:
|
ft_list_size:
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
|
|
||||||
count_start:
|
.count_start:
|
||||||
test rdi, rdi
|
test rdi, rdi
|
||||||
jz end
|
jz .end
|
||||||
|
|
||||||
mov rdi, [rdi + 8]
|
mov rdi, [rdi + 8]
|
||||||
|
inc rax
|
||||||
|
|
||||||
lea rax, [rax + 1]
|
jmp .count_start
|
||||||
|
|
||||||
jmp count_start
|
.end:
|
||||||
|
|
||||||
end:
|
|
||||||
ret
|
ret
|
||||||
|
@ -19,37 +19,37 @@ list_swap:
|
|||||||
|
|
||||||
ft_list_sort:
|
ft_list_sort:
|
||||||
test rdi, rdi
|
test rdi, rdi
|
||||||
jz err
|
jz .err
|
||||||
test rsi, rsi
|
test rsi, rsi
|
||||||
jz err
|
jz .err
|
||||||
|
|
||||||
mov rdi, [rdi]
|
mov rdi, [rdi]
|
||||||
push rdi
|
push rdi
|
||||||
call ft_list_size
|
call ft_list_size
|
||||||
pop rdi
|
pop rdi
|
||||||
|
|
||||||
loop:
|
.loop:
|
||||||
test rax, rax
|
test rax, rax
|
||||||
jz err
|
jz .err
|
||||||
|
|
||||||
push rax
|
push rax
|
||||||
push rdi
|
push rdi
|
||||||
push rsi
|
push rsi
|
||||||
|
|
||||||
mov rax, rsi ; move cmp to rax
|
mov rax, rsi ; move cmp to rax
|
||||||
jmp loop2
|
jmp .loop2
|
||||||
|
|
||||||
loop2_inc:
|
.loop2_inc:
|
||||||
mov rdi, [rdi + 8]
|
mov rdi, [rdi + 8]
|
||||||
|
|
||||||
loop2:
|
.loop2:
|
||||||
|
|
||||||
test rdi, rdi
|
test rdi, rdi
|
||||||
jz loop2_end
|
jz .loop2_end
|
||||||
|
|
||||||
mov rsi, [rdi + 8]
|
mov rsi, [rdi + 8]
|
||||||
test rsi, rsi
|
test rsi, rsi
|
||||||
jz loop2_end
|
jz .loop2_end
|
||||||
|
|
||||||
push rdi
|
push rdi
|
||||||
mov rdi, [rdi]
|
mov rdi, [rdi]
|
||||||
@ -61,24 +61,23 @@ loop2:
|
|||||||
cmp eax, 0
|
cmp eax, 0
|
||||||
pop rax
|
pop rax
|
||||||
pop rdi
|
pop rdi
|
||||||
jle loop2_inc
|
jle .loop2_inc
|
||||||
|
|
||||||
; then call list_swap
|
; then call list_swap
|
||||||
mov rsi, [rdi + 8]
|
mov rsi, [rdi + 8]
|
||||||
push rax
|
push rax
|
||||||
|
sub rsp, 8
|
||||||
call list_swap
|
call list_swap
|
||||||
|
add rsp, 8
|
||||||
pop rax
|
pop rax
|
||||||
jmp loop2_inc
|
jmp .loop2_inc
|
||||||
|
|
||||||
|
.loop2_end:
|
||||||
|
|
||||||
loop2_end:
|
|
||||||
pop rsi
|
pop rsi
|
||||||
pop rdi
|
pop rdi
|
||||||
pop rax
|
pop rax
|
||||||
lea rax, [rax - 1]
|
lea rax, [rax - 1]
|
||||||
jmp loop
|
jmp .loop
|
||||||
|
|
||||||
|
.err:
|
||||||
err:
|
|
||||||
ret
|
ret
|
||||||
|
@ -7,13 +7,15 @@ ft_read:
|
|||||||
syscall
|
syscall
|
||||||
|
|
||||||
cmp rax, 0 ; if return is < 0, it's an error in negative
|
cmp rax, 0 ; if return is < 0, it's an error in negative
|
||||||
jge success
|
jge .success
|
||||||
|
|
||||||
neg rax ; inv errno, and set errno
|
neg rax ; inv errno, and set errno
|
||||||
mov edi, eax
|
mov edi, eax
|
||||||
|
sub rsp, 8
|
||||||
call __errno_location wrt ..plt
|
call __errno_location wrt ..plt
|
||||||
|
add rsp, 8
|
||||||
mov [rax], edi
|
mov [rax], edi
|
||||||
mov rax, -1
|
mov rax, -1
|
||||||
|
|
||||||
success:
|
.success:
|
||||||
ret
|
ret
|
||||||
|
@ -5,44 +5,43 @@ ft_strcmp:
|
|||||||
xor rax, rax
|
xor rax, rax
|
||||||
|
|
||||||
test rdi, rdi ; test if args are false
|
test rdi, rdi ; test if args are false
|
||||||
jz err
|
jz .err
|
||||||
test rsi, rsi
|
test rsi, rsi
|
||||||
jz err
|
jz .err
|
||||||
loop_start:
|
.loop_start:
|
||||||
mov al, [rdi]
|
mov al, [rdi]
|
||||||
cmp al, [rsi]
|
cmp al, [rsi]
|
||||||
jne compare
|
jne .compare
|
||||||
|
|
||||||
test al, al ; test if rdi is \0
|
test al, al ; test if rdi is \0
|
||||||
jz compare
|
jz .compare
|
||||||
|
|
||||||
mov al, [rsi] ; test if rsi is \0
|
mov al, [rsi] ; test if rsi is \0
|
||||||
test al, al
|
test al, al
|
||||||
jz compare
|
jz .compare
|
||||||
|
|
||||||
lea rsi, [rsi + 1]
|
lea rsi, [rsi + 1]
|
||||||
lea rdi, [rdi + 1]
|
lea rdi, [rdi + 1]
|
||||||
jmp loop_start
|
jmp .loop_start
|
||||||
|
|
||||||
|
|
||||||
compare:
|
.compare:
|
||||||
mov al, [rdi]
|
mov al, [rdi]
|
||||||
cmp al, [rsi]
|
cmp al, [rsi]
|
||||||
jl lower
|
jl .lower
|
||||||
jg greater
|
jg .greater
|
||||||
mov eax, 0
|
mov eax, 0
|
||||||
jmp end
|
jmp .end
|
||||||
|
|
||||||
lower:
|
.lower:
|
||||||
mov eax, -1
|
mov eax, -1
|
||||||
jmp end
|
jmp .end
|
||||||
|
|
||||||
greater:
|
.greater:
|
||||||
mov eax, 1
|
mov eax, 1
|
||||||
jmp end
|
jmp .end
|
||||||
|
|
||||||
err:
|
.err:
|
||||||
mov eax, 0
|
mov eax, 0
|
||||||
|
.end:
|
||||||
end:
|
|
||||||
ret
|
ret
|
||||||
|
@ -3,25 +3,25 @@ section .text
|
|||||||
|
|
||||||
ft_strcpy:
|
ft_strcpy:
|
||||||
test rdi, rdi
|
test rdi, rdi
|
||||||
jz err
|
jz .err
|
||||||
test rsi, rsi
|
test rsi, rsi
|
||||||
jz err
|
jz .err
|
||||||
push rdi
|
push rdi
|
||||||
|
|
||||||
|
|
||||||
loop_start:
|
.loop_start:
|
||||||
mov al, [rsi]
|
mov al, [rsi]
|
||||||
mov [rdi],al
|
mov [rdi],al
|
||||||
lea rdi, [rdi + 1]
|
lea rdi, [rdi + 1]
|
||||||
lea rsi, [rsi + 1]
|
lea rsi, [rsi + 1]
|
||||||
|
|
||||||
test al, al
|
test al, al
|
||||||
jne loop_start
|
jne .loop_start
|
||||||
|
|
||||||
end:
|
.end:
|
||||||
pop rax
|
pop rax
|
||||||
ret
|
ret
|
||||||
|
|
||||||
err:
|
.err:
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
ret
|
ret
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
extern malloc
|
|
||||||
global ft_strdup
|
global ft_strdup
|
||||||
|
|
||||||
|
extern malloc
|
||||||
extern ft_strcpy
|
extern ft_strcpy
|
||||||
extern ft_strlen
|
extern ft_strlen
|
||||||
extern __errno_location
|
extern __errno_location
|
||||||
@ -7,7 +8,7 @@ section .text
|
|||||||
|
|
||||||
ft_strdup:
|
ft_strdup:
|
||||||
test rdi, rdi ; test if arg is NULL
|
test rdi, rdi ; test if arg is NULL
|
||||||
jz err
|
jz .err
|
||||||
|
|
||||||
push rdi ;push to use it for the strcpy
|
push rdi ;push to use it for the strcpy
|
||||||
call ft_strlen
|
call ft_strlen
|
||||||
@ -16,17 +17,21 @@ ft_strdup:
|
|||||||
|
|
||||||
call malloc wrt ..plt
|
call malloc wrt ..plt
|
||||||
test rax, rax
|
test rax, rax
|
||||||
jz malloc_failed
|
jz .malloc_failed
|
||||||
mov rdi, rax
|
mov rdi, rax
|
||||||
pop rsi
|
pop rsi
|
||||||
|
sub rsp, 8
|
||||||
call ft_strcpy
|
call ft_strcpy
|
||||||
|
add rsp, 8
|
||||||
ret
|
ret
|
||||||
|
|
||||||
malloc_failed:
|
.malloc_failed:
|
||||||
|
sub rsp, 8
|
||||||
call __errno_location wrt ..plt
|
call __errno_location wrt ..plt
|
||||||
|
add rsp, 8
|
||||||
mov dword [rax], 12
|
mov dword [rax], 12
|
||||||
pop rax
|
pop rax
|
||||||
|
|
||||||
err:
|
.err:
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
ret
|
ret
|
||||||
|
@ -3,22 +3,22 @@ section .text
|
|||||||
|
|
||||||
ft_strlen:
|
ft_strlen:
|
||||||
test rdi, rdi
|
test rdi, rdi
|
||||||
jz err
|
jz .err
|
||||||
mov rsi, rdi
|
mov rsi, rdi
|
||||||
jmp loop_start
|
jmp .loop_start
|
||||||
|
|
||||||
increase_pointer:
|
.increase_pointer:
|
||||||
lea rsi, [rsi + 1]
|
lea rsi, [rsi + 1]
|
||||||
|
|
||||||
loop_start:
|
.loop_start:
|
||||||
mov al, [rsi]
|
mov al, [rsi]
|
||||||
test al, al
|
test al, al
|
||||||
jnz increase_pointer
|
jnz .increase_pointer
|
||||||
end:
|
.end:
|
||||||
sub rsi, rdi
|
sub rsi, rdi
|
||||||
mov rax, rsi
|
mov rax, rsi
|
||||||
ret
|
ret
|
||||||
|
|
||||||
err:
|
.err:
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
ret
|
ret
|
||||||
|
@ -7,14 +7,16 @@ ft_write:
|
|||||||
syscall
|
syscall
|
||||||
|
|
||||||
cmp rax, 0 ; if return is < 0, it's an error in negative
|
cmp rax, 0 ; if return is < 0, it's an error in negative
|
||||||
jge success
|
jge .success
|
||||||
|
|
||||||
neg rax ; inv errno, and set errno
|
neg rax ; inv errno, and set errno
|
||||||
mov edi, eax
|
mov edi, eax
|
||||||
|
sub rsp, 8
|
||||||
call __errno_location wrt ..plt
|
call __errno_location wrt ..plt
|
||||||
|
add rsp, 8
|
||||||
mov [rax], edi
|
mov [rax], edi
|
||||||
mov rax, -1
|
mov rax, -1
|
||||||
|
|
||||||
|
|
||||||
success:
|
.success:
|
||||||
ret
|
ret
|
||||||
|
Reference in New Issue
Block a user