passing function parameters between ASM and C
passing function parameters between ASM and C
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?
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:passing function parameters between ASM and C
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.
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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:passing function parameters between ASM and C
the c function pops the parameters in order I guess?
e.g.
That right?
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
Re:passing function parameters between ASM and C
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.