views:

44

answers:

3

I'm write a simple graphic-based program in Assembly for learning purpose; for this, I intended to use either OpenGL or SDL. I'm trying to call OpenGL/SDL's function from assembly.

The problem is, unlike many assembly and OpenGL/SDL tutorials I found in the internet, the OpenGL/SDL in my machine apparently doesn't use C calling convention. I wrote a simple program in C, compile it to assembly (using -S switch), and apparently the assembly code that is generated by GCC calls the OpenGL/SDL functions by passing parameters in the registers instead of being pushed to the stack.

Now, the question is, how do I determine how to pass arguments to these OpenGL/SDL functions? That is, how do I figure out which argument corresponds to which registers?

Obviously since GCC can compile C code to call OpenGL/SDL, so therefore there must be a way to figure out the correspondence between function arguments and registers. In C calling conventions, the rule is easy, push parameters backwards and return value in eax/rax, I can simply read their C documentation and I can easily figure out how to pass the parameters. But how about these?

Is there a way to call OpenGL/SDL using C calling convention?

btw, I'm using yasm, with gcc/ld as the linker on Gentoo Linux amd64.

A: 

I believe this would help:

http://en.wikipedia.org/wiki/X86_calling_conventions#Borland_fastcall

Evaluating arguments from left to right, it passes three arguments via EAX, EDX, ECX. Remaining arguments are pushed onto the stack, also left to right.
Matias Valdenegro
+1  A: 

I wrote a simple program in C, compile it to assembly (using -S switch), and apparently the assembly code that is generated by GCC calls the OpenGL/SDL functions by passing parameters in the registers instead of being pushed to the stack.

It's perfectly normal: on x86-64, the registers are used as much as possible when passing parameters.

I find that this document has the most comprehensive information.

Bastien Léonard
I see, that makes sense, with x86-64's huge number of register, parameter passing through registers using fastcall would be much more convenient
Lie Ryan
+1  A: 

On a x86-64 linux system the standard x86-64 ABI convention is followed for function calls. In a nutshell:

  • The first six integer/pointer arguments are passed in rdi, rsi, rdx, rcx, r8, r9, in that order.
  • The first eight floating-point arguments are passed as scalars in xmm0-xmm7.
  • The remaining arguments that did not fit are pushed on the stack, in C order. The stack gets padded as needed to keep rsp aligned to 16 bytes.
slacker
thanks, that piece of info helped me figure out how to call all the functions I needed.
Lie Ryan