From b4b6364c2fc4b14a0aaac406891c3880243a9733 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 23 Nov 2019 02:54:37 +0100 Subject: refactored mandatory functions, to respect c calling convention --- ft_strcmp.s | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'ft_strcmp.s') diff --git a/ft_strcmp.s b/ft_strcmp.s index da8bc1c..5be82a5 100644 --- a/ft_strcmp.s +++ b/ft_strcmp.s @@ -12,33 +12,33 @@ global _ft_strcmp +; int ft_strcmp(const char *s1, const char *s2); _ft_strcmp: - mov rax, rdi - mov rbx, rsi - xor rcx, rcx + push r12 + push r13 + push rcx + mov r12, rdi ; s1 + mov r13, rsi ; s2 + mov rcx, -1 ; index FT_STRCMP_LOOP: - cmp byte [rax + rcx], 0 - je FT_STRCMP_LOOP_END - cmp byte [rbx + rcx], 0 - je FT_STRCMP_LOOP_END - mov dl, byte [rax + rcx] - cmp dl, byte [rbx + rcx] - jne FT_STRCMP_LOOP_END inc rcx - jmp FT_STRCMP_LOOP + cmp byte [r12 + rcx], 0 ; check and of s1 + je FT_STRCMP_LOOP_END + mov dl, byte [r12 + rcx] + cmp dl, byte [r13 + rcx] ; s1[rcx] == s2[rcx] + je FT_STRCMP_LOOP FT_STRCMP_LOOP_END: - mov dl, byte [rax + rcx] - cmp dl, byte [rbx + rcx] - jl FT_STRCMP_S1_LOWER - mov rdx, rax - xor rax, rax - mov al, [rdx + rcx] - sub al, [rbx + rcx] - ret - FT_STRCMP_S1_LOWER: - mov rdx, rax - xor rax, rax - mov al, [rbx + rcx] - sub al, [rdx + rcx] - neg eax + + xor rax, rax + mov al, byte [r12 + rcx] + sub al, byte [r13 + rcx] + jnc FT_STRCMP_END ; jump end if no substraction overflow + + neg al ; negate al to cancel overflow + neg eax ; negate the whole int since the function returns that type + + FT_STRCMP_END: + pop rcx + pop r13 + pop r12 ret -- cgit