GCC calling convention

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
The_Legend

GCC calling convention

Post 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.
Kernel Panic

Re: GCC calling convention

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