feat: Makefile, strlen and strcpy
This commit is contained in:
parent
fa6e7916ef
commit
60d76c0453
6 changed files with 118 additions and 0 deletions
45
Makefile
45
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)
|
||||||
25
src/ft_strcpy.s
Normal file
25
src/ft_strcpy.s
Normal file
|
|
@ -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
|
||||||
17
src/ft_strlen.s
Normal file
17
src/ft_strlen.s
Normal file
|
|
@ -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
|
||||||
17
src/mylib.h
Normal file
17
src/mylib.h
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#ifndef MYLIB_H
|
||||||
|
#define MYLIB_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
size_t ft_strlen(char *str);
|
||||||
|
char *ft_strcpy(char *dest, const char *src);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
14
test.c
Normal file
14
test.c
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
#include "src/mylib.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue