Search code examples
cfunctionassembly64-bitcall

Calling ASM x64 function from C (double), GAS


I've got this really simple functions in ASM and C. I want to call ASM function from C code for double. I think return value from ASM should be stored in XMM0, but what actually happen is that my return value is taken from rax or if rax isn't set, I get 1.

C code:

#include <stdio.h>

int main() {
double a = 3.14;
double b = add(a);
printf("%lf\n", b);

return 0;
}

ASM function:

.type add, @function

.globl add
add:

#movq $1, %rax
addsd %XMM0, %XMM0

ret

What's wrong with it? Appreciate all hints.


Solution

  • You haven't told the compiler what the function takes in or returns. Implicit declaration will make it assume return value of int.

    The compiler should warn you about this. If it doesn't, turn the warnings up.

    You should add

    extern double add(double val);
    

    so the compiler knows what's up.