Skip to content

Commit

Permalink
Merge pull request #14 from tdameros/refactor
Browse files Browse the repository at this point in the history
refactor: asm functions
  • Loading branch information
tdameros committed Aug 8, 2024
2 parents f8e2fce + ec9a198 commit 1b1ec4d
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 149 deletions.
4 changes: 1 addition & 3 deletions src/ft_atoi_base.s
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/ft_create_elem.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ extern malloc

global ft_create_elem

section .note.GNU-stack

section .text
;typedef struct s_list
;{
Expand All @@ -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
5 changes: 0 additions & 5 deletions src/ft_list.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
bits 64

section .note.GNU-stack

;typedef struct s_list
;{
; void *data;
Expand All @@ -11,6 +9,3 @@ section .data
LIST_DATA_OFFSET EQU 0
LIST_NEXT_OFFSET EQU 8
LIST_SIZE EQU 16



4 changes: 2 additions & 2 deletions src/ft_list_push_front.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ extern ft_create_elem

global ft_list_push_front

section .note.GNU-stack

section .text
;typedef struct s_list
;{
Expand All @@ -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
Expand Down
70 changes: 30 additions & 40 deletions src/ft_list_remove_if.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ extern free

global ft_list_remove_if

section .note.GNU-stack

section .text
;typedef struct s_list
;{
Expand Down Expand Up @@ -45,24 +43,24 @@ 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
; current_node
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
12 changes: 5 additions & 7 deletions src/ft_list_size.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ bits 64

global ft_list_size

section .note.GNU-stack

section .text
;typedef struct s_list
;{
Expand All @@ -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
Loading

0 comments on commit 1b1ec4d

Please sign in to comment.