「🏗️」 wip(str*): working on them, strcmp doesn't work the rest does i think
This commit is contained in:
32
src/str/ft_strcmp.s
Normal file
32
src/str/ft_strcmp.s
Normal file
@@ -0,0 +1,32 @@
|
||||
section .text
|
||||
global ft_strcmp
|
||||
|
||||
; int ft_strcmp(const char *s1, const char *s2)
|
||||
ft_strcmp:
|
||||
mov rax, 0
|
||||
cmp rdi, 0
|
||||
je .ret
|
||||
cmp rsi, 0
|
||||
je .ret
|
||||
|
||||
.loop:
|
||||
|
||||
mov r10b, BYTE [rdi]
|
||||
cmp r10b, BYTE [rsi]
|
||||
je .end
|
||||
|
||||
cmp BYTE [rdi], 0
|
||||
je .end
|
||||
cmp BYTE [rsi], 0
|
||||
je .end
|
||||
|
||||
inc rsi
|
||||
inc rdi
|
||||
jmp .loop
|
||||
|
||||
.end:
|
||||
mov al, BYTE [rsi]
|
||||
mov r10b, BYTE [rdi]
|
||||
sub rax, r10
|
||||
.ret:
|
||||
ret
|
||||
28
src/str/ft_strcpy.s
Normal file
28
src/str/ft_strcpy.s
Normal file
@@ -0,0 +1,28 @@
|
||||
section .text
|
||||
global ft_strcpy
|
||||
|
||||
; char *ft_strcpy(char *dest, const char *src)
|
||||
ft_strcpy:
|
||||
mov rax, 0
|
||||
cmp rdi, 0
|
||||
je .end
|
||||
cmp rsi, 0
|
||||
je .end
|
||||
|
||||
mov rax, rdi
|
||||
|
||||
.loop:
|
||||
mov r10b, BYTE [rsi]
|
||||
cmp r10b, 0
|
||||
je .end_loop
|
||||
|
||||
mov BYTE [rdi], r10b
|
||||
inc rsi
|
||||
inc rdi
|
||||
jmp .loop
|
||||
|
||||
.end_loop:
|
||||
mov BYTE [rdi], 0
|
||||
|
||||
.end:
|
||||
ret
|
||||
18
src/str/ft_strlen.s
Normal file
18
src/str/ft_strlen.s
Normal file
@@ -0,0 +1,18 @@
|
||||
section .text
|
||||
global ft_strlen
|
||||
|
||||
; size_t ft_strlen(const char *str)
|
||||
ft_strlen:
|
||||
mov rax, rdi
|
||||
cmp rdi, 0
|
||||
je .end
|
||||
|
||||
.loop:
|
||||
cmp BYTE [rax], 0
|
||||
je .end
|
||||
inc rax
|
||||
jmp .loop
|
||||
|
||||
.end:
|
||||
sub rax, rdi
|
||||
ret
|
||||
30
src/str/main.c
Normal file
30
src/str/main.c
Normal file
@@ -0,0 +1,30 @@
|
||||
int ft_strlen(char *str);
|
||||
char *ft_strcpy(char *dest, const char *src);
|
||||
int ft_strcmp(const char *s1, const char *s2);
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("\n--- ft_strlen ---\n");
|
||||
printf("arg : \"i love kissing girls\" | result : %d\n", ft_strlen("i love kissing girls"));
|
||||
printf("arg : NULL | result : %d\n", ft_strlen(NULL));
|
||||
printf("arg : \"\" | result : %d\n", ft_strlen(""));
|
||||
|
||||
printf("\n--- ft_strcpy ---\n");
|
||||
char *a1 = calloc(sizeof(char), 10);
|
||||
char *a2 = calloc(sizeof(char), 10);
|
||||
printf("arg : \"abcdefgh\" | result : %s\n", ft_strcpy(a1, "abcdefgh"));
|
||||
printf("arg : \"abcdefgh\" | result : %s\n", strcpy(a2, "abcdefgh"));
|
||||
printf("arg : \"67\" | result : %s\n", ft_strcpy(a1, "67"));
|
||||
printf("arg : \"67\" | result : %s\n", strcpy(a2, "67"));
|
||||
printf("arg : \"aaa\" (dest = NULL) | result : %s\n", ft_strcpy(NULL, "67"));
|
||||
printf("arg : NULL | result : %s\n", ft_strcpy(a2, NULL));
|
||||
|
||||
|
||||
printf("\n--- ft_strcmp ---\n");
|
||||
printf("arg : \"aaaaaa\" \"aaaa\" | result : %d (expected : %d)\n", ft_strcmp("aaaaaa", "aaaa"), strcmp("aaaaaa", "aaaa"));
|
||||
printf("arg : \"aaaa\" \"bb\" | result : %d (expected : %d)\n", ft_strcmp("aaaa", "bb"), strcmp("aaaa", "bb"));
|
||||
}
|
||||
Reference in New Issue
Block a user