aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--ft_read.s6
-rw-r--r--ft_strcmp.s41
-rw-r--r--ft_strcpy.s20
-rw-r--r--ft_strlen.s6
-rw-r--r--ft_write.s6
-rw-r--r--main.c50
7 files changed, 99 insertions, 31 deletions
diff --git a/.gitignore b/.gitignore
index 1a687bd..36340e5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
*.o
a.out
+*.dSYM
diff --git a/ft_read.s b/ft_read.s
index e69de29..c40aeaa 100644
--- a/ft_read.s
+++ b/ft_read.s
@@ -0,0 +1,6 @@
+.globl _ft_read
+
+_ft_read:
+ mov rax, 0x2000003
+ syscall
+ ret
diff --git a/ft_strcmp.s b/ft_strcmp.s
index 4c5aee7..216b56a 100644
--- a/ft_strcmp.s
+++ b/ft_strcmp.s
@@ -1,12 +1,35 @@
+.globl _ft_strcmp
+
_ft_strcmp:
- mov eax, sp
- mov ebx, sp + 8
+ mov rax, rdi # s1
+ mov rbx, rsi # s2
+ xor rcx, rcx
FT_STRCMP_LOOP:
- cmp eax, 0
- jneq FT_STRCMP_LOOP
- cmp ebx, 0
- jneq FT_STRCMP_LOOP
- cmp eax, ebx
- jeq FT_STRCMP_LOOP
- sub eax, ebx
+ cmp byte ptr [rax + rcx], 0
+ je FT_STRCMP_LOOP_END
+ cmp byte ptr [rbx + rcx], 0
+ je FT_STRCMP_LOOP_END
+ mov dl, byte ptr [rax + rcx]
+ cmp dl, byte ptr [rbx + rcx]
+ jne FT_STRCMP_LOOP_END
+ inc rcx
+ jmp FT_STRCMP_LOOP
+ FT_STRCMP_LOOP_END:
+ mov dl, byte ptr [rax + rcx]
+ cmp dl, byte ptr [rbx + rcx]
+ jl FT_STRCMP_S1_LOWER
+ # s1 >= s2
+ mov rdx, rax
+ xor rax, rax
+ mov al, [rdx + rcx]
+ sub al, [rbx + rcx]
+ ret
+ # s1 < s2
+ FT_STRCMP_S1_LOWER:
+ mov rdx, rax
+ xor rax, rax
+ mov al, [rbx + rcx]
+ sub al, [rdx + rcx]
+ not eax # two's complement
+ inc eax
ret
diff --git a/ft_strcpy.s b/ft_strcpy.s
index 2b7bbb9..228ea04 100644
--- a/ft_strcpy.s
+++ b/ft_strcpy.s
@@ -1,11 +1,13 @@
+.globl _ft_strcpy
+
_ft_strcpy:
- pop ax
- pop bx
- mov ecx, eax ; copy
-FT_STRCPY_LOOP:
- mov [ecx], ebx
- inc ebx
- inc ecx
- cmp ebx, 0
- jneq FT_STRCPY_LOOP
+ mov rax, rdi # dst
+ mov rbx, rsi # src
+ xor rcx, rcx
+ FT_STRCPY_LOOP:
+ mov dl, [rbx + rcx]
+ mov [rax + rcx], dl
+ inc rcx
+ cmp byte ptr [rbx + rcx], 0
+ jne FT_STRCPY_LOOP
ret
diff --git a/ft_strlen.s b/ft_strlen.s
index 82c0e34..c548d73 100644
--- a/ft_strlen.s
+++ b/ft_strlen.s
@@ -1,8 +1,8 @@
-.globl _ft_strlen
+.globl _ft_strlen
_ft_strlen:
- mov rbx, rdi # first argument in rbx
- xor rax, rax # rax = 0
+ mov rbx, rdi # str argument
+ xor rax, rax
FT_STRLEN_LOOP:
cmp byte ptr [rbx + rax], 0 # compare rbx[rax] and '\0'
je FT_STRLEN_RET
diff --git a/ft_write.s b/ft_write.s
index a21ec45..9cd1d3f 100644
--- a/ft_write.s
+++ b/ft_write.s
@@ -1,4 +1,6 @@
+.globl _ft_write
+
_ft_write:
- mov eax, 4
- int 0x80
+ mov rax, 0x2000004
+ syscall
ret
diff --git a/main.c b/main.c
index fac04a4..fccd80d 100644
--- a/main.c
+++ b/main.c
@@ -1,23 +1,57 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* main.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/22 02:02:24 by cacharle #+# #+# */
+/* Updated: 2019/11/22 02:14:53 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
#include <stdio.h>
int ft_strlen(char *);
+char *ft_strcpy(char *dst, const char *src);
+int ft_strcmp(const char *s1, const char *s2);
+int ft_write(int, const void*, size_t);
+int ft_read(int, void*, size_t);
-/* extern void test(); */
int main()
{
- /* extern say_hi(); */
- char *a = "";
- char *b = "a";
+ /* char *a = ""; */
+ /* char *b = "a"; */
/* printf("%d\n", sizeof(char*)); */
/* printf("a: %p\n", (void*)a); */
/* printf("b: %p\n", (void*)a); */
/* extern ft_strlen("bonjour"); */
- printf("%d\n", ft_strlen(a));
- printf("%d\n", ft_strlen(b));
- printf("%d\n", ft_strlen("bonjour"));
+ /* printf("%d\n", ft_strlen(a)); */
+ /* printf("%d\n", ft_strlen(b)); */
+ /* printf("%d\n", ft_strlen("bonjour")); */
+
+
+ /* char c[32] = "bon"; */
+ /* char *d = "bonjourasdfasdf"; */
+ /* */
+ /* printf("%s\n", ft_strcpy(c, d)); */
+ /* printf("%s\n", c); */
+
+
+ char *e = "\x03";
+ 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]; */
+ /* int ret = ft_read(0, g, 2); */
+ /* g[ret] = 0; */
+ /* printf("%s\n", g); */
+
return 0;
}
-