diff --git a/.gitignore b/.gitignore index 8e736c4..c570914 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.o *.out -*.a \ No newline at end of file +*.a +*.c \ No newline at end of file diff --git a/ft_read.s b/ft_read.s index 7522ee8..4be3f8a 100644 --- a/ft_read.s +++ b/ft_read.s @@ -2,16 +2,16 @@ global ft_read extern __errno_location ft_read: - mov eax, 0x11 + mov eax, 0 syscall test rax, rax js .error ret .error: - mov ebx, eax + mov rdx, rax call __errno_location wrt ..plt - neg ebx - mov dword [rax], ebx + neg rdx + mov [rax], rdx mov rax, -1 ret \ No newline at end of file diff --git a/ft_strcmp.s b/ft_strcmp.s index 93bc1b7..1c367d0 100644 --- a/ft_strcmp.s +++ b/ft_strcmp.s @@ -1,17 +1,27 @@ global ft_strcmp -extern _malloc ft_strcmp: + xor r8, r8 xor rax, rax - xor rbx, rbx .count: - mov rax, [rdi + rbx] - sub rax, [rsi + rbx] - cmp rax, 0 + mov al, BYTE [rdi + r8] + mov dl, BYTE [rsi + r8] + test al, al + jz .end + test dl, dl + jz .end + cmp al, dl jne .end - inc rbx + inc r8 jmp .count .end: + sub al, dl + js .neg + ret + +.neg: + neg al + neg rax ret \ No newline at end of file diff --git a/ft_strcpy.s b/ft_strcpy.s index d2ea71d..72803c9 100644 --- a/ft_strcpy.s +++ b/ft_strcpy.s @@ -1,17 +1,17 @@ global ft_strcpy ft_strcpy: - xor rbx, rbx + xor rdx, rdx .cpy: - cmp byte [rsi + rbx], 0 + cmp byte [rsi + rdx], 0 je .end - mov rax, [rsi + rbx] - mov [rdi + rbx], rax - inc rbx + mov rax, [rsi + rdx] + mov [rdi + rdx], rax + inc rdx jmp .cpy .end: - mov byte [rdi + rbx], 0 + mov byte [rdi + rdx], 0 mov rax, rdi ret diff --git a/ft_strdup.s b/ft_strdup.s index e790de2..9f48a77 100644 --- a/ft_strdup.s +++ b/ft_strdup.s @@ -3,23 +3,21 @@ global ft_strdup extern ft_strlen extern ft_strcpy extern malloc -extern __errno_location ft_strdup: - cmp rdi, 0 - je .end - mov r12, rdi call ft_strlen inc rax - mov rdi, rax + push rdi + mov rdi, rax ; move the result of strlen in rdi call malloc wrt ..plt test rax, rax je .end - mov rdi, rax - mov rsi, r12 + pop r8 + 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 ret .end: - xor rax, rax + pop r8 ret \ No newline at end of file diff --git a/ft_write.s b/ft_write.s index f0cb24a..02992bb 100644 --- a/ft_write.s +++ b/ft_write.s @@ -2,16 +2,16 @@ global ft_write extern __errno_location ft_write: - mov eax, 0x1 + mov eax, 0x1 ; put the syscall number of write syscall test rax, rax js .error ret .error: - mov ebx, eax + mov rdx, rax call __errno_location wrt ..plt - neg ebx - mov dword [rax], ebx + neg rdx + mov [rax], rdx mov rax, -1 ret \ No newline at end of file diff --git a/libasm.a b/libasm.a deleted file mode 100644 index 5ed300b..0000000 Binary files a/libasm.a and /dev/null differ diff --git a/test.c b/test.c index 4c9f780..27769c4 100644 --- a/test.c +++ b/test.c @@ -21,9 +21,11 @@ extern char *ft_strdup(const char *s); void Test_read(char *filename, int len) { char buffer[100] = {0}; + int errnosave; int fd = open(filename, O_RDONLY); printf("READ : %ld | ", read(fd, buffer, len)); + errnosave = errno; printf("%s\n", buffer); close(fd); bzero(buffer, len); @@ -32,6 +34,7 @@ void Test_read(char *filename, int len) printf("READ : %ld | ", ft_read(fd, buffer, len)); printf("%s\n", buffer); close(fd); + printf("READ : %s\n", (errnosave == errno) ? "OK" : "KO"); } void Test_write(char *buffer, int len) @@ -47,7 +50,7 @@ void Test_strdup(char *buffer) newbuffer = strdup(buffer); printf("STRDUP : %s\n", newbuffer); free(newbuffer); - + newbuffer = ft_strdup(buffer); printf("FT_STRDUP : %s\n", newbuffer); free(newbuffer); @@ -90,7 +93,7 @@ int main() Test_strlen("clafete"); - Test_read("mauvaisdavid.txt", 15); + Test_read("mauvaisdavid.txt", 3); Test_write("youpipouic", 10); @@ -98,5 +101,5 @@ int main() Test_strdup("youpilolololol"); - Test_strcmp("vilain", "vilain"); + Test_strcmp(buffer, "123"); } \ No newline at end of file