passing function parameters between ASM and C

Programming, for all ages and all languages.
Post Reply
stonedzealot

passing function parameters between ASM and C

Post 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?
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:passing function parameters between ASM and C

Post 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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
stonedzealot

Re:passing function parameters between ASM and C

Post 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?
Tim

Re:passing function parameters between ASM and C

Post 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.
Post Reply