Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value of a C function to ASM

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?

like image 720
Juan Pablo Avatar asked May 29 '11 23:05

Juan Pablo


2 Answers

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.

like image 178
R.. GitHub STOP HELPING ICE Avatar answered Sep 28 '22 11:09

R.. GitHub STOP HELPING ICE


Although the answers are sufficient to answer the OP's question, here's an extract covering most cases from DJPP's manpage for completeness:

Return Value

  • Integers (of any size up to 32 bits) and pointers are returned in the %eax register.
  • Floating point values are returned in the 387 top-of-stack register, st(0).
  • Return values of type long long int are returned in %edx:%eax (the most significant word in %edx and the least significant in %eax).
  • Returning a structure is complicated and rarely useful; try to avoid it. (Note that this is different from returning a pointer to a structure.)

If your function returns void (e.g. no value), the contents of these registers are not used.

like image 20
legends2k Avatar answered Sep 28 '22 11:09

legends2k