diff --git a/Makefile b/Makefile index d00e6cf..eda4420 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ NAME = libasm SRC = \ ft_strlen.s \ ft_strcpy.s \ + ft_strcmp.s \ OBJ = $(SRC:.s=.o) diff --git a/ft_strcmp.s b/ft_strcmp.s new file mode 100644 index 0000000..99f718f --- /dev/null +++ b/ft_strcmp.s @@ -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 + diff --git a/libasm.h b/libasm.h index dc1a5d6..c4bf3cc 100644 --- a/libasm.h +++ b/libasm.h @@ -5,5 +5,6 @@ size_t ft_strlen(const char *); char *ft_strcpy(char *, const char *); +int ft_strcmp(const char *, const char *); #endif diff --git a/main.c b/main.c index 7b577ba..ee9eaf4 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,8 @@ #include 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; str = (char *)malloc(150 * sizeof(char)); @@ -13,5 +14,13 @@ int main() { printf("%s\n", str); printf("%s\n", dst); 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; }