Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling SDL/OpenGL from Assembly code on Linux

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.

like image 269
Lie Ryan Avatar asked Aug 23 '26 23:08

Lie Ryan


2 Answers

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.
like image 91
slacker Avatar answered Aug 26 '26 22:08

slacker


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.

like image 39
Bastien Léonard Avatar answered Aug 26 '26 22:08

Bastien Léonard