」 feat(write + strcmp): fixed strcmp, write done ! first syscall git status :D

This commit is contained in:
y-syo
2025-12-05 20:15:39 +01:00
parent 0d80bed612
commit 09649f52d5
4 changed files with 87 additions and 5 deletions

44
Makefile Normal file
View File

@@ -0,0 +1,44 @@
SHELL = bash
# --- lib ---
NAME = libasm.a
OBJ_DIR = obj/
SRC_DIR = src/
SRC = $(shell find $(SRC_DIR) -name '*.s')
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.s=.o))
AS = nasm
ASFLAGS = -f elf64
# --- tester ---
t_CC = gcc
t_CFLAGS = -z noexecstack
t_SRC = src/tests/main.c
t_NAME = tester
all: $(NAME)
$(NAME): $(OBJ)
ar rcs -o $@ $(OBJ)
$(OBJ_DIR)%.o: %.s
@mkdir -p $(@D)
$(AS) $(ASFLAGS) $< -o $@
clean:
rm -rf $(OBJ_DIR)
fclean: clean
rm $(NAME)
re: fclean
$(MAKE) -s all
test: $(OBJ)
$(t_CC) $(OBJ) $(t_SRC) $(t_CFLAGS) -o $(t_NAME)
.PHONY: clean fclean all re

View File

@@ -9,11 +9,13 @@ ft_strcmp:
cmp rsi, 0 cmp rsi, 0
je .ret je .ret
mov rax, rdi
.loop: .loop:
mov r10b, BYTE [rdi] mov r10b, BYTE [rdi]
cmp r10b, BYTE [rsi] cmp r10b, BYTE [rsi]
je .end jne .end
cmp BYTE [rdi], 0 cmp BYTE [rdi], 0
je .end je .end
@@ -25,8 +27,11 @@ ft_strcmp:
jmp .loop jmp .loop
.end: .end:
mov al, BYTE [rsi] movzx rax, BYTE [rdi]
mov r10b, BYTE [rdi] movzx r10, BYTE [rsi]
sub rax, r10 sub rax, r10
;sub rdi, rax
;mov rax, rdi
.ret: .ret:
ret ret

22
src/sys/ft_write.s Normal file
View File

@@ -0,0 +1,22 @@
extern __errno_location
section .text
global ft_write
; ssize_t write(int fd, const void *buf, size_t count);
ft_write:
mov eax, 1
syscall
cmp rax, 0
jle .error
.ret:
ret
.error:
mov r10, rax
call __errno_location wrt ..plt
mov [rax], r10
mov rax, -1
jmp .ret

View File

@@ -1,8 +1,10 @@
int ft_strlen(char *str); int ft_strlen(char *str);
char *ft_strcpy(char *dest, const char *src); char *ft_strcpy(char *dest, const char *src);
int ft_strcmp(const char *s1, const char *s2); int ft_strcmp(const char *s1, const char *s2);
int ft_write(int fd, const char *buf, unsigned int count);
#include <stdio.h> #include <stdio.h>
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -20,11 +22,20 @@ int main(void)
printf("arg : \"abcdefgh\" | result : %s\n", strcpy(a2, "abcdefgh")); printf("arg : \"abcdefgh\" | result : %s\n", strcpy(a2, "abcdefgh"));
printf("arg : \"67\" | result : %s\n", ft_strcpy(a1, "67")); printf("arg : \"67\" | result : %s\n", ft_strcpy(a1, "67"));
printf("arg : \"67\" | result : %s\n", strcpy(a2, "67")); printf("arg : \"67\" | result : %s\n", strcpy(a2, "67"));
printf("arg : \"aaa\" (dest = NULL) | result : %s\n", ft_strcpy(NULL, "67")); printf("EXCEPTION : dest : NULL | result : %s\n", ft_strcpy(NULL, "67"));
printf("arg : NULL | result : %s\n", ft_strcpy(a2, NULL)); printf("EXCEPTION : src : NULL | result : %s\n", ft_strcpy(a2, NULL));
printf("\n--- ft_strcmp ---\n"); printf("\n--- ft_strcmp ---\n");
printf("arg : \"aaaaaa\" \"aaaa\" | result : %d (expected : %d)\n", ft_strcmp("aaaaaa", "aaaa"), strcmp("aaaaaa", "aaaa")); printf("arg : \"aaaaaa\" \"aaaa\" | result : %d (expected : %d)\n", ft_strcmp("aaaaaa", "aaaa"), strcmp("aaaaaa", "aaaa"));
printf("arg : \"aaaa\" \"bb\" | result : %d (expected : %d)\n", ft_strcmp("aaaa", "bb"), strcmp("aaaa", "bb")); printf("arg : \"aaaa\" \"bb\" | result : %d (expected : %d)\n", ft_strcmp("aaaa", "bb"), strcmp("aaaa", "bb"));
printf("arg : \"i love nixos !!\" \"i love nixos !!\" | result : %d (expected : %d)\n", ft_strcmp("i love nixos !!", "i love nixos !!"), strcmp("i love nixos !!", "i love nixos !!"));
printf("EXCEPTION : s1 : NULL | result : %d \n", ft_strcmp(NULL, "hiiiii :D"));
printf("EXCEPTION : s2 : NULL | result : %d \n", ft_strcmp("hihi :3", NULL));
printf("\n--- ft_write ---\n");
printf("| 1 \"xd\" 2 | %d\n", ft_write(1, "xd", 2));
printf("| 200 \"xd\" 2 | %d\n", ft_write(200, "xd", 2));
int fd = open("out", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
printf("| %d \"[...]\" 42 | %d\n", fd, ft_write(fd, "wizard money gang, we love casting spells\n", 42));
} }