diff --git a/Makefile b/Makefile index eda4420..5b50d53 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ SRC = \ ft_strlen.s \ ft_strcpy.s \ ft_strcmp.s \ + ft_write.s \ + ft_read.s \ + ft_strdup.s \ OBJ = $(SRC:.s=.o) @@ -16,7 +19,7 @@ $(NAME): $(OBJ) ar rcs libasm.a $(OBJ) test: $(NAME) - gcc -Wall -Werror -Wextra -g main.c -L. -lasm -o libasm_unit_tests + gcc -Wall -Werror -Wextra -no-pie -g main.c -lc -L. -lasm -o libasm_unit_tests clean: rm $(OBJ) diff --git a/ft_read.s b/ft_read.s new file mode 100644 index 0000000..d434b3c --- /dev/null +++ b/ft_read.s @@ -0,0 +1,21 @@ +extern __errno_location + +section .text + global ft_read + +ft_read: + mov rax, 0 ; syscall read, other arguments should be good accord to calling convention + syscall + cmp eax, -1 ; linux syscalls errors are always in the range -4095 to -1 + jg .done + cmp eax, -4095 + jl .done + mov rbx, rax ; save return value of syscall + call __errno_location ; put address of errno in rax + neg rbx ; errno should be positive + mov [rax], rbx ; set errno + mov rax, -1 ; set return value of ft_read to -1 + ret + +.done: + ret diff --git a/ft_strcmp.s b/ft_strcmp.s index 99f718f..2e7fc6a 100644 --- a/ft_strcmp.s +++ b/ft_strcmp.s @@ -12,8 +12,8 @@ ft_strcmp: inc rdx jmp .loop .done: - movsx rax, byte [rdi + rdx] - movsx rbx, byte [rsi + rdx] + mov al, byte [rdi + rdx] + mov bl, byte [rsi + rdx] sub rax, rbx ret diff --git a/ft_strcpy.s b/ft_strcpy.s index ce98d48..0c75a75 100644 --- a/ft_strcpy.s +++ b/ft_strcpy.s @@ -10,7 +10,6 @@ ft_strcpy: inc rax jmp .loop .done: - inc rax mov byte [rdi + rax], 0 mov rax, rdi ret diff --git a/ft_strdup.s b/ft_strdup.s new file mode 100644 index 0000000..68ac018 --- /dev/null +++ b/ft_strdup.s @@ -0,0 +1,25 @@ +global ft_strdup + +extern my_test +extern malloc +extern ft_strlen +extern ft_strcpy + +ft_strdup: + enter 16, 0 + push rdi + enter 16, 0 + call ft_strlen + mov rdi, rax + inc rdi + call malloc + cmp rax, 0 + jz .done + leave + mov rdi, rax + pop rsi + call ft_strcpy + +.done: + leave + ret diff --git a/ft_write.s b/ft_write.s new file mode 100644 index 0000000..0ef58c4 --- /dev/null +++ b/ft_write.s @@ -0,0 +1,21 @@ +extern __errno_location + +section .text + global ft_write + +ft_write: + mov rax, 1 ; syscall write, other arguments should be good accord to calling convention + syscall + cmp eax, -1 ; linux syscalls errors are always in the range -4095 to -1 + jg .done + cmp eax, -4095 + jl .done + mov rbx, rax ; save return value of syscall + call __errno_location ; put address of errno in rax + neg rbx ; errno should be positive + mov [rax], rbx ; set errno + mov rax, -1 ; set return value of ft_write to -1 + ret + +.done: + ret diff --git a/libasm.h b/libasm.h index c4bf3cc..4f1dcb6 100644 --- a/libasm.h +++ b/libasm.h @@ -2,9 +2,14 @@ # define _LIBASM_H #include +#include +#include size_t ft_strlen(const char *); char *ft_strcpy(char *, const char *); int ft_strcmp(const char *, const char *); +ssize_t ft_write(int, const void *, size_t); +ssize_t ft_read(int, void *, size_t); +char *ft_strdup(const char *); #endif diff --git a/main.c b/main.c index ee9eaf4..0f61ff4 100644 --- a/main.c +++ b/main.c @@ -1,26 +1,53 @@ #include "libasm.h" +#include #include +#include + +void my_test(void) { + write(1, "Hello, world!\n", 14); + printf("Hello, world!\n"); +} + +void *my_malloc(size_t size) { + printf("CALLLLLLED\n"); + fflush(stdout); + printf("called with size %ld\n", size); + fflush(stdout); + printf("222323\n"); + fflush(stdout); + return malloc(size); +} + int main() { - char my_string[37] = "c'est moi la chaine de 36 caracteres"; - printf("%ld\n", ft_strlen(my_string)); +/* char *str1 = (char *)malloc(88 * sizeof(char)); + ft_strcpy(str1, "Salut moi une chaine de caractere trop longue\n"); + printf("%ld\n", ft_write(1, str1, 46)); + printf("%d\n", (int)str1[0]); + free(str1); - char *str; - str = (char *)malloc(150 * sizeof(char)); - str = "Salut c'est moi la zone mdr\0"; - char *dst; - dst = (char *)malloc(50 * sizeof(char)); - char *test = ft_strcpy(dst, str); - printf("%s\n", str); - printf("%s\n", dst); - printf("%s\n", test); - printf("should be 0: %d\n", ft_strcmp(str, dst)); - printf("should be 0 and not segv: %d\n", ft_strcmp(dst, test)); - printf("should be 16 %d\n", ft_strcmp(my_string, str)); - printf("should be -16 %d\n", ft_strcmp(str, my_string)); - dst = "Salut c'est moi la diff mdr\0"; - printf("should be 22: %d\n", ft_strcmp(str, dst)); - dst = "Salut\0 c'est moi la diff mdr\0"; - printf("should be 32: %d\n", ft_strcmp(str, dst)); + char *str2 = (char *)malloc(88 * sizeof(char)); + strcpy(str2, "Salut moi une chaine de caractere trop longue\n"); + printf("%ld\n", write(1, str2, 46)); + printf("%d\n", (int)str2[0]); + free(str2); + + ssize_t write_ret = write(4, "test\n", 5); + if (write_ret == -1) { + fprintf(stderr, "error = %s\n", strerror(errno)); + perror("write"); + } + errno = 42; + write_ret = ft_write(4, "test\n", 5); + printf("ft_write ret %ld\n", write_ret); + if (write_ret == -1) { + fprintf(stderr, "error = %d\n", errno); + fprintf(stderr, "error = %s\n", strerror(errno)); + perror("ft_write"); + }*/ + char *str = ft_strdup("Salut c'est moi libasm\n"); + ft_write(1, "Hello, world!\n", 14); + printf("len %ld\n", ft_strlen(str)); + ft_write(1, str, ft_strlen(str)); return 0; }