Page 1 of 1

GCC calling convention

Posted: Wed Mar 20, 2002 8:22 am
by The_Legend
I want to call a C-Function from within an asm-function, and pass parameters to it, but I don't know the calling convention.

Re: GCC calling convention

Posted: Wed Mar 20, 2002 9:28 am
by Kernel Panic
You have to pass parameters right-to-left. That is, if you have a function:
int x(x1, x2, x3)
you have to do this:
push x3
push x2
push x1
call x
add $0x0C, %esp

In case you use NASM or TASM replace the last line with:
add esp,0Ch
Also, the GCC calling conventions assume (AFAIK) that ebx, esi, edi and ebp are preserved during the call (and the segment registers, of course, as well).

So.. it's rather easy.. you just have to push the arguments in reverse order and clean up the stack afterwards..