aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile23
-rw-r--r--ft_atoi_base.s83
-rw-r--r--ft_list_sort.s21
-rw-r--r--ft_read.s26
-rw-r--r--ft_strcmp.s46
-rw-r--r--ft_strcpy.s34
-rw-r--r--ft_strdup.s30
-rw-r--r--ft_strlen.s30
-rw-r--r--ft_write.s26
-rw-r--r--main.c9
10 files changed, 173 insertions, 155 deletions
diff --git a/Makefile b/Makefile
index 21ebf11..9eee32f 100644
--- a/Makefile
+++ b/Makefile
@@ -6,21 +6,23 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/22 02:56:22 by cacharle #+# #+# #
-# Updated: 2019/11/22 03:58:18 by cacharle ### ########.fr #
+# Updated: 2019/11/23 00:27:22 by cacharle ### ########.fr #
# #
# **************************************************************************** #
-NAME = libasm.a
+RM = rm -f
+LIB = ar rcs
CC = gcc
-CCFLAGS = -masm=intel -m64
+CCFLAGS = -Wall -Wextra
-ASMSRC = ft_strlen.s ft_strcpy.s ft_strcmp.s ft_write.s ft_read.s ft_strdup.s \
- ft_atoi_base.s
-ASMOBJ = $(ASMSRC:.s=.o)
+NASM = nasm
+NASMFLAGS = -f macho64
-RM = rm -f
-LIB = ar rcs
+NAME = libasm.a
+ASMSRC = ft_strlen.s ft_strcpy.s ft_strcmp.s ft_write.s ft_read.s \
+ ft_strdup.s ft_atoi_base.s
+ASMOBJ = $(ASMSRC:.s=.o)
all: $(NAME)
@@ -28,11 +30,10 @@ $(NAME): $(ASMOBJ)
$(LIB) $(NAME) $(ASMOBJ)
test: all
- $(CC) main.c -L. -lasm
+ $(CC) main.c $(NAME)
%.o: %.s
- $(CC) $(CCFLAGS) -c -o $@ $<
- @#nasm -f macho64 -o $@ $<
+ $(NASM) $(NASMFLAGS) -o $@ $<
clean:
$(RM) $(ASMOBJ)
diff --git a/ft_atoi_base.s b/ft_atoi_base.s
index 6245765..e51487c 100644
--- a/ft_atoi_base.s
+++ b/ft_atoi_base.s
@@ -1,25 +1,26 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# ft_atoi_base.s :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2019/11/22 03:59:15 by cacharle #+# #+# #
-# Updated: 2019/11/22 21:18:08 by cacharle ### ########.fr #
-# #
-# **************************************************************************** #
+; **************************************************************************** ;
+; ;
+; ::: :::::::: ;
+; ft_atoi_base.s :+: :+: :+: ;
+; +:+ +:+ +:+ ;
+; By: cacharle <marvin@42.fr> +;+ +:+ +;+ ;
+; +;+;+;+;+;+ +;+ ;
+; Created: 2019/11/22 03:59:15 by cacharle ;+; ;+; ;
+; Updated: 2019/11/22 21:18:08 by cacharle ;;; ;;;;;;;;.fr ;
+; ;
+; **************************************************************************** ;
-.globl _ft_atoi_base
-.globl _check_base
+extern _ft_strlen
-# int ft_atoi_base(const char*, const char*);
+global _ft_atoi_base
+
+; int ft_atoi_base(const char*, const char*);
_ft_atoi_base:
- push rbp # save previous stackframe
- mov rbp, rsp # create new one
+ push rbp ; save previous stackframe
+ mov rbp, rsp ; create new one
- mov rbx, rdi # rbx = str
- mov rcx, rsi # rcx = base
+ mov rbx, rdi ; rbx = str
+ mov rcx, rsi ; rcx = base
mov rdi, rcx
call _check_base
@@ -28,11 +29,11 @@ _ft_atoi_base:
mov rdi, rcx
call _ft_strlen
- #mov r8, rax # r8 = radix
- #mov rax, r8
+ ;mov r8, rax ; r8 = radix
+ ;mov rax, r8
- xor rdx, rdx # rdx = 0
- FT_ATOI_BASE_SPACE_LOOP: # remove spaces
+ xor rdx, rdx ; rdx = 0
+ FT_ATOI_BASE_SPACE_LOOP: ; remove spaces
mov rdi, [rbx]
call ft_isspace
cmp rax, 1
@@ -41,18 +42,18 @@ _ft_atoi_base:
jmp FT_ATOI_BASE_SPACE_LOOP
FT_ATOI_BASE_SPACE_LOOP_END:
- xor rax, rax # rax = 0
- xor rdx, rdx # rdx = 0
+ xor rax, rax ; rax = 0
+ xor rdx, rdx ; rdx = 0
FT_ATOI_BASE_LOOP:
- cmp byte ptr [rbx], 30h # while isdigit
- jl FT_ATOI_BASE_END # if *rbx < '0' jmp end
- cmp byte ptr [rbx], 39h
- ja FT_ATOI_BASE_END # if *rbx > '9' jmp end
+ cmp byte [rbx], 30h ; while isdigit
+ jl FT_ATOI_BASE_END ; if *rbx < '0' jmp end
+ cmp byte [rbx], 39h
+ ja FT_ATOI_BASE_END ; if *rbx > '9' jmp end
- imul eax, 10 # eax *= 10, shift previous digits
- mov dl, byte ptr [rbx]
- and dl, 0x0F # char to digit
- add eax, edx # insert as first digit
+ imul eax, 10 ; eax *= 10, shift previous digits
+ mov dl, byte [rbx]
+ and dl, 0x0F ; char to digit
+ add eax, edx ; insert as first digit
inc rbx
jmp FT_ATOI_BASE_LOOP
FT_ATOI_BASE_END:
@@ -63,9 +64,9 @@ _base_pos:
mov rcx, rsi
xor rax, rax
BASE_POS_LOOP:
- cmp byte ptr [rdi + rax], 0
+ cmp byte [rdi + rax], 0
je BASE_POS_NOT_FOUND
- cmp cl, byte ptr [rdi + rax]
+ cmp cl, byte [rdi + rax]
je BASE_POS_FOUND
inc eax
jmp BASE_POS_LOOP
@@ -78,23 +79,23 @@ _check_base:
push rbx
mov rbx, rsp
- mov rbx, rdi # rbx = str
+ mov rbx, rdi ; rbx = str
- call _ft_strlen # f strlen(rbx) < 2
+ call _ft_strlen ; f strlen(rbx) < 2
cmp eax, 2
jl CHECK_BASE_ERROR
CHECK_BASE_LOOP:
- cmp byte ptr [rbx], 2bh # *rbx == '-' || *rbx == '+'
+ cmp byte [rbx], 2bh ; *rbx == '-' || *rbx == '+'
je CHECK_BASE_ERROR
- cmp byte ptr [rbx], 2dh
+ cmp byte [rbx], 2dh
je CHECK_BASE_ERROR
call ft_isspace
cmp rax, 1
je CHECK_BASE_ERROR
inc rbx
- cmp byte ptr [rbx], 0h
+ cmp byte [rbx], 0h
jne CHECK_BASE_LOOP
mov rax, 1
pop rbx
@@ -105,9 +106,9 @@ _check_base:
ret
ft_isspace:
- cmp byte ptr [rdi], 20h # if space jump next
+ cmp byte [rdi], 20h ; if space jump next
je ISSPACE_TRUE_END
- mov dl, byte ptr [rdi] # if \t\n\r\v\f jump next
+ mov dl, byte [rdi] ; if \t\n\r\v\f jump next
sub dl, 9h
cmp dl, 5h
jl ISSPACE_TRUE_END
diff --git a/ft_list_sort.s b/ft_list_sort.s
index dc16183..1ab5485 100644
--- a/ft_list_sort.s
+++ b/ft_list_sort.s
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/11/22 21:03:52 by cacharle #+# #+# #
-# Updated: 2019/11/22 21:17:36 by cacharle ### ########.fr #
+# Updated: 2019/11/22 21:27:27 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -32,5 +32,20 @@ _ft_list_sort:
merge_sorted:
-
-
+#ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
+#{
+# ListNode *merged = NULL;
+#
+# if (l1 == NULL && l2 == NULL)
+# return NULL;
+# if (l1 == NULL)
+# return l2;
+# if (l2 == NULL)
+# return l1;
+# merged = l1->val < l2->val ? l1 : l2;
+# if (l1->val < l2->val)
+# merged->next = mergeTwoLists(l1->next, l2);
+# else
+# merged->next = mergeTwoLists(l1, l2->next);
+# return merged;
+#}
diff --git a/ft_read.s b/ft_read.s
index e249078..00718e8 100644
--- a/ft_read.s
+++ b/ft_read.s
@@ -1,18 +1,18 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# ft_read.s :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2019/11/22 03:04:44 by cacharle #+# #+# #
-# Updated: 2019/11/22 21:19:13 by cacharle ### ########.fr #
-# #
-# **************************************************************************** #
+; **************************************************************************** ;
+; ;
+; ::: :::::::: ;
+; ft_read.s :+: :+: :+: ;
+; +:+ +:+ +:+ ;
+; By: cacharle <marvin@42.fr> +;+ +:+ +;+ ;
+; +;+;+;+;+;+ +;+ ;
+; Created: 2019/11/22 03:04:44 by cacharle ;+; ;+; ;
+; Updated: 2019/11/22 21:19:13 by cacharle ;;; ;;;;;;;;.fr ;
+; ;
+; **************************************************************************** ;
-.globl _ft_read
+global _ft_read
-# int ft_read(int, void*, size_t);
+; int ft_read(int, void*, size_t);
_ft_read:
mov rax, 0x2000003
syscall
diff --git a/ft_strcmp.s b/ft_strcmp.s
index e04938a..da8bc1c 100644
--- a/ft_strcmp.s
+++ b/ft_strcmp.s
@@ -1,48 +1,44 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# ft_strcmp.s :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2019/11/22 03:04:38 by cacharle #+# #+# #
-# Updated: 2019/11/22 21:18:49 by cacharle ### ########.fr #
-# #
-# **************************************************************************** #
+; **************************************************************************** ;
+; ;
+; ::: :::::::: ;
+; ft_strcmp.s :+: :+: :+: ;
+; +:+ +:+ +:+ ;
+; By: cacharle <marvin@42.fr> +;+ +:+ +;+ ;
+; +;+;+;+;+;+ +;+ ;
+; Created: 2019/11/23 00:17:19 by cacharle ;+; ;+; ;
+; Updated: 2019/11/23 00:17:19 by cacharle ;;; ;;;;;;;;.fr ;
+; ;
+; **************************************************************************** ;
-.globl _ft_strcmp
+global _ft_strcmp
-# int ft_strcmp(const char *s1, const char *s2);
_ft_strcmp:
- mov rax, rdi # s1
- mov rbx, rsi # s2
+ mov rax, rdi
+ mov rbx, rsi
xor rcx, rcx
FT_STRCMP_LOOP:
- cmp byte ptr [rax + rcx], 0
+ cmp byte [rax + rcx], 0
je FT_STRCMP_LOOP_END
- cmp byte ptr [rbx + rcx], 0
+ cmp byte [rbx + rcx], 0
je FT_STRCMP_LOOP_END
- mov dl, byte ptr [rax + rcx]
- cmp dl, byte ptr [rbx + rcx]
+ mov dl, byte [rax + rcx]
+ cmp dl, byte [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]
+ mov dl, byte [rax + rcx]
+ cmp dl, byte [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
+ neg eax
ret
diff --git a/ft_strcpy.s b/ft_strcpy.s
index 820d970..1857933 100644
--- a/ft_strcpy.s
+++ b/ft_strcpy.s
@@ -1,27 +1,27 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# ft_strcpy.s :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2019/11/22 03:04:28 by cacharle #+# #+# #
-# Updated: 2019/11/22 21:18:38 by cacharle ### ########.fr #
-# #
-# **************************************************************************** #
+; **************************************************************************** ;
+; ;
+; ::: :::::::: ;
+; ft_strcpy.s :+: :+: :+: ;
+; +:+ +:+ +:+ ;
+; By: cacharle <marvin@42.fr> +;+ +:+ +;+ ;
+; +;+;+;+;+;+ +;+ ;
+; Created: 2019/11/22 03:04:28 by cacharle ;+; ;+; ;
+; Updated: 2019/11/22 21:18:38 by cacharle ;;; ;;;;;;;;.fr ;
+; ;
+; **************************************************************************** ;
-.globl _ft_strcpy
+global _ft_strcpy
-# char *ft_strcpy(char *dst, const char *src);
+; char *ft_strcpy(char *dst, const char *src);
_ft_strcpy:
- mov rax, rdi # dst
- mov rbx, rsi # src
+ 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
+ cmp byte [rbx + rcx], 0
jne FT_STRCPY_LOOP
- mov byte ptr [rax + rcx], 0
+ mov byte [rax + rcx], 0
ret
diff --git a/ft_strdup.s b/ft_strdup.s
index d96ad3b..fa28326 100644
--- a/ft_strdup.s
+++ b/ft_strdup.s
@@ -1,18 +1,22 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# ft_strdup.s :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2019/11/22 03:04:32 by cacharle #+# #+# #
-# Updated: 2019/11/22 21:19:29 by cacharle ### ########.fr #
-# #
-# **************************************************************************** #
+; **************************************************************************** ;
+; ;
+; ::: :::::::: ;
+; ft_strdup.s :+: :+: :+: ;
+; +:+ +:+ +:+ ;
+; By: cacharle <marvin@42.fr> +;+ +:+ +;+ ;
+; +;+;+;+;+;+ +;+ ;
+; Created: 2019/11/22 03:04:32 by cacharle ;+; ;+; ;
+; Updated: 2019/11/23 00:19:26 by cacharle ;;; ;;;;;;;;.fr ;
+; ;
+; **************************************************************************** ;
-.globl _ft_strdup
+extern _ft_strlen
+extern _ft_strcpy
+extern _malloc
-# char *ft_strdup(const char*);
+global _ft_strdup
+
+; char *ft_strdup(const char*);
_ft_strdup:
push rbp
mov rbp, rsp
diff --git a/ft_strlen.s b/ft_strlen.s
index 2965ada..319a321 100644
--- a/ft_strlen.s
+++ b/ft_strlen.s
@@ -1,23 +1,23 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# ft_strlen.s :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2019/11/22 03:04:20 by cacharle #+# #+# #
-# Updated: 2019/11/22 21:18:30 by cacharle ### ########.fr #
-# #
-# **************************************************************************** #
+; **************************************************************************** ;
+; ;
+; ::: :::::::: ;
+; ft_strlen.s :+: :+: :+: ;
+; +:+ +:+ +:+ ;
+; By: cacharle <marvin@42.fr> +;+ +:+ +;+ ;
+; +;+;+;+;+;+ +;+ ;
+; Created: 2019/11/22 03:04:20 by cacharle ;+; ;+; ;
+; Updated: 2019/11/23 00:17:47 by cacharle ;;; ;;;;;;;;.fr ;
+; ;
+; **************************************************************************** ;
-.globl _ft_strlen
+global _ft_strlen
-# int ft_strlen(char *);
+; int ft_strlen(char *);
_ft_strlen:
- mov rbx, rdi # str argument
+ mov rbx, rdi ; str argument
xor rax, rax
FT_STRLEN_LOOP:
- cmp byte ptr [rbx + rax], 0 # compare rbx[rax] and '\0'
+ cmp byte [rbx + rax], 0 ; compare rbx[rax] and '\0'
je FT_STRLEN_RET
inc rax
jmp FT_STRLEN_LOOP
diff --git a/ft_write.s b/ft_write.s
index 7303af0..c558ab6 100644
--- a/ft_write.s
+++ b/ft_write.s
@@ -1,18 +1,18 @@
-# **************************************************************************** #
-# #
-# ::: :::::::: #
-# ft_write.s :+: :+: :+: #
-# +:+ +:+ +:+ #
-# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
-# +#+#+#+#+#+ +#+ #
-# Created: 2019/11/22 03:04:47 by cacharle #+# #+# #
-# Updated: 2019/11/22 21:19:02 by cacharle ### ########.fr #
-# #
-# **************************************************************************** #
+; **************************************************************************** ;
+; ;
+; ::: :::::::: ;
+; ft_write.s :+: :+: :+: ;
+; +:+ +:+ +:+ ;
+; By: cacharle <marvin@42.fr> +;+ +:+ +;+ ;
+; +;+;+;+;+;+ +;+ ;
+; Created: 2019/11/22 03:04:47 by cacharle ;+; ;+; ;
+; Updated: 2019/11/22 21:19:02 by cacharle ;;; ;;;;;;;;.fr ;
+; ;
+; **************************************************************************** ;
-.globl _ft_write
+global _ft_write
-# int ft_write(int, const void*, size_t);
+; int ft_write(int, const void*, size_t);
_ft_write:
mov rax, 0x2000004
syscall
diff --git a/main.c b/main.c
index 0bf3354..3bf85d1 100644
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/22 02:02:24 by cacharle #+# #+# */
-/* Updated: 2019/11/22 21:00:30 by cacharle ### ########.fr */
+/* Updated: 2019/11/23 00:25:35 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,6 +30,7 @@ int check_base(const char*);
void ft_list_push_front(t_list **begin_list, void *data);
int ft_list_size(t_list *begin_list);
+void ft_list_sort(t_list **begin_list, int (*cmp)());
int main()
{
@@ -46,8 +47,8 @@ int main()
/* char *d = "bonjourasdfasdf"; */
/* printf("%s\n", ft_strcpy(c, d)); */
/* printf("%s\n", c); */
- /* */
- /* char *e = "\x03"; */
+
+ /* char *e = "\x01"; */
/* char *f = "\x02"; */
/* printf("%d\n", ft_strcmp(e, f)); */
/* printf("%d\n", strcmp(e, f)); */
@@ -66,6 +67,6 @@ int main()
/* printf("%d\n", check_base("01")); */
- printf("%d\n", ft_atoi_base(" \t\v\r \n 1012h", "01"));
+ /* printf("%d\n", ft_atoi_base(" \t\v\r \n 1012h", "01")); */
return 0;
}