Skip to content

Commit

Permalink
init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathéo committed Mar 9, 2023
0 parents commit db2964d
Show file tree
Hide file tree
Showing 23 changed files with 810 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
.vscode
*.o
libasm.so
*.gcda
*.gcno
unit_tests
Binary file added B-ASM-400_minilibc.pdf
Binary file not shown.
58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
##
## EPITECH PROJECT, 2023
## MiniLibC
## File description:
## MAKEFILE file.
##

# Source files
SRC = src/strlen.asm \
src/strchr.asm \
src/strrchr.asm \
src/memset.asm \
src/memcpy.asm \
src/strcmp.asm \
src/strcasecmp.asm \
src/strncmp.asm \
src/strpbrk.asm

# Object files
OBJ = $(SRC:.asm=.o)

# Output library name
NAME = libasm.so

# Output tests name
TESTS_NAME = unit_tests

# Method to compile the .asm files
$(OBJ):
for asm in $(SRC); do \
nasm -f elf64 $$asm; \
done

# Method to compile the library
$(NAME): $(OBJ)
ld -shared -o $(NAME) $(OBJ)

# Main method
all: $(NAME)

# Method to remove the .o files
clean:
rm -f $(OBJ)

# Method to remove the library
fclean: clean
rm -f $(NAME)

# Method to recompile the all project
re: fclean all

# Method to test the library
tests_run: re
gcc -lcriterion --coverage -o $(TESTS_NAME) $(OBJ) tests/*.c
LD_PRELOAD=./$(NAME) ./unit_tests

# To avoid relinking
.PHONY: all clean fclean re
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# MiniLibC

[![Continuous Integration](https://github.com/matheograil/mini-lib-c/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/matheograil/mini-lib-c/actions/workflows/ci.yml)

Here are the functions to be implemented in your MiniLibC:
- strlen
- strchr
- strrchr
- memset
- memcpy
- strcmp
- memmove
- strncmp
- strcasecmp
- strstr
- strpbrk
- strcspn
29 changes: 29 additions & 0 deletions src/memcpy.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BITS 64 ; 64-bits mode

section .text
global memcpy ; The "memcpy" function must be callable outside

memcpy:
; "rdi" corresponds to the destination string
; "rsi" corresponds to the source string
; "rdx" corresponds to the number of characters to copy

mov rax, rdi ; The return value is the destination string

call memcpy_loop

memcpy_loop:
cmp rdx, 0
je memcpy_end ; If the index is 0, we stop

mov bh, [rsi] ; We not use "al" because "rax" is the return value, and "bl" becuase it's proctected
mov [rdi], bh ; Replaces the current character (only first byte)

dec rdx ; Decrements the index
inc rsi ; Moves to the next character (source)
inc rdi ; Moves to the next character (destination)

jmp memcpy_loop ; Recursivity

memcpy_end:
ret
27 changes: 27 additions & 0 deletions src/memset.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
BITS 64 ; 64-bits mode

section .text
global memset ; The "memset" function must be callable outside

memset:
; "rdi" corresponds to the string
; "rsi" corresponds to the character to set
; "rdx" corresponds to the number of characters to set

mov rax, rdi ; The return value is the same string as the parameter

call memset_loop

memset_loop:
cmp rdx, 0
je memset_end ; If the index is 0, we stop

mov [rdi], sil ; Replaces the current character (only first byte)

inc rdi ; Moves to the next character
dec rdx ; Decrements the index

jmp memset_loop ; Recursivity

memset_end:
ret
56 changes: 56 additions & 0 deletions src/strcasecmp.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
BITS 64 ; 64-bits mode

section .text
global strcasecmp ; The "strcasecmp" function must be callable outside

strcasecmp:
; "rdi" corresponds to the first string
; "rsi" corresponds to the second string

mov al, [rdi] ; Stores the current character (first string)
mov ah, [rsi] ; Stores the current character (second string)

cmp al, 0 ; Checks if the first string is finished
je strcasecmp_end_first
cmp ah, 0 ; Checks if the second string is finished
je strcasecmp_end_second

or al, 32 ; Converts the current character to lowercase (first string)
or ah, 32 ; Converts the current character to lowercase (second string)

cmp al, ah ; Compares the current characters
je strcasecmp_equal
ja strcasecmp_greater
jb strcasecmp_lower

strcasecmp_equal:
inc rdi ; Moves to the next character (first string)
inc rsi ; Moves to the next character (second string)

jmp strcasecmp ; Recursivity

strcasecmp_greater:
mov rax, 1
ret

strcasecmp_lower:
mov rax, -1
ret

strcasecmp_end_first:
cmp ah, 0 ; Checks if the both strings are finished
je strcasecmp_end_both

mov rax, -1
ret

strcasecmp_end_second:
cmp al, 0 ; Checks if the both strings are finished
je strcasecmp_end_both

mov rax, 1
ret

strcasecmp_end_both:
mov rax, 0
ret
23 changes: 23 additions & 0 deletions src/strchr.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
BITS 64 ; 64-bits mode

section .text
global strchr ; The "strchr" function must be callable outside

strchr:
cmp [rdi], sil
je strchr_found ; If the current character corresponds to the one we are looking for, returns a pointer to it

cmp [rdi], byte 0
je strchr_not_found ; If the current character is null, returns a null pointer

inc rdi ; Moves to the next character

jmp strchr ; Recursivity

strchr_found:
mov rax, rdi
ret

strchr_not_found:
mov rax, 0
ret
53 changes: 53 additions & 0 deletions src/strcmp.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
BITS 64 ; 64-bits mode

section .text
global strcmp ; The "strcmp" function must be callable outside

strcmp:
; "rdi" corresponds to the first string
; "rsi" corresponds to the second string

mov al, [rdi] ; Stores the current character (first string)
mov ah, [rsi] ; Stores the current character (second string)

cmp al, 0 ; Checks if the first string is finished
je strcmp_end_first
cmp ah, 0 ; Checks if the second string is finished
je strcmp_end_second

cmp al, ah ; Compares the current characters
je strcmp_equal
ja strcmp_greater
jb strcmp_lower

strcmp_equal:
inc rdi ; Moves to the next character (first string)
inc rsi ; Moves to the next character (second string)

jmp strcmp ; Recursivity

strcmp_greater:
mov rax, 1
ret

strcmp_lower:
mov rax, -1
ret

strcmp_end_first:
cmp ah, 0 ; Checks if the both strings are finished
je strcmp_end_both

mov rax, -1
ret

strcmp_end_second:
cmp al, 0 ; Checks if the both strings are finished
je strcmp_end_both

mov rax, 1
ret

strcmp_end_both:
mov rax, 0
ret
21 changes: 21 additions & 0 deletions src/strlen.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BITS 64 ; 64-bits mode

section .text
global strlen ; The "strlen" function must be callable outside

strlen:
mov rax, 0 ; Initializes the counter to 0

call strlen_loop ; Calls the "strlen_loop" function

strlen_loop:
cmp [rdi], byte 0 ; Is the current character equal to 0 ?
je strlen_loop_end ; If yes, we're done

inc rax ; Increments the counter
inc rdi ; Moves to the next character

jmp strlen_loop ; Recursivity

strlen_loop_end:
ret
58 changes: 58 additions & 0 deletions src/strncmp.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
BITS 64 ; 64-bits mode

section .text
global strncmp ; The "strncmp" function must be callable outside

strncmp:
; "rdi" corresponds to the first string
; "rsi" corresponds to the second string
; "rdx" corresponds to the number of characters to compare

mov al, [rdi] ; Stores the current character (first string)
mov ah, [rsi] ; Stores the current character (second string)

cmp al, 0 ; Checks if the first string is finished
je strncmp_end_first
cmp ah, 0 ; Checks if the second string is finished
je strncmp_end_second

cmp rdx, 1
je strncmp_end_both

cmp al, ah ; Compares the current characters
je strncmp_equal
ja strncmp_greater
jb strncmp_lower

strncmp_equal:
inc rdi ; Moves to the next character (first string)
inc rsi ; Moves to the next character (second string)
dec rdx ; Decreases the number of characters to compare

jmp strncmp ; Recursivity

strncmp_greater:
mov rax, 1
ret

strncmp_lower:
mov rax, -1
ret

strncmp_end_first:
cmp ah, 0 ; Checks if the both strings are finished
je strncmp_end_both

mov rax, -1
ret

strncmp_end_second:
cmp al, 0 ; Checks if the both strings are finished
je strncmp_end_both

mov rax, 1
ret

strncmp_end_both:
mov rax, 0
ret
Loading

0 comments on commit db2964d

Please sign in to comment.