diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-02-11 15:40:48 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-02-11 15:40:48 +0100 |
| commit | cd5c9e6a923878e797212d27476ee217eb844a14 (patch) | |
| tree | f5ea8605d0521cbfbf384b05567bf3d2155b0b45 | |
| parent | 28424105e255f0a4c887b6e6a83afd6dce372709 (diff) | |
| download | libasm-cd5c9e6a923878e797212d27476ee217eb844a14.tar.gz libasm-cd5c9e6a923878e797212d27476ee217eb844a14.tar.bz2 libasm-cd5c9e6a923878e797212d27476ee217eb844a14.zip | |
Added Linux compatibility
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 12 | ||||
| -rw-r--r-- | ft_atoi_base.s | 23 | ||||
| -rw-r--r-- | ft_list_push_front.s | 16 | ||||
| -rw-r--r-- | ft_list_remove_if.s | 22 | ||||
| -rw-r--r-- | ft_list_size.s | 11 | ||||
| -rw-r--r-- | ft_list_sort.s | 14 | ||||
| -rw-r--r-- | ft_read.s | 56 | ||||
| -rw-r--r-- | ft_strcmp.s | 12 | ||||
| -rw-r--r-- | ft_strcpy.s | 11 | ||||
| -rw-r--r-- | ft_strdup.s | 34 | ||||
| -rw-r--r-- | ft_strlen.s | 11 | ||||
| -rw-r--r-- | ft_write.s | 54 |
13 files changed, 179 insertions, 98 deletions
@@ -6,3 +6,4 @@ test.s test.c t.c t.s +tags @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/22 02:56:22 by cacharle #+# #+# # -# Updated: 2020/02/08 20:55:14 by cacharle ### ########.fr # +# Updated: 2021/02/11 14:50:09 by charles ### ########.fr # # # # **************************************************************************** # @@ -17,7 +17,11 @@ CC = gcc CCFLAGS = -g -Wall -Wextra -fomit-frame-pointer NASM = nasm -NASMFLAGS = -f macho64 +ifeq ($(shell uname),Linux) + NASMFLAGS = -f elf64 -D__LINUX__=1 +else + NASMFLAGS = -f macho64 +endif NAME = libasm.a ASMSRC = ft_strlen.s ft_strcpy.s ft_strcmp.s ft_write.s ft_read.s \ @@ -27,6 +31,8 @@ ASMOBJ = $(ASMSRC:.s=.o) all: $(NAME) +bonus: all + $(NAME): $(ASMOBJ) $(LIB) $(NAME) $(ASMOBJ) @@ -43,3 +49,5 @@ fclean: clean $(RM) $(NAME) re: fclean all + +.PHONY: all bonus test clean fclean re diff --git a/ft_atoi_base.s b/ft_atoi_base.s index 663b399..6803000 100644 --- a/ft_atoi_base.s +++ b/ft_atoi_base.s @@ -19,7 +19,7 @@ ; 2. pass param with regs rdi, rsi, rdx, rcx, r8, r9 ; if there is more pass them onto the stack in reverse order ; 3. use call -; 4. restore stack state by removing passed on the stack param +; 4. restore stack state by removing passed on the stack param ; 5. return value of callee in rax ; 6. restore the caller-saved register and saved passed params ; We can assume that no other register has been altered. @@ -37,13 +37,22 @@ ; 5. remove local var, add to the stack the previoulsy subtracted amount. ; 6. use ret +%ifdef __LINUX__ + %define M_FT_STRLEN ft_strlen + %define M_FT_ATOI_BASE ft_atoi_base +%else + %define M_FT_STRLEN _ft_strlen + %define M_FT_ATOI_BASE _ft_atoi_base +%endif -extern _ft_strlen -global _ft_atoi_base +extern M_FT_STRLEN +global M_FT_ATOI_BASE + +section .text ; int ft_atoi_base(const char *str, const char *base); -_ft_atoi_base: +M_FT_ATOI_BASE: ; ===prolog=== ; long int nb (8) ; int radix (4) @@ -95,9 +104,9 @@ FT_ATOI_BASE_SIGN_LOOP: ; base radix push rdi mov rdi, rsi - call _ft_strlen + call M_FT_STRLEN pop rdi - mov dword [rsp + 8], eax + mov dword [rsp + 8], eax ; main loop FT_ATOI_BASE_LOOP: @@ -163,7 +172,7 @@ _check_base: mov dword [rsp], 0 ; check for empty or size 1 base push rdi - call _ft_strlen + call M_FT_STRLEN pop rdi cmp rax, 2 jl CHECK_BASE_FALSE diff --git a/ft_list_push_front.s b/ft_list_push_front.s index 6a95909..e4cfb02 100644 --- a/ft_list_push_front.s +++ b/ft_list_push_front.s @@ -10,12 +10,20 @@ ; ; ; **************************************************************************** ; -extern _malloc +%ifdef __LINUX__ + %define M_FT_LIST_PUSH_FRONT ft_list_push_front + %define M_MALLOC malloc +%else + %define M_FT_LIST_PUSH_FRONT _ft_list_push_front + %define M_MALLOC _malloc +%endif -global _ft_list_push_front +extern M_MALLOC + +global M_FT_LIST_PUSH_FRONT ; void ft_list_push_front(t_list **begin_list, void *data); -_ft_list_push_front: +M_FT_LIST_PUSH_FRONT: cmp rdi, 0 je FT_LIST_PUSH_FRONT_END @@ -23,7 +31,7 @@ _ft_list_push_front: push rsi xor rdi, rdi mov edi, 16 - call _malloc + call M_MALLOC wrt ..plt pop rsi pop rdi cmp rax, 0 diff --git a/ft_list_remove_if.s b/ft_list_remove_if.s index 5a2fd8b..7ce06a0 100644 --- a/ft_list_remove_if.s +++ b/ft_list_remove_if.s @@ -10,9 +10,17 @@ ; ; ; **************************************************************************** ; -global _ft_list_remove_if +%ifdef __LINUX__ + %define M_FT_LIST_REMOVE_IF ft_list_remove_if + %define M_FREE free +%else + %define M_FT_LIST_REMOVE_IF _ft_list_remove_if + %define M_FREE _free +%endif -extern _free +global M_FT_LIST_REMOVE_IF + +extern M_FREE %define NULL 0x0 @@ -33,7 +41,7 @@ extern _free ; ft_list_remove_if(t_list **begin_list, void *data_ref, ; int (*cmp)(void *data, void *data_ref), ; void (*free_fct)(void *)) -_ft_list_remove_if: +M_FT_LIST_REMOVE_IF: ; t_list *saved_next ; === prolog === @@ -50,7 +58,7 @@ _ft_list_remove_if: ; === compare (*begin_list)->data and data_ref EXTERN_FUNCTION_SAVE mov rdi, [rdi] - mov rdi, [rdi + 0] + mov rdi, [rdi + 0] call rdx ; cmp((*begin_list)->data, data_ref) EXTERN_FUNCTION_SAVE_END cmp rax, 0 @@ -60,7 +68,7 @@ _ft_list_remove_if: push rdi mov rdi, [rdi] lea rdi, [rdi + 8] - call _ft_list_remove_if + call M_FT_LIST_REMOVE_IF pop rdi jmp FT_LIST_REMOVE_IF_END @@ -80,12 +88,12 @@ FT_LIST_REMOVE_IF_REMOVE: EXTERN_FUNCTION_SAVE mov rdi, [rdi] - call _free ; free(*begin_list) + call M_FREE wrt ..plt ; free(*begin_list) EXTERN_FUNCTION_SAVE_END mov rax, [rbp - 8] mov [rdi], rax - call _ft_list_remove_if + call M_FT_LIST_REMOVE_IF FT_LIST_REMOVE_IF_END: ; === epilog === diff --git a/ft_list_size.s b/ft_list_size.s index b41cfb4..0d80bd9 100644 --- a/ft_list_size.s +++ b/ft_list_size.s @@ -10,10 +10,17 @@ ; ; ; **************************************************************************** ; -global _ft_list_size +%ifdef __LINUX__ + %define M_FT_LIST_SIZE ft_list_size +%else + %define M_FT_LIST_SIZE _ft_list_size +%endif +global M_FT_LIST_SIZE + +section .text ; int ft_list_size(t_list *begin_list); -_ft_list_size: +M_FT_LIST_SIZE: xor eax, eax FT_LIST_SIZE_LOOP: cmp rdi, 0 diff --git a/ft_list_sort.s b/ft_list_sort.s index a541bd8..b4899ef 100644 --- a/ft_list_sort.s +++ b/ft_list_sort.s @@ -10,12 +10,18 @@ ; ; ; **************************************************************************** ; -global _ft_list_sort +%ifdef __LINUX__ + %define M_FT_LIST_SORT ft_list_sort +%else + %define M_FT_LIST_SORT _ft_list_sort +%endif + +global M_FT_LIST_SORT %define NULL 0x0 ; void ft_list_sort(t_list **begin_list, int (*cmp)(void*, void*)); -_ft_list_sort: +M_FT_LIST_SORT: ; t_list *slow : rax = *begin_list ; t_list *fast : rbx = (*begin_list)->next ; t_list *middle : rsp + 0 @@ -54,9 +60,9 @@ FT_LIST_SORT_MIDDLE_LOOP_END: ; === sorting both child list === push rdi push rsi - call _ft_list_sort ; ft_list_sort(begin_list, cmp) + call M_FT_LIST_SORT ; ft_list_sort(begin_list, cmp) lea rdi, [rbp - 8] - call _ft_list_sort ; ft_list_sort(&middle, cmp) + call M_FT_LIST_SORT ; ft_list_sort(&middle, cmp) pop rsi pop rdi @@ -10,38 +10,38 @@ ; ; ; **************************************************************************** ; -global _ft_read +%ifdef __LINUX__ + %define M_FT_READ ft_read + %define M_ERRNO_LOCATION __errno_location + %define M_SYSCALL_READ 0x0 +%else + %define M_FT_READ _ft_read + %define M_ERRNO_LOCATION ___error + %define M_SYSCALL_READ 0x2000003 +%endif -%define F_GETFD 1 -%define SYSCALL_READ 0x2000003 -%define SYSCALL_FCNTL 0x200005c +extern M_ERRNO_LOCATION -; int ft_read(int, void*, size_t); -_ft_read: - cmp rdx, 0 - je FT_READ_NO_SIZE - cmp rdi, 0 - jl FT_READ_ERROR - cmp rsi, 0 - je FT_READ_ERROR - - push rdx - push rsi - xor rsi, rsi - mov esi, F_GETFD - mov rax, SYSCALL_FCNTL - syscall - pop rsi - pop rdx - cmp eax, 0 - jne FT_READ_ERROR +global M_FT_READ - mov rax, SYSCALL_READ +section .text +; int ft_read(int, void*, size_t); +M_FT_READ: + mov rax, M_SYSCALL_READ syscall +%ifdef __LINUX__ + cmp rax, 0 + jl FT_READ_ERROR +%else + jc FT_READ_ERROR +%endif ret FT_READ_ERROR: - mov rax, -1 - ret -FT_READ_NO_SIZE: - xor rax, rax +%ifdef __LINUX__ + neg rax +%endif + push rax + call M_ERRNO_LOCATION wrt ..plt + pop qword [rax] + mov rax, -1 ret diff --git a/ft_strcmp.s b/ft_strcmp.s index 7704aa5..7c55a43 100644 --- a/ft_strcmp.s +++ b/ft_strcmp.s @@ -10,10 +10,18 @@ ; ; ; **************************************************************************** ; -global _ft_strcmp +%ifdef __LINUX__ + %define M_FT_STRCMP ft_strcmp +%else + %define M_FT_STRCMP _ft_strcmp +%endif + +global M_FT_STRCMP + +section .text ; int ft_strcmp(const char *s1, const char *s2); -_ft_strcmp: +M_FT_STRCMP: push r12 push r13 push rcx diff --git a/ft_strcpy.s b/ft_strcpy.s index 3789283..eb8ea1a 100644 --- a/ft_strcpy.s +++ b/ft_strcpy.s @@ -10,10 +10,17 @@ ; ; ; **************************************************************************** ; -global _ft_strcpy +%ifdef __LINUX__ + %define M_FT_STRCPY ft_strcpy +%else + %define M_FT_STRCPY _ft_strcpy +%endif +global M_FT_STRCPY + +section .text ; char *ft_strcpy(char *dst, const char *src); -_ft_strcpy: +M_FT_STRCPY: push rbx push rcx mov rax, rdi ; dst diff --git a/ft_strdup.s b/ft_strdup.s index a31f36f..346f2b4 100644 --- a/ft_strdup.s +++ b/ft_strdup.s @@ -10,30 +10,42 @@ ; ; ; **************************************************************************** ; -extern _ft_strlen -extern _ft_strcpy -extern _malloc - -global _ft_strdup +%ifdef __LINUX__ + %define M_FT_STRDUP ft_strdup + %define M_FT_STRLEN ft_strlen + %define M_FT_STRCPY ft_strcpy + %define M_MALLOC malloc +%else + %define M_FT_STRDUP _ft_strdup + %define M_FT_STRLEN _ft_strlen + %define M_FT_STRCPY _ft_strcpy + %define M_MALLOC _malloc +%endif + +extern M_FT_STRLEN +extern M_FT_STRCPY +extern M_MALLOC + +global M_FT_STRDUP + +section .text ; char *ft_strdup(const char *str); -_ft_strdup: +M_FT_STRDUP: push rdi ; save rdi because it will be overwrite for malloc - call _ft_strlen ; rdi is still == str + call M_FT_STRLEN ; rdi is still == str inc rax ; len++ for '\0' mov rdi, rax ; size to malloc - call _malloc + call M_MALLOC wrt ..plt cmp rax, 0 je FT_STRDUP_ERROR pop rsi ; original str as src mov rdi, rax ; allocated as dest - call _ft_strcpy + call M_FT_STRCPY ret FT_STRDUP_ERROR: pop rdi ret - - diff --git a/ft_strlen.s b/ft_strlen.s index 2f2dd26..b0f84e7 100644 --- a/ft_strlen.s +++ b/ft_strlen.s @@ -10,10 +10,17 @@ ; ; ; **************************************************************************** ; -global _ft_strlen +%ifdef __LINUX__ + %define M_FT_STRLEN ft_strlen +%else + %define M_FT_STRLEN _ft_strlen +%endif +global M_FT_STRLEN + +section .text ; int ft_strlen(char *); -_ft_strlen: +M_FT_STRLEN: mov eax, -1 FT_STRLEN_LOOP: inc eax @@ -10,38 +10,38 @@ ; ; ; **************************************************************************** ; -global _ft_write +%ifdef __LINUX__ + %define M_FT_WRITE ft_write + %define M_ERRNO_LOCATION __errno_location + %define M_SYSCALL_WRITE 0x1 +%else + %define M_FT_WRITE _ft_write + %define M_ERRNO_LOCATION ___error + %define M_SYSCALL_WRITE 0x2000004 +%endif -%define F_GETFD 1 -%define SYSCALL_WRITE 0x2000004 -%define SYSCALL_FCNTL 0x200005c +extern M_ERRNO_LOCATION -; int ft_write(int rdi, const void *rsi, size_t rdx); -_ft_write: - cmp rdx, 0 - je FT_WRITE_NO_SIZE - cmp rdi, 0 - jl FT_WRITE_ERROR ; fd < 0 - cmp rsi, 0 - je FT_WRITE_ERROR ; buf == NULL - - push rdx - push rsi - xor rsi, rsi - mov esi, F_GETFD - mov rax, SYSCALL_FCNTL - syscall - pop rsi - pop rdx - cmp eax, 0 - jne FT_WRITE_ERROR +global M_FT_WRITE - mov rax, SYSCALL_WRITE +section .text +; int ft_write(int rdi, const void *rsi, size_t rdx); +M_FT_WRITE: + mov rax, M_SYSCALL_WRITE syscall +%ifdef __LINUX__ + cmp rax, 0 + jl FT_WRITE_ERROR +%else + jc FT_WRITE_ERROR +%endif ret FT_WRITE_ERROR: +%ifdef __LINUX__ + neg rax +%endif + push rax + call M_ERRNO_LOCATION wrt ..plt + pop qword [rax] mov rax, -1 ret -FT_WRITE_NO_SIZE: - mov rax, 0 - ret |
