aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--ft_strcmp.s50
-rw-r--r--ft_strcpy.s11
-rw-r--r--ft_strdup.s21
-rw-r--r--ft_strlen.s13
-rw-r--r--main.c15
6 files changed, 58 insertions, 56 deletions
diff --git a/Makefile b/Makefile
index 9eee32f..c30f961 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/22 02:56:22 by cacharle #+# #+# #
-# Updated: 2019/11/23 00:27:22 by cacharle ### ########.fr #
+# Updated: 2019/11/23 02:31:39 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -14,7 +14,7 @@ RM = rm -f
LIB = ar rcs
CC = gcc
-CCFLAGS = -Wall -Wextra
+CCFLAGS = -Wall -Wextra -fomit-frame-pointer
NASM = nasm
NASMFLAGS = -f macho64
diff --git a/ft_strcmp.s b/ft_strcmp.s
index da8bc1c..5be82a5 100644
--- a/ft_strcmp.s
+++ b/ft_strcmp.s
@@ -12,33 +12,33 @@
global _ft_strcmp
+; int ft_strcmp(const char *s1, const char *s2);
_ft_strcmp:
- mov rax, rdi
- mov rbx, rsi
- xor rcx, rcx
+ push r12
+ push r13
+ push rcx
+ mov r12, rdi ; s1
+ mov r13, rsi ; s2
+ mov rcx, -1 ; index
FT_STRCMP_LOOP:
- cmp byte [rax + rcx], 0
- je FT_STRCMP_LOOP_END
- cmp byte [rbx + rcx], 0
- je FT_STRCMP_LOOP_END
- mov dl, byte [rax + rcx]
- cmp dl, byte [rbx + rcx]
- jne FT_STRCMP_LOOP_END
inc rcx
- jmp FT_STRCMP_LOOP
+ cmp byte [r12 + rcx], 0 ; check and of s1
+ je FT_STRCMP_LOOP_END
+ mov dl, byte [r12 + rcx]
+ cmp dl, byte [r13 + rcx] ; s1[rcx] == s2[rcx]
+ je FT_STRCMP_LOOP
FT_STRCMP_LOOP_END:
- mov dl, byte [rax + rcx]
- cmp dl, byte [rbx + rcx]
- jl FT_STRCMP_S1_LOWER
- mov rdx, rax
- xor rax, rax
- mov al, [rdx + rcx]
- sub al, [rbx + rcx]
- ret
- FT_STRCMP_S1_LOWER:
- mov rdx, rax
- xor rax, rax
- mov al, [rbx + rcx]
- sub al, [rdx + rcx]
- neg eax
+
+ xor rax, rax
+ mov al, byte [r12 + rcx]
+ sub al, byte [r13 + rcx]
+ jnc FT_STRCMP_END ; jump end if no substraction overflow
+
+ neg al ; negate al to cancel overflow
+ neg eax ; negate the whole int since the function returns that type
+
+ FT_STRCMP_END:
+ pop rcx
+ pop r13
+ pop r12
ret
diff --git a/ft_strcpy.s b/ft_strcpy.s
index 1857933..5d32f2d 100644
--- a/ft_strcpy.s
+++ b/ft_strcpy.s
@@ -14,14 +14,17 @@ global _ft_strcpy
; char *ft_strcpy(char *dst, const char *src);
_ft_strcpy:
+ push rbx
+ push rcx
mov rax, rdi ; dst
mov rbx, rsi ; src
- xor rcx, rcx
+ mov rcx, -1
FT_STRCPY_LOOP:
- mov dl, [rbx + rcx]
- mov [rax + rcx], dl
inc rcx
+ mov dl, byte [rbx + rcx]
+ mov byte [rax + rcx], dl
cmp byte [rbx + rcx], 0
jne FT_STRCPY_LOOP
- mov byte [rax + rcx], 0
+ pop rcx
+ pop rbx
ret
diff --git a/ft_strdup.s b/ft_strdup.s
index fa28326..8ff46b5 100644
--- a/ft_strdup.s
+++ b/ft_strdup.s
@@ -16,20 +16,19 @@ extern _malloc
global _ft_strdup
-; char *ft_strdup(const char*);
+; char *ft_strdup(const char *str);
_ft_strdup:
- push rbp
- mov rbp, rsp
- mov rbx, rdi
- mov rax, rbx
- call _ft_strlen
- inc rax
- mov rdi, rax
+ push rdi ; save rdi because it will be overwrite for malloc
+
+ call _ft_strlen ; rdi is still == str
+ inc rax ; len++ for '\0'
+
+ mov rdi, rax ; size to malloc
call _malloc
- mov rdi, rax
- mov rsi, rbx
+
+ pop rsi ; original str as src
+ mov rdi, rax ; allocated as dest
call _ft_strcpy
- pop rbp
ret
diff --git a/ft_strlen.s b/ft_strlen.s
index 319a321..41372eb 100644
--- a/ft_strlen.s
+++ b/ft_strlen.s
@@ -14,12 +14,9 @@ global _ft_strlen
; int ft_strlen(char *);
_ft_strlen:
- mov rbx, rdi ; str argument
- xor rax, rax
+ mov eax, -1
FT_STRLEN_LOOP:
- cmp byte [rbx + rax], 0 ; compare rbx[rax] and '\0'
- je FT_STRLEN_RET
- inc rax
- jmp FT_STRLEN_LOOP
- FT_STRLEN_RET:
- ret
+ inc eax
+ cmp byte [rdi + rax], 0 ; compare rbx[rax] and '\0'
+ jne FT_STRLEN_LOOP
+ ret
diff --git a/main.c b/main.c
index 3bf85d1..ff6ce3c 100644
--- a/main.c
+++ b/main.c
@@ -6,12 +6,13 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/22 02:02:24 by cacharle #+# #+# */
-/* Updated: 2019/11/23 00:25:35 by cacharle ### ########.fr */
+/* Updated: 2019/11/23 02:13:29 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
typedef struct s_list
{
@@ -44,7 +45,9 @@ int main()
/* printf("%d\n", ft_strlen("bonjour")); */
/* char c[32] = "bon"; */
- /* char *d = "bonjourasdfasdf"; */
+ /* char *d = "b"; */
+ /* */
+ /* printf("%s\n", c); */
/* printf("%s\n", ft_strcpy(c, d)); */
/* printf("%s\n", c); */
@@ -52,7 +55,7 @@ int main()
/* char *f = "\x02"; */
/* printf("%d\n", ft_strcmp(e, f)); */
/* printf("%d\n", strcmp(e, f)); */
- /* */
+
/* ft_write(1, "bon\n", 4); */
/* */
/* char g[32]; */
@@ -60,9 +63,9 @@ int main()
/* g[ret] = 0; */
/* printf("%s\n", g); */
/* */
- /* char *h = ft_strdup("bonjour"); */
- /* printf("%s\n", h); */
- /* free(h); */
+ char *h = ft_strdup("bonjour");
+ printf("%s\n", h);
+ free(h);
/* printf("%d\n", check_base("01")); */