This commit is contained in:
2024-03-27 23:02:24 +01:00
parent f9396fc10e
commit b7a3b7140b
4 changed files with 25 additions and 16 deletions

View File

@@ -6,7 +6,7 @@
# By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ # # By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/03/15 14:16:21 by vvaas #+# #+# # # Created: 2024/03/15 14:16:21 by vvaas #+# #+# #
# Updated: 2024/03/15 16:52:30 by vvaas ### ########.fr # # Updated: 2024/03/27 17:45:37 by vvaas ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#

View File

@@ -4,11 +4,11 @@ ft_strcpy:
xor rdx, rdx xor rdx, rdx
.cpy: .cpy:
cmp byte [rsi + rdx], 0 cmp byte [rsi + rdx], 0 ; if src is empty
je .end je .end
mov rax, [rsi + rdx] mov rax, [rsi + rdx] ; move src[rdx] to rax
mov [rdi + rdx], rax mov word [rdi + rdx], ax ; dest[rdx] = src[rdx]
inc rdx inc rdx ; rdx++
jmp .cpy jmp .cpy
.end: .end:

View File

@@ -5,18 +5,18 @@ extern ft_strcpy
extern malloc extern malloc
ft_strdup: ft_strdup:
call ft_strlen call ft_strlen
inc rax inc rax ; we add one to the result of strlen for the \0
push rdi push rdi ; we put rdi in r10 for later use
mov rdi, rax ; move the result of strlen in rdi mov rdi, rax ; move the result of strlen in rdi
call malloc wrt ..plt call malloc wrt ..plt ;
test rax, rax test rax, rax ; test if malloc failled
je .end je .end
pop rsi pop rsi ; we take the value we saved earlier
mov rdi, rax ; move the result of malloc into rdi (override strlen result) mov rdi, rax ; move the result of malloc into rdi (override strlen result)
call ft_strcpy call ft_strcpy
ret ret
.end: .end:
pop r8 pop rsi
ret ret

View File

@@ -2,16 +2,25 @@ global ft_write
extern __errno_location extern __errno_location
ft_write: ft_write:
cmp rsi, 0
je .bad_buff
mov eax, 0x1 ; put the syscall number of write mov eax, 0x1 ; put the syscall number of write
syscall syscall
test rax, rax test rax, rax ; test if the syscall failed
js .error js .error
ret ret
.error: .bad_buff:
mov rdx, rax mov rdx, 22
call __errno_location wrt ..plt call __errno_location wrt ..plt
neg rdx
mov [rax], rdx mov [rax], rdx
mov rax, -1 mov rax, -1
ret
.error:
mov rdx, rax ; move the syscall return value
call __errno_location wrt ..plt
neg rdx ; we neg the syscall return value to put it in errno
mov [rax], rdx ; *errno_location = value
mov rax, -1 ; we return -1
ret ret