在 Linux 上从汇编代码调用 SDL/OpenGL
我正在汇编中编写一个简单的基于图形的程序以用于学习目的;为此,我打算使用 OpenGL 或 SDL。我正在尝试从程序集中调用 OpenGL/SDL 的函数。
问题是,与我在互联网上找到的许多汇编和 OpenGL/SDL 教程不同,我的机器中的 OpenGL/SDL 显然不使用 C 调用约定。我用 C 编写了一个简单的程序,将其编译为汇编(使用 -S 开关),显然 GCC 生成的汇编代码通过在寄存器中传递参数而不是被推送到堆栈来调用 OpenGL/SDL 函数。
现在的问题是,我如何确定如何将参数传递给这些 OpenGL/SDL 函数?也就是说,我如何找出哪个参数对应于哪个寄存器?
显然,由于GCC可以编译C代码来调用OpenGL/SDL,因此必须有一种方法来弄清楚函数参数和寄存器之间的对应关系。在C调用约定中,规则很简单,将参数向后推并在eax/rax中返回值,我只需阅读他们的C文档,就可以轻松弄清楚如何传递参数。但这些又如何呢?
有没有办法使用 C 调用约定来调用 OpenGL/SDL?
顺便说一句,我在 Gentoo Linux amd64 上使用 yasm,并使用 gcc/ld 作为链接器。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 x86-64 Linux 系统上,函数调用遵循标准 x86-64 ABI 约定。简而言之:
On a x86-64 linux system the standard x86-64 ABI convention is followed for function calls. In a nutshell:
这是完全正常的:在 x86-64 上,寄存器的使用量与传递参数时可能。
我发现这个文档拥有最全面的信息。
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.
我相信这会有所帮助:
http://en.wikipedia.org/wiki/X86_calling_conventions#Borland_fastcall< /a>
I believe this would help:
http://en.wikipedia.org/wiki/X86_calling_conventions#Borland_fastcall