From ec9a19856a6e99c09f377eeb291bbdcba28cb167 Mon Sep 17 00:00:00 2001 From: tdameros Date: Thu, 8 Aug 2024 23:19:46 +0200 Subject: [PATCH] refactor: asm functions --- src/ft_atoi_base.s | 4 +-- src/ft_create_elem.s | 8 ++--- src/ft_list.s | 5 --- src/ft_list_push_front.s | 4 +-- src/ft_list_remove_if.s | 70 +++++++++++++++++----------------------- src/ft_list_size.s | 12 +++---- src/ft_list_sort.s | 62 +++++++++++++++++------------------ src/ft_read.s | 10 +++--- src/ft_strcmp.s | 31 +++++++++--------- src/ft_strcpy.s | 24 +++++++------- src/ft_strdup.s | 12 +++---- src/ft_strlen.s | 12 +++---- src/ft_write.s | 10 +++--- 13 files changed, 115 insertions(+), 149 deletions(-) diff --git a/src/ft_atoi_base.s b/src/ft_atoi_base.s index 9cdd051..e50c0ea 100644 --- a/src/ft_atoi_base.s +++ b/src/ft_atoi_base.s @@ -4,8 +4,7 @@ extern ft_strlen global ft_atoi_base -section .note.GNU-stack - +section .text ; int ft_atoi_base(char *str, char *base) ;{ ; int index; @@ -88,7 +87,6 @@ section .note.GNU-stack ; return (-1); ; } -section .text ;int ft_atoi_base(char *str, char *base); ft_atoi_base: push rdi diff --git a/src/ft_create_elem.s b/src/ft_create_elem.s index bbb51f2..dd4167c 100644 --- a/src/ft_create_elem.s +++ b/src/ft_create_elem.s @@ -6,8 +6,6 @@ extern malloc global ft_create_elem -section .note.GNU-stack - section .text ;typedef struct s_list ;{ @@ -21,11 +19,11 @@ ft_create_elem: mov rdi, LIST_SIZE call malloc wrt ..plt cmp rax, 0 - jz return + jz .return pop rdi mov [rax + LIST_DATA_OFFSET], rdi mov qword [rax + LIST_NEXT_OFFSET], 0 - jmp return + ret -return: +.return: ret diff --git a/src/ft_list.s b/src/ft_list.s index 996d270..54feab5 100644 --- a/src/ft_list.s +++ b/src/ft_list.s @@ -1,7 +1,5 @@ bits 64 -section .note.GNU-stack - ;typedef struct s_list ;{ ; void *data; @@ -11,6 +9,3 @@ section .data LIST_DATA_OFFSET EQU 0 LIST_NEXT_OFFSET EQU 8 LIST_SIZE EQU 16 - - - diff --git a/src/ft_list_push_front.s b/src/ft_list_push_front.s index 863f602..0e9a613 100644 --- a/src/ft_list_push_front.s +++ b/src/ft_list_push_front.s @@ -7,8 +7,6 @@ extern ft_create_elem global ft_list_push_front -section .note.GNU-stack - section .text ;typedef struct s_list ;{ @@ -21,10 +19,12 @@ ft_list_push_front: test rdi, rdi jz .return push rdi + push rsi mov rdi, rsi call ft_create_elem cmp rax, 0 jz .return + pop rsi pop rdi mov rsi, [rdi] mov qword [rax + LIST_NEXT_OFFSET], rsi diff --git a/src/ft_list_remove_if.s b/src/ft_list_remove_if.s index eb5a093..16c6248 100644 --- a/src/ft_list_remove_if.s +++ b/src/ft_list_remove_if.s @@ -6,8 +6,6 @@ extern free global ft_list_remove_if -section .note.GNU-stack - section .text ;typedef struct s_list ;{ @@ -45,10 +43,10 @@ section .text ft_list_remove_if: ; begin_list test rdi, rdi - jz end + jz .end ; cmp test rdx, rdx - jz end + jz .end ; save r8 and r9 push r8 push r9 @@ -56,13 +54,13 @@ ft_list_remove_if: mov r8, [rdi] ; previous_node xor r9, r9 - jmp loop -loop: + jmp .loop +.loop: test r8, r8 - jz end_loop - jmp cmp_function + jz .end_loop + jmp .cmp_function -cmp_function: +.cmp_function: push rdi push rsi push rdx @@ -79,29 +77,29 @@ cmp_function: pop rsi pop rdi test rax, rax - jz remove_node - jnz next_node + jz .remove_node + jnz .next_node -remove_node: +.remove_node: ; if previous != NULL test r9, r9 - jnz link_previous_to_next - jmp link_to_next + jnz .link_previous_to_next + jmp .link_to_next -link_previous_to_next: +.link_previous_to_next: mov rax, [r8 + LIST_NEXT_OFFSET] mov [r9 + LIST_NEXT_OFFSET], rax - jmp free_node + jmp .free_node -link_to_next: +.link_to_next: mov rax, [r8 + LIST_NEXT_OFFSET] mov [rdi], rax - jmp free_node + jmp .free_node -free_node: +.free_node: ; if free_fct != NULL test rcx, rcx - jnz call_free_fct + jnz .call_free_fct push rdi push rsi push rdx @@ -116,9 +114,9 @@ free_node: pop rdx pop rsi pop rdi - jmp update_current_after_free + jmp .update_current_after_free -call_free_fct: +.call_free_fct: push rdi push rsi push rdx @@ -129,15 +127,7 @@ call_free_fct: call rcx pop r9 pop r8 - pop rcx - pop rdx - pop rsi - pop rdi - push rdi - push rsi - push rdx - push rcx push r8 push r9 mov rdi, r8 @@ -149,29 +139,29 @@ call_free_fct: pop rdx pop rsi pop rdi - jmp update_current_after_free + jmp .update_current_after_free -update_current_after_free: +.update_current_after_free: ; if previous != NULL test r9, r9 - jnz link_current_to_previous_next + jnz .link_current_to_previous_next mov r8, [rdi] - jmp loop + jmp .loop -link_current_to_previous_next: +.link_current_to_previous_next: mov r8, [r9 + LIST_NEXT_OFFSET] - jmp loop + jmp .loop -next_node: +.next_node: mov r9, r8 mov r8, [r8 + LIST_NEXT_OFFSET] - jmp loop + jmp .loop -end_loop: +.end_loop: ; restore r8 and r9 pop r9 pop r8 ret -end: +.end: ret diff --git a/src/ft_list_size.s b/src/ft_list_size.s index 0a9f112..1f826da 100644 --- a/src/ft_list_size.s +++ b/src/ft_list_size.s @@ -4,8 +4,6 @@ bits 64 global ft_list_size -section .note.GNU-stack - section .text ;typedef struct s_list ;{ @@ -16,12 +14,12 @@ section .text ;int ft_list_size(t_list *begin_list); ft_list_size: xor rax, rax - jmp loop -loop: + jmp .loop +.loop: cmp rdi, 0 - je end + je .end mov rdi, [rdi + LIST_NEXT_OFFSET] inc rax - jmp loop -end: + jmp .loop +.end: ret \ No newline at end of file diff --git a/src/ft_list_sort.s b/src/ft_list_sort.s index f2ae1bf..95e459b 100644 --- a/src/ft_list_sort.s +++ b/src/ft_list_sort.s @@ -2,8 +2,6 @@ bits 64 %include "ft_list.s" -section .note.GNU-stack - global ft_list_sort section .text @@ -71,13 +69,13 @@ section .text ;void ft_list_sort(t_list **begin_list, int (*cmp)()); ft_list_sort: test rdi, rdi - jz ft_list_sort_end + jz .ft_list_sort_end mov r8, [rdi] test r8, r8 - jz ft_list_sort_end + jz .ft_list_sort_end mov r8, [r8 + LIST_NEXT_OFFSET] test r8, r8 - jz ft_list_sort_end + jz .ft_list_sort_end sub rsp, 16 push rdi push rsi @@ -124,16 +122,16 @@ ft_list_sort: add rsp, 16 ret -ft_list_sort_end: +.ft_list_sort_end: ret ;t_list* sorted_merge(t_list *left, t_list *right, int (*cmp)(void*, void*)) sorted_merge: test rdi, rdi - jz sorted_merge_ret_right + jz .sorted_merge_ret_right test rsi, rsi - jz sorted_merge_ret_left + jz .sorted_merge_ret_left ; result sub rsp, 8 mov qword [rsp + 0], 0 @@ -148,18 +146,18 @@ sorted_merge: pop rdi ; cmp only int32_t cmp eax, 0 - jle sorted_merge_below_or_equal - jmp sorted_merge_greater + jle .sorted_merge_below_or_equal + jmp .sorted_merge_greater -sorted_merge_ret_left: +.sorted_merge_ret_left: mov rax, rdi ret -sorted_merge_ret_right: +.sorted_merge_ret_right: mov rax, rsi ret -sorted_merge_below_or_equal: +.sorted_merge_below_or_equal: mov [rsp + 0], rdi push rdi push rsi @@ -171,9 +169,9 @@ sorted_merge_below_or_equal: pop rdi mov r8, [rsp + 0] mov [r8 + LIST_NEXT_OFFSET], rax - jmp sorted_merge_end + jmp .sorted_merge_end -sorted_merge_greater: +.sorted_merge_greater: mov [rsp + 0], rsi push rdi push rsi @@ -185,9 +183,9 @@ sorted_merge_greater: pop rdi mov r8, [rsp + 0] mov [r8 + LIST_NEXT_OFFSET], rax - jmp sorted_merge_end + jmp .sorted_merge_end -sorted_merge_end: +.sorted_merge_end: mov rax, [rsp + 0] add rsp, 8 ret @@ -195,31 +193,31 @@ sorted_merge_end: ;t_list* get_middle(t_list *head) get_middle: test rdi, rdi - jz get_middle_ret_head - jnz get_middle_loop + jz .get_middle_ret_head + jnz .get_middle_loop -get_middle_loop: +.get_middle_loop: sub rsp, 16 ; slow mov [rsp + 0], rdi ; fast mov [rsp + 8], rdi - jmp get_middle_loop_condition + jmp .get_middle_loop_condition -get_middle_loop_condition: +.get_middle_loop_condition: mov r8, [rsp + 8] mov r8, [r8 + LIST_NEXT_OFFSET] test r8, r8 - jnz get_middle_loop_condition2 - jmp get_middle_ret_slow + jnz .get_middle_loop_condition2 + jmp .get_middle_ret_slow -get_middle_loop_condition2: +.get_middle_loop_condition2: mov r8, [r8 + LIST_NEXT_OFFSET] test r8, r8 - jnz get_middle_loop_routine - jmp get_middle_ret_slow + jnz .get_middle_loop_routine + jmp .get_middle_ret_slow -get_middle_loop_routine: +.get_middle_loop_routine: mov r8, [rsp + 0] mov r8, [r8 + LIST_NEXT_OFFSET] mov [rsp + 0], r8 @@ -227,16 +225,16 @@ get_middle_loop_routine: mov r8, [r8 + LIST_NEXT_OFFSET] mov r8, [r8 + LIST_NEXT_OFFSET] mov [rsp + 8], r8 - jmp get_middle_loop_condition + jmp .get_middle_loop_condition -get_middle_ret_head: +.get_middle_ret_head: mov rax, rdi ret -get_middle_ret_slow: +.get_middle_ret_slow: mov rax, [rsp + 0] add rsp, 16 ret -end: +.end: ret diff --git a/src/ft_read.s b/src/ft_read.s index 7e992db..db60ad9 100644 --- a/src/ft_read.s +++ b/src/ft_read.s @@ -4,21 +4,19 @@ extern __errno_location global ft_read -section .note.GNU-stack - section .text ; ssize_t ft_read(int fd, void *buf, size_t count); ft_read: xor rax, rax syscall test rax, rax - js syscall_error + js .syscall_error ret -syscall_error: +.syscall_error: neg rax - mov rdx, rax + mov rdi, rax call __errno_location wrt ..plt - mov [rax], rdx + mov [rax], rdi mov rax, -1 ret \ No newline at end of file diff --git a/src/ft_strcmp.s b/src/ft_strcmp.s index 5b8e45a..89688a4 100644 --- a/src/ft_strcmp.s +++ b/src/ft_strcmp.s @@ -2,24 +2,23 @@ bits 64 global ft_strcmp -section .note.GNU-stack - section .text ; int ft_strcmp(const char *s1, const char *s2); ft_strcmp: xor rax, rax - xor rdx, rdx - xor rcx, rcx - jmp loop -loop: - mov al, [rdi + rcx] ; *s1 - mov dl, [rsi + rcx] ; *s2 - cmp al, 0 - je end_loop - cmp al, dl - jne end_loop - inc rcx - jmp loop -end_loop: - sub rax, rdx + xor r8, r8 + xor r9, r9 + jmp .loop +.loop: + mov r8b, [rdi + rax] ; *s1 + mov r9b, [rsi + rax] ; *s2 + cmp r8b, 0 + je .end_loop + cmp r8b, r9b + jne .end_loop + inc rax + jmp .loop +.end_loop: + sub r8, r9 + mov rax, r8 ret \ No newline at end of file diff --git a/src/ft_strcpy.s b/src/ft_strcpy.s index 0db11c9..5d8884f 100644 --- a/src/ft_strcpy.s +++ b/src/ft_strcpy.s @@ -2,23 +2,21 @@ bits 64 global ft_strcpy -section .note.GNU-stack - section .text ; char *ft_strcpy(char *dst, const char *src); ft_strcpy: - xor rcx, rcx - jmp loop + xor rax, rax + jmp .loop -loop: - cmp BYTE [rsi + rcx], 0 - je end_loop - mov dl, [rsi + rcx] - mov [rdi + rcx], dl - inc rcx - jmp loop +.loop: + cmp BYTE [rsi + rax], 0 + je .end_loop + mov r8b, [rsi + rax] + mov [rdi + rax], r8b + inc rax + jmp .loop -end_loop: - mov BYTE [rdi + rcx], 0 +.end_loop: + mov BYTE [rdi + rax], 0 mov rax, rdi ret \ No newline at end of file diff --git a/src/ft_strdup.s b/src/ft_strdup.s index 7b49d4e..00c525b 100644 --- a/src/ft_strdup.s +++ b/src/ft_strdup.s @@ -7,27 +7,25 @@ extern malloc global ft_strdup -section .note.GNU-stack - section .text ; char *ft_strdup(const char *s1); ft_strdup: - call ft_strlen push rdi + call ft_strlen mov rdi, rax inc rdi call malloc wrt ..plt cmp rax, 0 - je malloc_error + je .malloc_error mov rdi, rax pop rsi call ft_strcpy ret -malloc_error: +.malloc_error: neg rax - mov rbx, rax + mov rdi, rax call __errno_location wrt ..plt - mov [rax], rbx + mov [rax], rdi xor rax, rax ret diff --git a/src/ft_strlen.s b/src/ft_strlen.s index abd3e05..4c61f5f 100644 --- a/src/ft_strlen.s +++ b/src/ft_strlen.s @@ -2,17 +2,15 @@ bits 64 global ft_strlen -section .note.GNU-stack - section .text ; size_t ft_strlen(const char *str); ft_strlen: xor rax, rax - jmp loop -loop: + jmp .loop +.loop: cmp BYTE [rdi + rax], 0 - je end_loop + je .end_loop inc rax - jmp loop -end_loop: + jmp .loop +.end_loop: ret \ No newline at end of file diff --git a/src/ft_write.s b/src/ft_write.s index 412c126..b7cab90 100644 --- a/src/ft_write.s +++ b/src/ft_write.s @@ -4,21 +4,19 @@ extern __errno_location global ft_write -section .note.GNU-stack - section .text ; ssize_t ft_write(int fd, const void *buf, size_t count); ft_write: mov rax, 1 syscall test rax, rax - js syscall_error + js .syscall_error ret -syscall_error: +.syscall_error: neg rax - mov rdx, rax + mov rdi, rax call __errno_location wrt ..plt - mov [rax], rdx + mov [rax], rdi mov rax, -1 ret \ No newline at end of file