在 Linux 上从汇编代码调用 SDL/OpenGL

发布于 2024-09-05 18:44:23 字数 553 浏览 7 评论 0原文

我正在汇编中编写一个简单的基于图形的程序以用于学习目的;为此,我打算使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撧情箌佬 2024-09-12 18:44:23

在 x86-64 Linux 系统上,函数调用遵循标准 x86-64 ABI 约定。简而言之:

  • 前六个整数/指针参数按 rdi、rsi、rdx、rcx、r8、r9 的顺序传递。
  • 前八个浮点参数作为 xmm0-xmm7 中的标量传递。
  • 剩余的不适合的参数按 C 顺序压入堆栈。堆栈根据需要进行填充,以保持 rsp 与 16 字节对齐。

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.
紫竹語嫣☆ 2024-09-12 18:44:23

我用 C 语言编写了一个简单的程序,将其编译为汇编语言(使用 -S 开关),显然 GCC 生成的汇编代码通过在寄存器中传递参数来调用 OpenGL/SDL 函数,而不是被推送到堆栈。

这是完全正常的:在 x86-64 上,寄存器的使用量与传递参数时可能。

我发现这个文档拥有最全面的信息。

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.

情话难免假 2024-09-12 18:44:23

我相信这会有所帮助:

http://en.wikipedia.org/wiki/X86_calling_conventions#Borland_fastcall< /a>

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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文