From 4ef28d1ce9ae2a44ab572a555fd5a0432afead05 Mon Sep 17 00:00:00 2001 From: Xamora Date: Sun, 20 Sep 2026 01:37:16 +0200 Subject: [PATCH] Finish mandatory --- Makefile | 4 ++-- main.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ main.h | 2 ++ src/calloc.s | 30 ++++++++++++++++++++++++ src/defines.inc | 3 +++ src/ft_read.s | 12 ++++++++++ src/ft_strcmp.s | 10 ++++---- src/ft_strcpy.s | 4 ++-- src/ft_strdup.s | 15 ++++++++++++ src/ft_strlen.s | 11 ++------- src/ft_write.s | 14 ++++++++++- 11 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 src/calloc.s diff --git a/Makefile b/Makefile index 99a8bbf..7b93a43 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC := gcc AS := nasm ASFLAGS := -g -f elf64 -I src/ -CFLAGS := -g -Wall -Wextra -Wno-override-init -std=c2x +CFLAGS := -g -Wall -Wextra -Wno-override-init -std=c2x -fPIC -fPIE MAIN := $(wildcard *.c) SRC := $(wildcard src/*.s) OBJ_DIR := build @@ -12,7 +12,7 @@ NAME := main LIB_NAME:= asm LD := $(CC) -LDFLAGS := -L$(OBJ_DIR) -l$(LIB_NAME) +LDFLAGS := -L$(OBJ_DIR) -l$(LIB_NAME) -fPIC -fPIE all: $(NAME) diff --git a/main.c b/main.c index 4df98e1..78fe5ac 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,8 @@ main(void) { test_read(); printf("\n"); test_strdup(); + printf("\n"); + test_calloc(); return 0; } @@ -99,12 +101,22 @@ void test_write() { // ft_write { + printf("ft_write ret: %ld\n", ft_write(1, "test ", 5)); + size_t error = ft_write(-1, "test ", 5); + printf("ft_write error: %ld\n", error); + printf("ERRNO_%d: %s\n", errno, strerror(errno)); + } + printf("\n"); + // write { printf("write ret: %ld\n", write(1, "test ", 5)); + size_t error = write(-1, "test ", 5); + printf("write error: %ld\n", error); + printf("ERRNO_%d: %s\n", errno, strerror(errno)); } } @@ -115,6 +127,9 @@ test_read() { char buf[10]; printf("ft_read ret: %ld\n", ft_read(open("main.c", O_RDONLY), buf, 10)); printf ("buf: %s\n", buf); + size_t error = ft_read(open("test", O_RDWR), buf, 10); + printf("ft_read error: %ld\n", error); + printf("ERRNO_%d: %s\n", errno, strerror(errno)); } printf("\n"); @@ -124,6 +139,9 @@ test_read() { char buf[10]; printf("read ret: %ld\n", read(open("main.c", O_RDONLY), buf, 10)); printf ("buf: %s\n", buf); + size_t error = read(open("test", O_RDWR), buf, 10); + printf("read error: %ld\n", error); + printf("ERRNO_%d: %s\n", errno, strerror(errno)); } } @@ -131,12 +149,27 @@ void test_strdup() { // ft_strdup { + char *buf = malloc(10); + strcpy(buf, "test"); + char *dup = ft_strdup(buf); + printf("ft_strdup buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup); + + free(buf); + free(dup); } + // strdup { + char *buf = malloc(10); + strcpy(buf, "test"); + char *dup = strdup(buf); + printf("strdup buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup); + + free(buf); + free(dup); } } @@ -144,11 +177,40 @@ void test_calloc() { // ft_calloc { + printf("ft_calloc\n"); + char *error = ft_calloc(2, 10); // return "" + printf("error: %p\n", error); + + char *empty = ft_calloc(0, 0); + printf("empty: %s_%p\n", empty, empty); + + char *buf = ft_calloc(10, 1); + strcpy(buf, "test"); + printf("test: %s_%p\n", buf, buf); + + free(empty); + free(buf); } + printf("\n"); + // calloc { + printf("calloc\n"); + char *error = calloc(2, 10); // return "" + printf("error: %p\n", error); + + char *empty = calloc(0, 0); + printf("empty: %s_%p\n", empty, empty); + + char *buf = calloc(10, 1); + strcpy(buf, "test"); + printf("test: %s_%p\n", buf, buf); + + free(empty); + free(buf); } } + diff --git a/main.h b/main.h index 254c864..0b43c56 100644 --- a/main.h +++ b/main.h @@ -5,6 +5,8 @@ #include #include #include +#include +#include // DONE extern size_t ft_strlen(const char *s); diff --git a/src/calloc.s b/src/calloc.s new file mode 100644 index 0000000..f5b65b3 --- /dev/null +++ b/src/calloc.s @@ -0,0 +1,30 @@ +bits 64 + +%include "defines.inc" + +section .text + global ft_calloc + ; rdi = nitems, rsi = size + ft_calloc: + push rbx + mov rbx, rdi + + mul rbx, rsi + cmp rbx, rdi + jl return_null + + mov rdi, rbx + call malloc wrt ..plt + + mov rdi, rax + inc rbx + mov rsi, rbx + call bzero wrt ..plt + + pop rbx + ret + + return_null: + pop rbx + mov rax, 0 + ret diff --git a/src/defines.inc b/src/defines.inc index f79d82d..79b2638 100644 --- a/src/defines.inc +++ b/src/defines.inc @@ -8,5 +8,8 @@ extern ft_strlen extern ft_strcpy +extern malloc +extern bzero +extern __errno_location %endif diff --git a/src/ft_read.s b/src/ft_read.s index f48a6a4..516c84f 100644 --- a/src/ft_read.s +++ b/src/ft_read.s @@ -9,4 +9,16 @@ section .text mov rax, SYS_READ syscall + cmp rax, 0 + jl errno + + ret + + errno: + neg rax + push rax + call __errno_location wrt ..plt + pop rdx + mov [rax], edx + mov rax, -1 ret diff --git a/src/ft_strcmp.s b/src/ft_strcmp.s index 8f923e8..94202c9 100644 --- a/src/ft_strcmp.s +++ b/src/ft_strcmp.s @@ -4,14 +4,14 @@ bits 64 section .text global ft_strcmp + ; rdi = s1, rsi = s2 ft_strcmp: - - mov rax, 0 - loop: - mov cl, [rdi + rax] - mov dl, [rsi + rax] + mov rax, -1 + loop: inc rax + mov cl, [rdi + rax] ; cl = s1[rax] + mov dl, [rsi + rax] ; dl = s2[rax] cmp cl, 0 je endloop diff --git a/src/ft_strcpy.s b/src/ft_strcpy.s index f71f107..de6ba00 100644 --- a/src/ft_strcpy.s +++ b/src/ft_strcpy.s @@ -6,7 +6,7 @@ section .text global ft_strcpy ; rdi = dst, rsi = src ft_strcpy: - push rbp +; push rbp mov rax, 0 ; counter mov rcx, 0 @@ -21,7 +21,7 @@ section .text cmp dl, 0 jne loop - pop rbp +; pop rbp mov rax, rdi ret diff --git a/src/ft_strdup.s b/src/ft_strdup.s index 6070c6e..6735132 100644 --- a/src/ft_strdup.s +++ b/src/ft_strdup.s @@ -4,4 +4,19 @@ bits 64 section .text global ft_strdup + ; rdi = s ft_strdup: + push rbx + mov rbx, rdi + + call ft_strlen + mov rdi, rax ; put strlen result in rdi + inc rdi + call malloc wrt ..plt + + mov rdi, rax ; put malloc result in dest + mov rsi, rbx ; put s in src + call ft_strcpy + + pop rbx + ret diff --git a/src/ft_strlen.s b/src/ft_strlen.s index d5e29e0..fa8cf39 100644 --- a/src/ft_strlen.s +++ b/src/ft_strlen.s @@ -3,24 +3,17 @@ bits 64 section .text global ft_strlen ft_strlen: - - ; empilement - push rbp - ; code mov rbx, rdi - mov rsi, 0 + mov rsi, -1 loop: - mov al, [rbx + rsi] inc rsi + mov al, [rbx + rsi] cmp al, 0 jne loop - ; rétablissement - pop rbp - ; return mov rax, rsi ret diff --git a/src/ft_write.s b/src/ft_write.s index 860d8a3..767823c 100644 --- a/src/ft_write.s +++ b/src/ft_write.s @@ -4,9 +4,21 @@ bits 64 section .text global ft_write - ft_write: ; rdi = fd, rsi = buf, rdx = count + ft_write: mov rax, SYS_WRITE syscall + cmp rax, 0 + jl errno + + ret + + errno: + neg rax + push rax + call __errno_location wrt ..plt + pop rdx + mov [rax], edx + mov rax, -1 ret