add other basic functions and test with them

This commit is contained in:
2025-03-29 19:54:58 +01:00
parent 76cf33dd1c
commit b6ad45ab6d
17 changed files with 311 additions and 6 deletions

View File

@ -0,0 +1,19 @@
global ft_read
extern __errno_location
section .text
ft_read:
mov rax, 0 ;syscall number for read, args are already set at the right place
syscall
cmp rax, 0 ; if return is < 0, it's an error in negative
jg success
neg rax ; inv errno, and set errno
mov edi, eax
call __errno_location wrt ..plt
mov [rax], edi
mov rax, -1
success:
ret

View File

@ -0,0 +1,48 @@
section .text
global ft_strcmp
ft_strcmp:
xor rax, rax
test rdi, rdi ; test if args are false
je err
test rsi, rsi
je err
loop_start:
mov al, [rdi]
cmp al, [rsi]
jne compare
test al, al ; test if rdi is \0
je compare
mov al, [rsi] ; test if rsi is \0
test al, al
je compare
lea rsi, [rsi + 1]
lea rdi, [rdi + 1]
jmp loop_start
compare:
mov al, [rdi]
cmp al, [rsi]
jl lower
jg greater
mov eax, 0
jmp end
lower:
mov eax, -1
jmp end
greater:
mov eax, 1
jmp end
err:
mov eax, 0
end:
ret

View File

@ -0,0 +1,29 @@
section .text
global ft_strcpy
ft_strcpy:
test rdi, rdi
je err
test rsi, rsi
je err
push rdi
jmp loop_start
condition_check:
mov al, [rdi]
test al, al
je end
loop_start:
mov rax, [rsi]
mov [rdi], rax
lea rdi, [rdi + 1]
lea rsi, [rsi + 1]
jmp condition_check
end:
pop rax
ret
err:
xor rax, rax
ret

View File

@ -0,0 +1,31 @@
extern malloc
global ft_strdup
extern ft_strcpy
extern ft_strlen
extern __errno_location
section .text
ft_strdup:
test rdi, rdi ; test if arg is NULL
je err
push rdi ;push to use it for the strcpy
call ft_strlen
mov rdi, rax
call malloc wrt ..plt
test rax, rax
je malloc_failed
mov rdi, rax
pop rsi
call ft_strcpy
ret
malloc_failed:
call __errno_location wrt ..plt
mov dword [rax], 12
pop rax
err:
xor rax, rax
ret

View File

@ -0,0 +1,20 @@
global ft_write
extern __errno_location
section .text
ft_write:
mov rax, 1 ;syscall number for write, args are already set at the right place
syscall
cmp rax, 0 ; if return is < 0, it's an error in negative
jg success
neg rax ; inv errno, and set errno
mov edi, eax
call __errno_location wrt ..plt
mov [rax], edi
mov rax, -1
success:
ret