diff --git a/Makefile b/Makefile index e69de29..2797c8d 100644 --- a/Makefile +++ b/Makefile @@ -0,0 +1,45 @@ +NASM=nasm +NASMFLAGS=-f elf64 -g -F dwarf +AR=ar +ARFLAGS=rcs +CC=gcc +CFLAGS=-Wall -g +SRC_DIR=src +OBJ_DIR=obj +LIB_NAME=mylib +STATIC_LIB=lib$(LIB_NAME).a +SHARED_LIB=lib$(LIB_NAME).so + +SRCS=$(SRC_DIR)/ft_strlen.s \ + $(SRC_DIR)/ft_strcpy.s + +OBJS=$(patsubst $(SRC_DIR)/%.s,$(OBJ_DIR)/%.o,$(SRCS)) +DEPS=$(OBJS:.o=.d) + +.PHONY: all clean dirs static shared + +all: dirs $(STATIC_LIB) $(SHARED_LIB) + +dirs: + @mkdir -p $(OBJ_DIR) + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.s + $(NASM) $(NASMFLAGS) $< -o $@ + +$(STATIC_LIB): $(OBJS) + $(AR) $(ARFLAGS) $@ $^ + +$(SHARED_LIB): $(OBJS) + $(CC) -shared -o $@ $^ + +static: $(STATIC_LIB) + +shared: $(SHARED_LIB) + +test: test.c $(STATIC_LIB) + $(CC) $(CFLAGS) test.c -L. -l$(LIB_NAME) -o test -Wl,-rpath=. + +clean: + rm -rf $(OBJ_DIR) $(STATIC_LIB) $(SHARED_LIB) test + +-include $(DEPS) diff --git a/src/.gitkeep b/src/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/ft_strcpy.s b/src/ft_strcpy.s new file mode 100644 index 0000000..6938166 --- /dev/null +++ b/src/ft_strcpy.s @@ -0,0 +1,25 @@ +global ft_strcpy +section .text + +; rdi = dest, rsi = src +; rax = return value +; should copy src in dest and return dest +ft_strcpy: + mov rax, rdi + +.loop: + mov dl, byte [rsi] + test dl, dl + je .done + + mov byte [rdi], dl + + inc rsi + inc rdi + + jmp .loop +.done: + + mov byte [rdi], 0x0 + mov rdi, rax + ret diff --git a/src/ft_strlen.s b/src/ft_strlen.s new file mode 100644 index 0000000..b9a219f --- /dev/null +++ b/src/ft_strlen.s @@ -0,0 +1,17 @@ +global ft_strlen +section .text + +ft_strlen: + mov rax, rdi + +.loop: + mov dl, byte [rax] + test dl, dl + je .done + + inc rax + + jmp .loop +.done: + sub rax, rdi + ret diff --git a/src/mylib.h b/src/mylib.h new file mode 100644 index 0000000..9b70800 --- /dev/null +++ b/src/mylib.h @@ -0,0 +1,17 @@ +#ifndef MYLIB_H +#define MYLIB_H + +#include +#ifdef __cplusplus +extern "C" { +#endif + +size_t ft_strlen(char *str); +char *ft_strcpy(char *dest, const char *src); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/test.c b/test.c new file mode 100644 index 0000000..6e0bf49 --- /dev/null +++ b/test.c @@ -0,0 +1,14 @@ +#include "src/mylib.h" +#include + +int +main(void) +{ + char dest[16]; + const char src[] = "Hello, world!\n"; + + printf("%s%s", ft_strcpy(dest, src), dest); + printf("%s", src); + printf("%ld\n", ft_strlen(dest)); + printf("%ld\n", strlen(dest)); +}