Makefile done, main to test, ft_strlen done

This commit is contained in:
Xamora 2026-09-14 17:34:13 +02:00
commit 7eb59b5c03
4 changed files with 80 additions and 0 deletions

38
Makefile Normal file
View file

@ -0,0 +1,38 @@
CC := gcc
AS := nasm
ASFLAGS := -g -Wall
CFLAGS := -g -Wall -Wextra -Wno-override-init -std=c2x
MAIN := $(wildcard *.c)
SRC := $(wildcard src/*.s)
OBJ_DIR := build
OBJ_CC := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o, $(MAIN)))
OBJ_AS := $(addprefix $(OBJ_DIR)/, $(patsubst %.s,%.o, $(SRC)))
DIR := .
NAME := main
LIB_NAME:= asm
LD := $(CC)
LDFLAGS := -L$(OBJ_DIR) -l$(LIB_NAME)
all: $(NAME)
$(NAME): $(OBJ_CC) $(OBJ_AS)
ar rcs $(OBJ_DIR)/lib$(LIB_NAME).a $(OBJ_AS)
$(LD) $(OBJ_CC) $(LDFLAGS) -o $(DIR)/$(NAME)
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/%.o: %.s
@mkdir -p $(@D)
$(AS) $(ASFLAGS) -f elf64 $< -o $@
clean:
@rm -f $(NAME)
@rm -rf $(OBJ_DIR)
re:
@make --no-print clean
@make --no-print all

7
main.c Normal file
View file

@ -0,0 +1,7 @@
#include "main.h"
int main(void) {
size_t size = ft_strlen("test\n");
printf("size: %ld\n", size);
return 0;
}

6
main.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
#include <string.h>
#include <stdio.h>
extern size_t ft_strlen(const char *s);

29
src/ft_strlen.s Normal file
View file

@ -0,0 +1,29 @@
bits 64
%define STDOUT 1
%define SYS_WRITE 1
%define SYS_EXIT 60
section .text
global ft_strlen
ft_strlen:
; empilement
push rbp
; code
mov rbx, rdi
mov rsi, -1
loop:
inc rsi
mov al, [rbx + rsi]
cmp al, 0
jne loop
; rétablissement
pop rbp
; return
mov rax, rsi
ret