Return value of a C function to ASM

Juan Pablo picture Juan Pablo · May 30, 2011 · Viewed 23.9k times · Source

I'm trying to call a function from within ASM. I know how to call it, but i'm having trouble finding how to get the return value of this function. An example follows:

C code:

int dummy() {  
    return 5;  
}  

(N)ASM code:

dummyFunction:
    call dummy
    ;grab return into eax
    inc eax ; eax should be 6 now
    ret  

Any ideas?

Answer

R.. GitHub STOP HELPING ICE picture R.. GitHub STOP HELPING ICE · May 30, 2011

The return value is in eax. If you've called a C function from asm, you can read the return value from eax. If you're trying to return from an asm function to C, store the intended return value in eax.

Things get a little bit more complicated for returning floating point values, long long values, or structures, so ask if you need that and someone (maybe me) will help you.