clean: better comments and code

This commit is contained in:
gbrochar 2024-03-11 17:30:24 +01:00
parent 2419433d71
commit 8dbf43ef0c
8 changed files with 73 additions and 144 deletions

View File

@ -3,7 +3,6 @@ extern malloc ; for LUT
extern free ; for LUT
ft_atoi_base:
push rsp
push rbx
push r12 ; sign
push r13 ; base
@ -11,28 +10,28 @@ ft_atoi_base:
push r15 ; base LUT
push rdi
push rsi
mov rdi, 128
mov rdi, 256
call malloc WRT ..plt
mov r15, rax
pop rsi
pop rdi
push rdi
push rsi
cmp r15, 0
jz .malloc_error
xor rcx, rcx
.init_lut_loop: ; sets LUT to 0
cmp rcx, 128
cmp rcx, 256
je .set_base
mov byte [r15 + rcx], 0
mov byte [r15 + rcx], -1
inc rcx
jmp .init_lut_loop
.set_base:
xor rcx, rcx ; rcx is base length/count
.set_base_loop:
cmp byte [rsi + rcx], 0
cmp byte [rsi + rcx], 0 ; end of base
je .check_base_len
cmp byte [rsi + rcx], 43
cmp byte [rsi + rcx], 43 ; check forbidden chars
je .error
cmp byte [rsi + rcx], 45
je .error
@ -42,11 +41,10 @@ ft_atoi_base:
jl .error
xor rbx, rbx
mov bl, byte [rsi + rcx]
cmp byte [r15 + rbx], 0
cmp byte [r15 + rbx], -1 ; check double digits in base
jne .error
mov rdx, rcx
inc rdx
mov byte [r15 + rbx], dl
mov byte [r15 + rbx], dl ; set base number in LUT
inc rcx
jmp .set_base_loop
@ -76,12 +74,9 @@ ft_atoi_base:
je .neg_once
jmp .get_number_loop
.pos_once:
inc rcx
jmp .get_sign_loop
.neg_once:
neg r12
.pos_once:
inc rcx
jmp .get_sign_loop
@ -91,16 +86,19 @@ ft_atoi_base:
cmp bl, 0
je .done
mov bl, byte [r15 + rbx] ; set bl to base index
cmp bl, 0
cmp bl, -1
je .done
mov rax, r14
mul r13
mov r14, rax
dec rbx
add r14, rbx
inc rcx
jmp .get_number_loop
.malloc_error:
mov rax, 0
jmp .exit
.error:
mov r14, 0
@ -108,16 +106,13 @@ ft_atoi_base:
mov rdi, r15
call free WRT ..plt
mov rax, r14
cmp r12, 1
cmp r12, 1 ; check sign
je .exit
neg rax
.exit:
pop rsi
pop rdi
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rsp
ret

View File

@ -2,40 +2,18 @@ global ft_list_push_front
extern malloc
ft_list_push_front:
push rsp
push rdi
push rsi
mov rdi, 16
push rdi ; caller saved (begin_list)
push rsi ; caller saved (data)
mov rdi, 16 ; sizeof(t_list)
call malloc WRT ..plt
cmp rax, 0
je .done
pop rsi
pop rdi
mov [rax], rsi
mov rbx, [rdi]
mov [rax + 8], rbx
mov [rdi], rax
.done:
pop rsp
ret
pop rsi ; restore data
pop rdi ; restore begin_list
mov [rax], rsi ; new_node->data = data(rsi)
mov rdx, [rdi] ; rdx = *begin_list
mov [rax + 8], rdx ; new_node->next = *begin_list
mov [rdi], rax ; *begin_list = new_node
;ft_list_push_front:
; push ebp
; mov ebp, esp
; sub esp 0x10
; push rdi
; push rsi
; mov rdi, 16
; call malloc
; pop rsi
; pop rdi
; ;cmp rax, 0
; ;je .error
;
; mov rbx, [rdi]
; mov rbx, [rbx + 8]
; mov [rax], rsi
; mov [rax + 8], rbx
; mov [rdi], rax
; leave
; ret
.done:
ret

View File

@ -2,8 +2,7 @@ global ft_list_remove_if
extern free
ft_list_remove_if:
push rsp ; push callee saved registers
push rbx
push rbx ; push callee saved registers
push r12
push r13
push r14
@ -57,5 +56,4 @@ ft_list_remove_if:
pop r13
pop r12
pop rbx
pop rsp
ret

View File

@ -8,5 +8,6 @@ ft_list_size:
mov rdi, [rdi + 8]
inc rax
jmp .loop
.done:
ret

View File

@ -1,19 +1,16 @@
global ft_list_sort
ft_list_sort:
push rsp
push rdi ; push begin_list
push rsi ; push cmp_func
push r12 ; callee saved
push r13 ; same
push r14 ; same
push r15
push r12 ; callee saved registers
push r13
push r14
push r15
cmp qword [rdi], 0
je .done
mov r12, 0 ; r12 "prev" = NULL
mov r13, [rdi] ; r13 "curr" = begin_list
mov r14, rdi ; r14 begin_list memory
mov r15, rsi
mov r13, [rdi] ; r13 "curr" = *begin_list
mov r14, rdi ; r14 "begin_list"
mov r15, rsi ; r15 cmp function (avoids pop push in .loop)
.loop:
cmp qword [r13 + 8], 0 ; if curr->next is null then exit
@ -21,76 +18,42 @@ ft_list_sort:
mov rdi, [r13] ; first arg is curr->data
mov rsi, [r13 + 8] ; rsi = curr->next;
mov rsi, [rsi] ; rsi = rsi->data;
;push rcx ; rcx is caller saved
;push r13 ; same
; align stack here ?
sub rsp, 72; align stack ???
call r15
add rsp, 72
;pop rdx ; rdx is caller saved
;pop rcx ; rcx is caller saved
cmp eax, 0
jle .else ; if in order continue...
jle .next; if in order continue...
cmp r13, [r14]
je .is_first_elem
mov rdx, [r13 + 8]
mov [r12 + 8], rdx
mov [r12 + 8], rdx ; prev->next = curr->next
mov rcx, [r13 + 8]
mov rdx, [rcx + 8]
mov rdx, [rcx + 8] ; curr->next = curr->next->next
mov [r13 + 8], rdx
mov rcx, [r12 + 8]
mov [rcx + 8], r13
mov r12, 0
mov r13, [r14]
;mov rcx, [r13 + 8]
;mov rcx, [r13 + 8]
;mov rcx, [rcx + 8]
;mov [r13 + 8], rcx
;mov rcx, [r12 + 8]
;mov rcx, [rcx + 8]
;mov rcx, r13
;mov r12, 0, ; reset prev
;mov r13, [r14] ; reset curr
jmp .loop
.is_first_elem:
;mov r14, [r13 + 8] ; begin_list = curr->next
;mov rcx, [r13 + 8] ; rcx = curr->next
;mov rdx, [rcx + 8] ; rcx = curr->next->next
;mov [r13 + 8], rdx ; curr->next = curr->next->next
;mov [r14 + 8], r13 ; begin_list->next = curr
;mov rdx, [r13 + 8]
mov rcx, [r13 + 8]
mov [r14], rcx
;je .done
mov rcx, [r13 + 8]
mov rdx, [rcx + 8]
mov [r13 + 8], rdx
mov rcx, [r14]
mov [rcx + 8], r13
;jmp .done
mov [rcx + 8], r13 ; prev->next->next = curr
mov r12, 0 ; reset prev
mov r13, [r14] ; reset curr
jmp .loop
.else: ; ...here
.is_first_elem:
mov rdx, [r13 + 8]
mov [r14], rdx ; *begin_list = curr->next
mov rdx, [r13 + 8]
mov rcx, [rdx + 8]
mov [r13 + 8], rcx ; curr->next = curr->next->next
mov rdx, [r14]
mov [rdx + 8], r13 ; *begin_list->next = curr
mov r12, 0 ; reset prev
mov r13, [r14] ; reset curr
jmp .loop
.next:
mov r12, r13 ; prev = curr;
mov r13, [r13 + 8] ; curr = curr->next;
jmp .loop
.done:
pop r15
pop r15 ; restore stack and return
pop r14
pop r13 ; restore stack and return
pop r13
pop r12
pop rsi
pop rdi
pop rsp
ret

View File

@ -1,8 +1,6 @@
global ft_read
extern __errno_location
section .text
global ft_read
ft_read:
mov rax, 0 ; syscall read, other arguments should be good accord to calling convention
syscall
@ -10,10 +8,10 @@ ft_read:
jg .done
cmp eax, -4095
jl .done
mov rbx, rax ; save return value of syscall
mov rcx, rax ; save return value of syscall
call __errno_location WRT ..plt; put address of errno in rax
neg rbx ; errno should be positive
mov [rax], rbx ; set errno
neg rcx ; errno should be positive
mov [rax], rcx ; set errno
mov rax, -1 ; set return value of ft_read to -1
ret

View File

@ -1,25 +1,23 @@
global ft_strdup
extern my_test
extern malloc
extern ft_strlen
extern ft_strcpy
ft_strdup:
enter 16, 0
push rdi
enter 16, 0
push rdi ; save string to available register
call ft_strlen
mov rdi, rax
inc rdi
inc rdi ; null terminated
call malloc WRT ..plt
cmp rax, 0
jz .done
leave
mov rdi, rax
pop rsi
cmp rax, 0 ; if malloc failed
jz .error
mov rdi, rax ; store malloc adress to dest
pop rsi ; restore string address to source
call ft_strcpy
jmp .done
.error:
pop rsi ; restore stack bc we skipped line #15
.done:
leave
ret

View File

@ -1,8 +1,6 @@
global ft_write
extern __errno_location
section .text
global ft_write
ft_write:
mov rax, 1 ; syscall write, other arguments should be good accord to calling convention
syscall
@ -10,10 +8,10 @@ ft_write:
jg .done
cmp eax, -4095
jl .done
mov rbx, rax ; save return value of syscall
mov rcx, rax ; save return value of syscall
call __errno_location WRT ..plt ; put address of errno in rax
neg rbx ; errno should be positive
mov [rax], rbx ; set errno
neg rcx ; errno should be positive
mov [rax], rcx ; set errno
mov rax, -1 ; set return value of ft_write to -1
ret