Fixed more segfaults, final commit ?

This commit is contained in:
2024-03-18 13:40:09 +01:00
parent ddabaeba7f
commit 56d82c386f
8 changed files with 44 additions and 32 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
*.o *.o
*.out *.out
*.a *.a
*.c

View File

@@ -2,16 +2,16 @@ global ft_read
extern __errno_location extern __errno_location
ft_read: ft_read:
mov eax, 0x11 mov eax, 0
syscall syscall
test rax, rax test rax, rax
js .error js .error
ret ret
.error: .error:
mov ebx, eax mov rdx, rax
call __errno_location wrt ..plt call __errno_location wrt ..plt
neg ebx neg rdx
mov dword [rax], ebx mov [rax], rdx
mov rax, -1 mov rax, -1
ret ret

View File

@@ -1,17 +1,27 @@
global ft_strcmp global ft_strcmp
extern _malloc
ft_strcmp: ft_strcmp:
xor r8, r8
xor rax, rax xor rax, rax
xor rbx, rbx
.count: .count:
mov rax, [rdi + rbx] mov al, BYTE [rdi + r8]
sub rax, [rsi + rbx] mov dl, BYTE [rsi + r8]
cmp rax, 0 test al, al
jz .end
test dl, dl
jz .end
cmp al, dl
jne .end jne .end
inc rbx inc r8
jmp .count jmp .count
.end: .end:
sub al, dl
js .neg
ret
.neg:
neg al
neg rax
ret ret

View File

@@ -1,17 +1,17 @@
global ft_strcpy global ft_strcpy
ft_strcpy: ft_strcpy:
xor rbx, rbx xor rdx, rdx
.cpy: .cpy:
cmp byte [rsi + rbx], 0 cmp byte [rsi + rdx], 0
je .end je .end
mov rax, [rsi + rbx] mov rax, [rsi + rdx]
mov [rdi + rbx], rax mov [rdi + rdx], rax
inc rbx inc rdx
jmp .cpy jmp .cpy
.end: .end:
mov byte [rdi + rbx], 0 mov byte [rdi + rdx], 0
mov rax, rdi mov rax, rdi
ret ret

View File

@@ -3,23 +3,21 @@ global ft_strdup
extern ft_strlen extern ft_strlen
extern ft_strcpy extern ft_strcpy
extern malloc extern malloc
extern __errno_location
ft_strdup: ft_strdup:
cmp rdi, 0
je .end
mov r12, rdi
call ft_strlen call ft_strlen
inc rax inc rax
mov rdi, rax push 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
je .end je .end
mov rdi, rax pop r8
mov rsi, r12 mov rdi, rax ; move the result of malloc into rdi (override strlen result)
mov rsi, r8 ; move the argument of strdup in rsi
call ft_strcpy call ft_strcpy
ret ret
.end: .end:
xor rax, rax pop r8
ret ret

View File

@@ -2,16 +2,16 @@ global ft_write
extern __errno_location extern __errno_location
ft_write: ft_write:
mov eax, 0x1 mov eax, 0x1 ; put the syscall number of write
syscall syscall
test rax, rax test rax, rax
js .error js .error
ret ret
.error: .error:
mov ebx, eax mov rdx, rax
call __errno_location wrt ..plt call __errno_location wrt ..plt
neg ebx neg rdx
mov dword [rax], ebx mov [rax], rdx
mov rax, -1 mov rax, -1
ret ret

BIN
libasm.a

Binary file not shown.

7
test.c
View File

@@ -21,9 +21,11 @@ extern char *ft_strdup(const char *s);
void Test_read(char *filename, int len) void Test_read(char *filename, int len)
{ {
char buffer[100] = {0}; char buffer[100] = {0};
int errnosave;
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
printf("READ : %ld | ", read(fd, buffer, len)); printf("READ : %ld | ", read(fd, buffer, len));
errnosave = errno;
printf("%s\n", buffer); printf("%s\n", buffer);
close(fd); close(fd);
bzero(buffer, len); bzero(buffer, len);
@@ -32,6 +34,7 @@ void Test_read(char *filename, int len)
printf("READ : %ld | ", ft_read(fd, buffer, len)); printf("READ : %ld | ", ft_read(fd, buffer, len));
printf("%s\n", buffer); printf("%s\n", buffer);
close(fd); close(fd);
printf("READ : %s\n", (errnosave == errno) ? "OK" : "KO");
} }
void Test_write(char *buffer, int len) void Test_write(char *buffer, int len)
@@ -90,7 +93,7 @@ int main()
Test_strlen("clafete"); Test_strlen("clafete");
Test_read("mauvaisdavid.txt", 15); Test_read("mauvaisdavid.txt", 3);
Test_write("youpipouic", 10); Test_write("youpipouic", 10);
@@ -98,5 +101,5 @@ int main()
Test_strdup("youpilolololol"); Test_strdup("youpilolololol");
Test_strcmp("vilain", "vilain"); Test_strcmp(buffer, "123");
} }