Compare commits
3 Commits
49501469c8
...
master
Author | SHA1 | Date | |
---|---|---|---|
1899b01d0a | |||
caa21a3fb0 | |||
2a65c14032 |
4
Makefile
4
Makefile
@ -27,11 +27,11 @@ FLAGS = -felf64
|
|||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
test: test.c bonus
|
test: test.c bonus
|
||||||
clang -z noexecstack test.c -L. -lasm_bonus -o test -g
|
clang -z noexecstack test.c -L. -lasm -o test -g
|
||||||
./test || echo -e "\033[31m tests exited with an error code"
|
./test || echo -e "\033[31m tests exited with an error code"
|
||||||
|
|
||||||
bonus: $(OBJS_DIR) $(OBJS) $(OBJS_BONUS)
|
bonus: $(OBJS_DIR) $(OBJS) $(OBJS_BONUS)
|
||||||
ar rcs $@ $(OBJS) $(OBJS_BONUS)
|
ar rcs $(NAME) $(OBJS) $(OBJS_BONUS)
|
||||||
|
|
||||||
$(NAME): $(OBJS_DIR) $(OBJS)
|
$(NAME): $(OBJS_DIR) $(OBJS)
|
||||||
ar rcs $@ $(OBJS)
|
ar rcs $@ $(OBJS)
|
||||||
|
@ -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
|
||||||
@ -40,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
|
||||||
@ -55,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
|
||||||
|
|
||||||
@ -69,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];
|
||||||
@ -97,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
|
||||||
|
|
||||||
@ -113,53 +119,56 @@ 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
|
||||||
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
|
||||||
xor rax, rax
|
|
||||||
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 rdi
|
push rdi
|
||||||
@ -169,30 +178,30 @@ 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
|
||||||
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
|
||||||
|
|
||||||
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
|
||||||
imul rbx
|
imul rbx
|
||||||
@ -201,14 +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:
|
||||||
ret
|
ret
|
||||||
|
@ -5,17 +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
|
||||||
mov rdi, 16
|
mov rdi, 16
|
||||||
|
sub rsp, 8
|
||||||
call malloc wrt ..plt
|
call malloc wrt ..plt
|
||||||
|
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]
|
||||||
@ -25,5 +27,5 @@ ft_list_push_front:
|
|||||||
|
|
||||||
mov [rdi], rax
|
mov [rdi], rax
|
||||||
|
|
||||||
err:
|
.err:
|
||||||
ret
|
ret
|
||||||
|
@ -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
|
||||||
@ -55,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
|
||||||
@ -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,19 +89,20 @@ 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
|
||||||
jmp loop
|
add rsp, 8
|
||||||
|
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
|
||||||
|
6
test.c
6
test.c
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */
|
/* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */
|
||||||
/* Updated: 2025/04/12 19:30:05 by tomoron ### ########.fr */
|
/* Updated: 2025/08/07 13:40:55 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -89,6 +89,7 @@ int test_write(void)
|
|||||||
int fail = 0;
|
int fail = 0;
|
||||||
|
|
||||||
TEST("writing hello and test return value", ft_write(1, "hello ", 6) == 6);
|
TEST("writing hello and test return value", ft_write(1, "hello ", 6) == 6);
|
||||||
|
TEST("writing nothing", ft_write(1, "hello", 0) == 0);
|
||||||
errno = 0;
|
errno = 0;
|
||||||
TEST("failed write return value", ft_write(-1, "", 0) == (size_t)-1);
|
TEST("failed write return value", ft_write(-1, "", 0) == (size_t)-1);
|
||||||
TEST("failed write errno test", errno == 9);
|
TEST("failed write errno test", errno == 9);
|
||||||
@ -111,6 +112,7 @@ int test_read(void)
|
|||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
fd = open("test_file.tmp", O_RDONLY);
|
fd = open("test_file.tmp", O_RDONLY);
|
||||||
|
TEST("read 0 char", ft_read(fd, buf, 0) == 0);
|
||||||
TEST("read 1 char", ft_read(fd, buf, 1) == 1);
|
TEST("read 1 char", ft_read(fd, buf, 1) == 1);
|
||||||
TEST("check read char", memcmp(buf, "p", 1) == 0);
|
TEST("check read char", memcmp(buf, "p", 1) == 0);
|
||||||
TEST("read multiple char", ft_read(fd, buf, 5) == 5);
|
TEST("read multiple char", ft_read(fd, buf, 5) == 5);
|
||||||
@ -249,7 +251,7 @@ int test_list_sort()
|
|||||||
|
|
||||||
int remove_if_compare(void *data, int *nb)
|
int remove_if_compare(void *data, int *nb)
|
||||||
{
|
{
|
||||||
return((long)data == *nb);
|
return((long)data != *nb);
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_list_remove_if()
|
int test_list_remove_if()
|
||||||
|
Reference in New Issue
Block a user