Page 1 of 1
passing function parameters between ASM and C
Posted: Fri Mar 21, 2003 2:37 pm
by stonedzealot
As far as I can tell, the parameters of a function in ASM called in C are pushed to EBP starting at [EBP + 8] and just follow sequentially by size after that. I'm not too sure of this, but I think that's right. The real thing I'm wondering about it the other way around, passing parameters to a C function called in ASM. I suppose it would just be pushing the parameters to EBP and the function will automatically pick them up...is this anywhere close to the truth?
Re:passing function parameters between ASM and C
Posted: Fri Mar 21, 2003 3:29 pm
by distantvoices
parameter passing to an asm function:
push ebp
mov ebp,esp ;stack pointer to ebp
mov eax,[ebp+8] ;fetch first parameter
... and so on
pop ebp
ret
passing parameters to a c function:
extern c_function
push parameter
call c_function
pop parameter to some unused register(say ecx) in order to get it off the stack.
-> the c function does what is described above too, if you look at it in the compiler listing (I don't know how you call this in c - on cobol mainframe programming you get an assembled listing of your code as an result of a compiler run amongst other things) you will discover the parameter passing policy of standard c.
Re:passing function parameters between ASM and C
Posted: Fri Mar 21, 2003 5:57 pm
by stonedzealot
the c function pops the parameters in order I guess?
e.g.
Code: Select all
void main(int X, int Y)
{
...
}
then in the ASM it would be:
push valuefory
push valueforx
call _main
pop ecx
pop ecx
That right?
Re:passing function parameters between ASM and C
Posted: Sat Mar 22, 2003 4:26 am
by Tim
Right, except that if you do [tt]add esp, 4[/tt] you can save some instructions and avoid overwriting some registers. This is what the compiler does.