I am trying to implement a sign function in assembly (64 bit) and then calling it from C.
My function takes in one argument: an unsigned int and returns an int, which can be -1 (if the argument is negative), 0 (if the argument is 0), and 1 (if the argument is positive).
My program works well for detecting positive numbers and zero but if I pass a negative number as the argument then it handles it as a positive one and returns 1.
main.c:
#include <stdio.h>
extern int sign(signed int);
int main()
{
signed int num;
scanf("%d",&num);
printf("num: %d sign: %d\n",num,sign(num));
return 0;
}
sign.asm:
global sign
section .text
sign:
test rdi,rdi
js neg_lab
je zero_lab
mov rax,1
jmp end_lab
neg_lab:
mov rax,-1
jmp end_lab
zero_lab:
xor rax,rax
end_lab:
ret
At first I thought that the problem was in the assembly code, so I set the tested register's value to a positive number then a negative number and then finally zero. It worked well. It recognized negative numbers too.
After that, I now think the problem is with the passing of arguments. I think the sign of the arguments is getting lost somewhere, or that the C way of storing negative numbers is just not compatible with assembly?
I would appreciate any help. Thank you in advance!