x86 汇编:主要的序言和尾声是什么?

发布于 2024-08-10 13:49:22 字数 380 浏览 10 评论 0原文

我正在关注关于 x86 程序集的 本教程。到目前为止,每个示例都使用作者所说的“c 驱动程序”程序,用汇编模块编译,以进行某种“初始化”。类似于:

int main(void) {
  int ret = asm_main();
  return ret;
}

然后使用 C 调用约定正常编写 asm_main 函数。我想知道 C 编译器生成的所需初始化到底是什么,以及是否可以以可移植的方式完成。

信息:我使用的是 Windows XP、32 位机器,使用 NASM 汇编器和 mingw32-gcc 进行链接。

I'm following this tutorial on x86 assembly. Every example so far uses what the author calls a "c-driver" program, compiled with the assembly module, for means of some "initialization". Something like:

int main(void) {
  int ret = asm_main();
  return ret;
}

And then the asm_main function is written normally, using a C calling convention. I'm wondering what exactly is the required initialization that's being generated by the C compiler, and if it can be done in a portable manner.

Infos: I'm on Windows XP, 32bit box, using the NASM assembler and mingw32-gcc for linking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

彡翼 2024-08-17 13:49:22

初始化不是由 c 编译器生成的,它是 c 库的一部分(这使得更容易针对每个操作系统/处理器进行定制)。

在 windows/unixy 系统上,相关代码通常非常简单 - 通常会进行一些库初始化(打开 STDIN、STDOUT、STDERR、设置时区等)、设置环境、处理传递给 main 的命令行;捕获 main() 的返回并调用 exit 等。

大多数 C 库中的启动代码位于名为 crt0.c、crt1.c 或类似文件(crt = c 运行时)的文件中。

在更原始或裸系统上,它还将设置堆栈和其他寄存器并清除 BSS 数据区域 - 在这种情况下,它通常位于汇编程序中(通常为 crt0.S)。

这是 BSD c 启动代码的链接 - 链接文本

Windows 版 mingw 的启动代码位于 crt1.c 中 - http:// /mingw.cvs.sourceforge.net/viewvc/mingw/runtime/

The initialisation isn't generated by the c compiler, it is part of the c library (which makes it easier to tailor for each OS/processor).

The code in question is normally very simple on windows/unixy systems - typically does a bit of library initialisation (opens STDIN, STDOUT, STDERR, sets timezone etc), sets up the environment, processes the command line for passing to main; catches the return from main() and calls exit etc.

The start up code in most c libraries is in a file called crt0.c, crt1.c or something similar (crt = c run time).

On more primitive or bare systems it will also set up the stack and other registers and clear the BSS data area - in this case it would often be in assembler (typically crt0.S).

Here is a link to the BSD c startup code - link text

And the start up code for mingw for windows is in crt1.c here - http://mingw.cvs.sourceforge.net/viewvc/mingw/runtime/

去了角落 2024-08-17 13:49:22

如果需要,您可以在汇编中编写 main 。但是很多人希望将调试语句放在 main 中,而这些语句在 C 中比在 asm 中更容易。

如果您在 asm 中编写 main,您可能必须处理 main 实际上被称为 _main 或使用备用调用约定(特别是在 Windows 下)或其他奇怪的事情,例如 C 编译器在为函数生成代码时自动为您处理命名为“主要”。这样就可以了,所以你也不必这样做。

You could write your main in assembly if you want. But a lot of people want to put debugging statements in the main and those are easier in C than in asm.

If you wrote main in asm you might have to deal with main actually being called _main or using an alternate calling convention (especially under Windows) or other strange things like that that the C compiler handles for you automatically when generating code for a function with the name "main". This way makes it so you don't have to do that either.

双手揣兜 2024-08-17 13:49:22

堆栈、寄存器和程序的文件部分(data、rodata、bss 等)必须在调用 main() 之前初始化。 C 运行时库 (CRT) 提供此初始化。

CRT 还提供在调用每个函数之前和之后执行的序言和结尾代码。序言和尾声代码更新堆栈和帧指针。

The stack, registers, and program's file sections (data, rodata, bss, etc) have to be initialized before main() is called. C runtime library (CRT) provides this initialzsation.

CRT also provides prologue and epilogue code that is executed before and after each function is called. The prologue and epilogue code updates the stack and frame pointers.

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