Added commentary

This commit is contained in:
2024-05-23 14:59:13 +02:00
parent b7a3b7140b
commit 44bb95ec7d
5 changed files with 37 additions and 35 deletions

View File

@@ -6,7 +6,7 @@
# By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/03/15 14:16:21 by vvaas #+# #+# #
# Updated: 2024/03/27 17:45:37 by vvaas ### ########.fr #
# Updated: 2024/05/23 14:42:18 by vvaas ### ########.fr #
# #
#******************************************************************************#
@@ -30,12 +30,15 @@ AR = ar
ARFLAG = rc
.s.o :
${CC} $< -o ${<:.s=.o} ${FLAGS}
@printf "\e[1;32m[compiling {"$(CC)"}...]\e[1;00m "$<"\n"
@${CC} $< -o ${<:.s=.o} ${FLAGS}
all: ${NAME}
${NAME}: ${OBJS}
${AR} ${ARFLAGS} ${NAME} ${OBJS}
@printf "\e[1;32m[linking {"$(CC)"}...]\e[1;00m "$@"\n"
@${AR} ${ARFLAGS} ${NAME} ${OBJS}
@printf "\e[1;32m[build finished]\e[1;00m\n"
clean:
rm -f ${OBJS}

View File

@@ -1,27 +1,27 @@
global ft_strcmp
ft_strcmp:
xor r8, r8
xor rax, rax
xor r8, r8 ; r8 = 0
xor rax, rax ; rax = 0
.count:
mov al, BYTE [rdi + r8]
mov dl, BYTE [rsi + r8]
test al, al
jz .end
test dl, dl
jz .end
cmp al, dl
jne .end
inc r8
jmp .count
mov al, BYTE [rdi + r8] ; al = s1[r8]
mov dl, BYTE [rsi + r8]; dl = s2[r8]
test al, al ; if (al == 0)
jz .end ; goto end
test dl, dl ; if (dl == 0)
jz .end ; goto end
cmp al, dl ; compare al and dl
jne .end ; if not equal, goto end
inc r8 ; r8++
jmp .count ; repeat
.end:
sub al, dl
sub al, dl ; take diff between al and dl
js .neg
ret
ret ; return it
.neg:
neg al
neg rax
ret
neg al ; al = -al;
neg rax ; al is the first 8 bit of rax, so reverse it
ret ; return it

View File

@@ -14,9 +14,9 @@ ft_strdup:
je .end
pop rsi ; we take the value we saved earlier
mov rdi, rax ; move the result of malloc into rdi (override strlen result)
call ft_strcpy
ret
call ft_strcpy ; copy the string
ret ; result is put into rax, return it
.end:
pop rsi
ret
pop rsi ; remove rsi from the stack
ret ; return

View File

@@ -1,13 +1,12 @@
global ft_strlen
ft_strlen:
xor rax, rax
xor rax, rax ; rax = 0;
.cmp_char:
cmp byte [rdi + rax], 0
je .end
inc rax
jmp .cmp_char
cmp byte [rdi + rax], 0 ; if (str[rax] == 0)
je .end ; goto end
inc rax ; rax++;
jmp .cmp_char ; repeat
.end:
ret
ret ; return it

View File

@@ -2,16 +2,16 @@ global ft_write
extern __errno_location
ft_write:
cmp rsi, 0
je .bad_buff
cmp rsi, 0 ; if buffer is null
je .bad_buff ; goto bad_buff
mov eax, 0x1 ; put the syscall number of write
syscall
syscall ; call write
test rax, rax ; test if the syscall failed
js .error
ret
.bad_buff:
mov rdx, 22
mov rdx, 22 ; put the corresponding errno code
call __errno_location wrt ..plt
mov [rax], rdx
mov rax, -1