From b2873fd6f67ca0c816a35dc52e912907470ae5ae Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 23 Nov 2017 17:55:55 +0100 Subject: [PATCH] last commit ? --- libftasm/Makefile | 36 +++---- libftasm/libft.h | 6 +- libftasm/{test.c => main.c} | 198 +++++++++++++++++++++++++++++++----- libftasm/srcs/ft_cat.s | 13 ++- libftasm/srcs/ft_max.s | 13 +++ libftasm/srcs/ft_min.s | 13 +++ libftasm/srcs/ft_strlen.s | 2 +- 7 files changed, 231 insertions(+), 50 deletions(-) rename libftasm/{test.c => main.c} (73%) create mode 100644 libftasm/srcs/ft_max.s create mode 100644 libftasm/srcs/ft_min.s diff --git a/libftasm/Makefile b/libftasm/Makefile index cf3a6087..e0d70040 100644 --- a/libftasm/Makefile +++ b/libftasm/Makefile @@ -39,26 +39,25 @@ OBJ_DIR = objs/ SRC_BASE = \ ft_bzero.s\ -ft_strcat.s\ -ft_isalpha.s\ -ft_isdigit.s\ -ft_isalnum.s\ -ft_isascii.s\ -ft_isprint.s\ -ft_toupper.s\ -ft_tolower.s\ -ft_puts.s\ -\ -ft_strlen.s\ -ft_memset.s\ -ft_memcpy.s\ -ft_strdup.s\ -\ ft_cat.s\ -\ +ft_isalnum.s\ +ft_isalpha.s\ +ft_isascii.s\ +ft_isdigit.s\ ft_islower.s\ +ft_isprint.s\ ft_isupper.s\ +ft_max.s\ +ft_memcpy.s\ +ft_memset.s\ +ft_min.s\ ft_putchar.s\ +ft_puts.s\ +ft_strcat.s\ +ft_strdup.s\ +ft_strlen.s\ +ft_tolower.s\ +ft_toupper.s SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.s=.o)) @@ -101,8 +100,9 @@ fclean : clean re : fclean all -test: $(NAME) test.c - @gcc test.c -I $(INC_DIR) -L. -lfts -o test + +test: $(NAME) main.c + gcc -Wall -Wextra -Werror main.c libfts.a @printf "\r\033[38;5;117m✓ MAKE test\033[0m\033[K\n" .PHONY : fclean clean re run-gdb diff --git a/libftasm/libft.h b/libftasm/libft.h index 4ecd06d6..d4187f68 100644 --- a/libftasm/libft.h +++ b/libftasm/libft.h @@ -19,6 +19,7 @@ int ft_isprint(int i); int ft_toupper(int i); int ft_tolower(int i); int ft_puts(char *s); + /* ** PART_2 */ @@ -26,14 +27,17 @@ int ft_strlen(char *s); void *ft_memset(void *b, int c, size_t len); void *ft_memcpy(void *dest, const void *src, size_t n); char *ft_strdup(const char *s1); + /* ** PART_3 */ void ft_cat(int fd); + /* ** PART_BONUS */ int ft_isupper(int i); int ft_islower(int i); int ft_putchar(int i); - +int ft_min(int a, int b); +int ft_max(int a, int b); diff --git a/libftasm/test.c b/libftasm/main.c similarity index 73% rename from libftasm/test.c rename to libftasm/main.c index d32a725f..4bc8b771 100644 --- a/libftasm/test.c +++ b/libftasm/main.c @@ -1,5 +1,6 @@ #include "libft.h" #include +#include int ft_putstr(const char *str) { @@ -219,7 +220,7 @@ int test_puts() } #endif #ifdef __APPLE__ -// puts(NULL) segfaults on linux +// puts(NULL) segfaults on linux because the compiler lets you shoot yourself printf("Original:|"); ret = puts(NULL);printf("|\n"); printf("Notre :|"); ret_cmp = ft_puts(NULL);printf("|\n"); if (ret != ret_cmp) @@ -419,18 +420,15 @@ int test_strdup() return (0); } -int test_cat(char ** argv) +int test_cat(char **av) { 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("test", O_RDONLY)); */ + (void)av; + ft_cat(0); + ft_cat(open(__FILE__, O_RDONLY)); + ft_cat(open(av[0], O_RDONLY)); + ft_cat(-42); return (0); } @@ -479,43 +477,193 @@ int test_putchar() return(1); } } - + ft_putstr(":\n"); return (0); } +int test_max() +{ + ft_putstr(__func__); ft_putstr(":\n"); + unsigned int a, b, ret, ret_cmp; + int ans = 0; + a = 5; + b = 6; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = 6; + b = 5; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -10; + b = -12; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -12; + b = -10; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -10; + b = 12; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -12; + b = 10; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = 0; + b = 10; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -12; + b = 0; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -2147483648; + b = 2147483647; + if ((ret = ft_max(a,b)) != (ret_cmp = fmax(a,b))) + { + dprintf(2, "FAILED max(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + + return (ans); +} + +int test_min() +{ + ft_putstr(__func__); ft_putstr(":\n"); + unsigned int a, b, ret, ret_cmp; + int ans = 0; + a = 5; + b = 6; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = 6; + b = 5; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -10; + b = -12; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -12; + b = -10; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -10; + b = 12; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -12; + b = 10; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = 0; + b = 10; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -12; + b = 0; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + a = -2147483648; + b = 2147483647; + if ((ret = ft_min(a,b)) != (ret_cmp = fmin(a,b))) + { + dprintf(2, "FAILED min(%d, %d) => %d vs %d\n",a,b,ret,ret_cmp); + ++ans; + } + + return (ans); +} + int main(int argc, char **argv) { (void)argc; - (void)argv; setbuf(stdout, NULL); ft_putstr("\nPART 1:\n_______\n"); - if (test_bzero() || - test_isdigit() || - test_isalpha() || + if ( + test_bzero() || test_strcat() || - test_isascii() || + test_isalpha() || + test_isdigit() || test_isalnum() || + test_isascii() || test_isprint() || test_toupper() || test_tolower() || - test_puts()) + test_puts() + ) return (1); ft_putstr("PART 2:\n_______\n"); - if (test_strlen() || + if ( + test_strlen() || test_memset() || test_memcpy() || - test_strdup()) - return (1); - ft_putstr("\nPART BONUS:\n_______\n"); - if (test_isupper() || - test_islower() || - test_isupper() || - test_putchar()) + test_strdup() + ) return (1); ft_putstr("\nPART 3:\n_______\n"); if (test_cat(argv)) return (1); + ft_putstr("\nPART BONUS:\n_______\n"); + if ( + test_isupper() || + test_islower() || + test_putchar() || + test_min() || + test_max() + ) + return (1); puts("\033c\n\033[38;5;117mALL PASSED\n___________\n\033[0m"); return (0); } diff --git a/libftasm/srcs/ft_cat.s b/libftasm/srcs/ft_cat.s index 1e6416ca..c145c0d2 100644 --- a/libftasm/srcs/ft_cat.s +++ b/libftasm/srcs/ft_cat.s @@ -8,14 +8,17 @@ section .bss buf: resb BUFF_SIZE section .text - -_ft_cat: ; void ft_cat(int fd) +; void ft_cat(int fd) +_ft_cat: ft_cat: - push rdi + mov r8, rdi + cmp rdi, 0 + jl end lea rsi, [rel buf] mov rdx, BUFF_SIZE mov rax, READ ; int read(int fd, void *buf, size_t count) syscall + jc end cmp rax, 0 jle end @@ -23,11 +26,11 @@ ft_cat: mov rdx, rax mov rax, WRITE ; int write(int fd, const void *buf, size_t count) syscall + jc end cmp rax, 0 jl end - pop rdi + mov rdi, r8 jmp ft_cat end: - pop rax ret diff --git a/libftasm/srcs/ft_max.s b/libftasm/srcs/ft_max.s new file mode 100644 index 00000000..83f97bce --- /dev/null +++ b/libftasm/srcs/ft_max.s @@ -0,0 +1,13 @@ +global _ft_max +global ft_max + +section .text +; int ft_max(int a, int b) +_ft_max: +ft_max: + mov rax, rdi + cmp rdi, rsi + jge end + mov rax, rsi +end: + ret diff --git a/libftasm/srcs/ft_min.s b/libftasm/srcs/ft_min.s new file mode 100644 index 00000000..016eb665 --- /dev/null +++ b/libftasm/srcs/ft_min.s @@ -0,0 +1,13 @@ +global _ft_min +global ft_min + +section .text +; int ft_min(int a, int b) +_ft_min: +ft_min: + mov rax, rdi + cmp rdi, rsi + jl end + mov rax, rsi +end: + ret diff --git a/libftasm/srcs/ft_strlen.s b/libftasm/srcs/ft_strlen.s index fc41dbfe..0d640ca2 100644 --- a/libftasm/srcs/ft_strlen.s +++ b/libftasm/srcs/ft_strlen.s @@ -6,7 +6,7 @@ ft_strlen: mov rax, 0 cmp rdi, 0 je end -t + mov rcx, -1 cld repnz scasb