Compare commits

..

6 Commits

Author SHA1 Message Date
y-syo
3137cde790 🔨」 fix(test/main.c): improving the tests readability 2026-01-06 12:06:26 +01:00
y-syo
16b3a15963 」 feat(strdup): yayy :D 2026-01-06 11:24:05 +01:00
y-syo
388aebd819 🔨」 fix(Makefile): fixed rm missing the -f flag for fclean + added tester 2025-12-06 17:44:56 +01:00
y-syo
f6ad491a44 」 feat(ft_read): added read yayy 2025-12-06 17:38:13 +01:00
y-syo
09649f52d5 」 feat(write + strcmp): fixed strcmp, write done ! first syscall git status :D 2025-12-05 20:17:34 +01:00
y-syo
0d80bed612 ❄️」 nix: nixed the project. 2025-12-05 20:15:24 +01:00
11 changed files with 256 additions and 33 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

4
.gitignore vendored
View File

@@ -1,2 +1,6 @@
.direnv/
*/**/a.out
*/**/*.o
libasm.a
out
tester

47
Makefile Normal file
View File

@@ -0,0 +1,47 @@
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 -f $(NAME)
rm -f $(t_NAME)
re: fclean
$(MAKE) -s all
test: $(OBJ)
rm -f out_libasm
rm -f out_libc
$(t_CC) $(OBJ) $(t_SRC) $(t_CFLAGS) -o $(t_NAME)
.PHONY: clean fclean all re

27
flake.lock generated Normal file
View File

@@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"flake": false,
"locked": {
"lastModified": 1751274312,
"narHash": "sha256-/bVBlRpECLVzjV19t5KMdMFWSwKLtb5RyXdjz3LJT+g=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "50ab793786d9de88ee30ec4e4c24fb4236fc2674",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-24.11",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

28
flake.nix Normal file
View File

@@ -0,0 +1,28 @@
{
description = "a test development environment for python";
inputs = {
nixpkgs = {
url = "nixpkgs/nixos-24.11";
flake = false;
};
};
outputs = inputs@{ nixpkgs, ... }:
{
devShell = {
x86_64-linux = let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in pkgs.mkShell {
packages = with pkgs; [
gcc nasm gnumake
];
shellHook = ''
echo -e "\x1B[0;33mentering libasm environment...\x1B[0m"
'';
};
};
};
}

31
src/mem/ft_strdup.s Normal file
View File

@@ -0,0 +1,31 @@
section .text
global ft_strdup
extern malloc
extern ft_strlen
extern ft_strcpy
; char *ft_strdup(const char *str)
ft_strdup:
cmp rdi, 0
je .endz
push rdi
call ft_strlen
dec rax
mov rdi, rax
call malloc
mov rdx, rdi
mov rdi, rax
pop rsi
call ft_strcpy
jmp .end
.endz:
mov rax, 0
.end:
ret

View File

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

View File

@@ -1,30 +0,0 @@
int ft_strlen(char *str);
char *ft_strcpy(char *dest, const char *src);
int ft_strcmp(const char *s1, const char *s2);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
printf("\n--- ft_strlen ---\n");
printf("arg : \"i love kissing girls\" | result : %d\n", ft_strlen("i love kissing girls"));
printf("arg : NULL | result : %d\n", ft_strlen(NULL));
printf("arg : \"\" | result : %d\n", ft_strlen(""));
printf("\n--- ft_strcpy ---\n");
char *a1 = calloc(sizeof(char), 10);
char *a2 = calloc(sizeof(char), 10);
printf("arg : \"abcdefgh\" | result : %s\n", ft_strcpy(a1, "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", strcpy(a2, "67"));
printf("arg : \"aaa\" (dest = NULL) | result : %s\n", ft_strcpy(NULL, "67"));
printf("arg : NULL | result : %s\n", ft_strcpy(a2, NULL));
printf("\n--- ft_strcmp ---\n");
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"));
}

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

@@ -0,0 +1,22 @@
extern __errno_location
section .text
global ft_read
; ssize_t ft_read(int fd, void *buf, size_t count);
ft_read:
mov eax, 0
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

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

@@ -0,0 +1,22 @@
extern __errno_location
section .text
global ft_write
; ssize_t ft_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

66
src/tests/main.c Normal file
View File

@@ -0,0 +1,66 @@
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
ssize_t ft_strlen(char *str);
char *ft_strcpy(char *dest, const char *src);
int ft_strcmp(const char *s1, const char *s2);
ssize_t ft_write(int fd, const char *buf, unsigned int count);
ssize_t ft_read(int fd, char *buf, unsigned int count);
char *ft_strdup(const char *str);
int main(void)
{
printf("\n--- ft_strlen ---\n");
printf("arg: \"i love kissing girls\" | result: %ld (expected: %ld)\n", ft_strlen("i love kissing girls"), strlen("i love kissing girls"));
printf("arg: \"\" | result: %ld (expected: %ld)\n", ft_strlen(""), strlen(""));
printf("EXCPETION: arg: NULL | result: %ld\n", ft_strlen(NULL));
printf("\n--- ft_strcpy ---\n");
char *a1 = calloc(sizeof(char), 10);
char *a2 = calloc(sizeof(char), 10);
printf("arg: \"abcdefgh\" | result: %s ", ft_strcpy(a1, "abcdefgh"));
printf("(expected: %s)\n", strcpy(a2, "abcdefgh"));
printf("arg: \"67\" | result: %s ", ft_strcpy(a1, "67"));
printf("(expected: %s)\n", strcpy(a2, "67"));
printf("EXCEPTION: dest: NULL | result: %s\n", ft_strcpy(NULL, "67"));
printf("EXCEPTION: src: NULL | result: %s\n", ft_strcpy(a2, NULL));
printf("\n--- ft_strcmp ---\n");
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: \"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("arg: 1 \"xd\" 2 | result: %ld ", ft_write(1, "xd", 2));
printf("(expected: %ld)\n", write(1, "xd", 2));
printf("arg: 200 \"xd\" 2 | result: %ld ", ft_write(200, "xd", 2));
printf("(expected: %ld)\n", write(200, "xd", 2));
int fd = open("out_libasm", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
int fd2 = open("out_libc", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
printf("arg: %d \"[...]\" 42 | result: %ld ", fd, ft_write(fd, "wizard money gang, we love casting spells", 41));
printf("(expected: %ld)\n", write(fd2, "wizard money gang, we love casting spells", 41));
close(fd);
printf("\n--- ft_read ---\n");
char *xd = calloc(sizeof(char), 43);
printf("arg: 200 (buffer) 2 | result: %ld : \"%s\" ", ft_read(200, xd, 6), xd);
printf("(expected: %ld : \"%s\")\n", read(200, xd, 6), xd);
fd = open("out_libasm", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
fd2 = open("out_libc", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
printf("arg: %d (buffer) 42 | result: %ld : \"%s\" ", fd, ft_read(fd, xd, 41), xd);
printf("(expected: %ld : \"%s\")\n", read(fd2, xd, 41), xd);
printf("\n--- ft_strdup ---\n");
printf("%s\n", ft_strdup("this is a very cool test yayy"));
printf("%s\n", ft_strdup("hi"));
if (ft_strdup(NULL) == NULL)
printf("arg: NULL | %s\n", "(null)"); // because printf's behavior on NULL is unexpected, i will not try it (it segfaults on my computer :c)
else
printf("arg: NULL | %s\n (this should never happen)", ft_strdup(NULL));
}