Let’s create two c language files,
//test.c
#include
int main(void)
{
printf("%lld\n", myNumber());
}
//func.c
#include
long long myNumber(void)
{
long long hash = -3524413842735980669;
printf("%lld\n", hash);
return hash;
}
and compile & link them:
gcc -c test.c -o test.o gcc -c func.c -o func.o gcc test.o func.o -o test
But after run “./test”, the result is
-3524413842735980669 2118164355
Why the result from myNumber() become different in main() function?
Let’s see the assembly language source of test.c (gcc -S test.c)
......
call myNumber
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
......
It only get the 32-bits result from function ‘myNumber’ (The size of %eax register is just 32-bits, smaller than the size of “long long”). Actually, we missed the declaration of myNumber() in test.c file so it only consider the result of myNumber() as 32-bits size.
After adding the declaration of myNumber() into test.c, we could check the assembly language source has changed:
......
call myNumber
movq %rax, %rsi
movl $.LC0, %edi
movl $0, %eax
call printf
(The size of %rax register is 64-bits)
And the result of running ‘./test’ is correct now.
-3524413842735980669 -3524413842735980669