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> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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
.cpy:
cmp byte [rsi + rdx], 0
cmp byte [rsi + rdx], 0 ; if src is empty
je .end
mov rax, [rsi + rdx]
mov [rdi + rdx], rax
inc rdx
mov rax, [rsi + rdx] ; move src[rdx] to rax
mov word [rdi + rdx], ax ; dest[rdx] = src[rdx]
inc rdx ; rdx++
jmp .cpy
.end:

View File

@@ -6,17 +6,17 @@ extern malloc
ft_strdup:
call ft_strlen
inc rax
push rdi
inc rax ; we add one to the result of strlen for the \0
push rdi ; we put rdi in r10 for later use
mov rdi, rax ; move the result of strlen in rdi
call malloc wrt ..plt
test rax, rax
call malloc wrt ..plt ;
test rax, rax ; test if malloc failled
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)
call ft_strcpy
ret
.end:
pop r8
pop rsi
ret

View File

@@ -2,16 +2,25 @@ global ft_write
extern __errno_location
ft_write:
cmp rsi, 0
je .bad_buff
mov eax, 0x1 ; put the syscall number of write
syscall
test rax, rax
test rax, rax ; test if the syscall failed
js .error
ret
.error:
mov rdx, rax
.bad_buff:
mov rdx, 22
call __errno_location wrt ..plt
neg rdx
mov [rax], rdx
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