feat: ft_strcmp
This commit is contained in:
parent
ff8d51b1c4
commit
f787b174b1
1
Makefile
1
Makefile
|
@ -3,6 +3,7 @@ NAME = libasm
|
||||||
SRC = \
|
SRC = \
|
||||||
ft_strlen.s \
|
ft_strlen.s \
|
||||||
ft_strcpy.s \
|
ft_strcpy.s \
|
||||||
|
ft_strcmp.s \
|
||||||
|
|
||||||
OBJ = $(SRC:.s=.o)
|
OBJ = $(SRC:.s=.o)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
global ft_strcmp
|
||||||
|
|
||||||
|
ft_strcmp:
|
||||||
|
xor rdx, rdx
|
||||||
|
xor rax, rax
|
||||||
|
.loop:
|
||||||
|
mov al, [rsi + rdx]
|
||||||
|
cmp byte [rdi + rdx], al
|
||||||
|
jne .done
|
||||||
|
cmp byte [rdi + rdx], 0
|
||||||
|
je .done
|
||||||
|
inc rdx
|
||||||
|
jmp .loop
|
||||||
|
.done:
|
||||||
|
movsx rax, byte [rdi + rdx]
|
||||||
|
movsx rbx, byte [rsi + rdx]
|
||||||
|
sub rax, rbx
|
||||||
|
ret
|
||||||
|
|
1
libasm.h
1
libasm.h
|
@ -5,5 +5,6 @@
|
||||||
|
|
||||||
size_t ft_strlen(const char *);
|
size_t ft_strlen(const char *);
|
||||||
char *ft_strcpy(char *, const char *);
|
char *ft_strcpy(char *, const char *);
|
||||||
|
int ft_strcmp(const char *, const char *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
11
main.c
11
main.c
|
@ -2,7 +2,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
printf("%ld\n", ft_strlen("c'est moi la chaine de 36 caracteres"));
|
char my_string[37] = "c'est moi la chaine de 36 caracteres";
|
||||||
|
printf("%ld\n", ft_strlen(my_string));
|
||||||
|
|
||||||
char *str;
|
char *str;
|
||||||
str = (char *)malloc(150 * sizeof(char));
|
str = (char *)malloc(150 * sizeof(char));
|
||||||
|
@ -13,5 +14,13 @@ int main() {
|
||||||
printf("%s\n", str);
|
printf("%s\n", str);
|
||||||
printf("%s\n", dst);
|
printf("%s\n", dst);
|
||||||
printf("%s\n", test);
|
printf("%s\n", test);
|
||||||
|
printf("should be 0: %d\n", ft_strcmp(str, dst));
|
||||||
|
printf("should be 0 and not segv: %d\n", ft_strcmp(dst, test));
|
||||||
|
printf("should be 16 %d\n", ft_strcmp(my_string, str));
|
||||||
|
printf("should be -16 %d\n", ft_strcmp(str, my_string));
|
||||||
|
dst = "Salut c'est moi la diff mdr\0";
|
||||||
|
printf("should be 22: %d\n", ft_strcmp(str, dst));
|
||||||
|
dst = "Salut\0 c'est moi la diff mdr\0";
|
||||||
|
printf("should be 32: %d\n", ft_strcmp(str, dst));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue