diff --git a/libftasm/Makefile b/libftasm/Makefile index 2c063883..cf3a6087 100644 --- a/libftasm/Makefile +++ b/libftasm/Makefile @@ -105,13 +105,6 @@ test: $(NAME) test.c @gcc test.c -I $(INC_DIR) -L. -lfts -o test @printf "\r\033[38;5;117m✓ MAKE test\033[0m\033[K\n" -debug: $(NAME) debug.c - @gcc debug.c -I $(INC_DIR) -L. -lfts -o debug - @printf "\r\033[38;5;117m✓ MAKE debug\033[0m\033[K\n" - -run-dbg: debug - lldb ./debug - .PHONY : fclean clean re run-gdb -include $(OBJS:.o=.d) diff --git a/libftasm/debug.c b/libftasm/debug.c deleted file mode 100644 index 5a6741e7..00000000 --- a/libftasm/debug.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "libft.h" - -int main(void) -{ - ft_puts("h2g2"); - return (0); -} diff --git a/libftasm/srcs/ft_cat.s b/libftasm/srcs/ft_cat.s index d174b885..1e6416ca 100644 --- a/libftasm/srcs/ft_cat.s +++ b/libftasm/srcs/ft_cat.s @@ -1,10 +1,8 @@ global _ft_cat global ft_cat -extern ft_putchar - %define STDOUT 1 -%define BUFF_SIZE 10 +%define BUFF_SIZE 1024 section .bss buf: resb BUFF_SIZE diff --git a/libftasm/srcs/ft_putchar.s b/libftasm/srcs/ft_putchar.s index 3a005c66..e29d6655 100644 --- a/libftasm/srcs/ft_putchar.s +++ b/libftasm/srcs/ft_putchar.s @@ -27,6 +27,7 @@ ft_putchar: ; success case then return c pop rax + movzx rax, al ; cast to char ret err: diff --git a/libftasm/srcs/ft_strdup.s b/libftasm/srcs/ft_strdup.s index 7953ed33..de78e1fa 100644 --- a/libftasm/srcs/ft_strdup.s +++ b/libftasm/srcs/ft_strdup.s @@ -12,19 +12,24 @@ ft_strdup: mov rax, 0 cmp rdi, 0 je end + push rdi call _ft_strlen inc rax push rax mov rdi, rax + + sub rsp, 8 ; align stack to 16 bytes, x86_64 or mach don't know call _malloc + add rsp, 8 + + pop rcx + pop rsi cmp rax, 0 je end mov rdi, rax - pop rcx - pop rsi - cld + cld ; clear the directon flag rep movsb end: ret diff --git a/libftasm/srcs/ft_strlen.s b/libftasm/srcs/ft_strlen.s index 2dbe5bb4..fc41dbfe 100644 --- a/libftasm/srcs/ft_strlen.s +++ b/libftasm/srcs/ft_strlen.s @@ -3,14 +3,16 @@ global ft_strlen _ft_strlen: ft_strlen: - mov rax, 0 - cmp rdi, 0 - je end -loop: - cmp byte [rdi], 0 - je end - inc rax - inc rdi - jmp loop + mov rax, 0 + cmp rdi, 0 + je end +t + mov rcx, -1 + cld + repnz scasb + + not rcx + lea rax, [rcx - 1] + end: ret diff --git a/libftasm/test.c b/libftasm/test.c index cd2dc492..d32a725f 100644 --- a/libftasm/test.c +++ b/libftasm/test.c @@ -6,11 +6,6 @@ int ft_putstr(const char *str) return (write(1, str, strlen(str))); } -/* int ft_putchar(const char c) */ -/* { */ -/* return (write(1, &c, 1)); */ -/* } */ - int ft_putnstr(const char *str, size_t n) { return (write(1, str, n)); @@ -400,26 +395,26 @@ int test_strdup() } str = ft_strdup("Coucou"); str_cmp = strdup("Coucou"); - /* len = strlen(str); */ - /* len_cmp = strlen(str_cmp); */ - /* if (len != len_cmp) */ - /* { */ - /* printf("FAILED: len is %d vs %d\n", len, len_cmp); */ - /* return (1); */ - /* } */ - /* i = -1; */ - /* while (++i < len) */ - /* { */ - /* if (str[i] != str_cmp[i]) */ - /* { */ - /* ft_putnstr(str, len); */ - /* ft_putnstr(str_cmp, len); */ - /* printf("FAILED: %c vs %c\n", str[i], str_cmp[i]); */ - /* return (1); */ - /* } */ - /* } */ - /* free(str); */ - /* free(str_cmp); */ + len = strlen(str); + len_cmp = strlen(str_cmp); + if (len != len_cmp) + { + printf("FAILED: len is %d vs %d\n", len, len_cmp); + return (1); + } + i = -1; + while (++i < len) + { + if (str[i] != str_cmp[i]) + { + ft_putnstr(str, len); + ft_putnstr(str_cmp, len); + printf("FAILED: %c vs %c\n", str[i], str_cmp[i]); + return (1); + } + } + free(str); + free(str_cmp); return (0); } @@ -427,13 +422,15 @@ int test_strdup() int test_cat(char ** argv) { ft_putstr(__func__); ft_putstr(":\n"); + /* ft_putstr("Wait for a user input:\n"); */ /* ft_cat(0); */ - ft_putstr("\ntest.c:\n"); - ft_cat(open(__FILE__, O_RDONLY)); - ft_putstr("\ntest binary:\n"); - ft_cat(open(argv[0], O_RDONLY)); - ft_cat(-42); + + /* ft_putstr("\ntest.c:\n"); */ + /* ft_cat(open(__FILE__, O_RDONLY)); */ + + /* ft_putstr("\ntest binary:\n"); */ + /* ft_cat(open("test", O_RDONLY)); */ return (0); } @@ -510,16 +507,15 @@ int main(int argc, char **argv) test_memcpy() || test_strdup()) return (1); - exit(1); ft_putstr("\nPART BONUS:\n_______\n"); if (test_isupper() || test_islower() || - test_isupper()) - /* test_putchar()) */ + test_isupper() || + test_putchar()) return (1); ft_putstr("\nPART 3:\n_______\n"); if (test_cat(argv)) return (1); - puts("\033c\n\033[38;5;117mALL PASSED:\n___________\n\033[0m"); + puts("\033c\n\033[38;5;117mALL PASSED\n___________\n\033[0m"); return (0); }