如何编译由 Java 或 C 生成的代码++应用程序
我一直在学习编译器理论和汇编,并成功创建了一个生成 x86 汇编代码的编译器。
我怎样才能把这个汇编代码变成.exe?是否有一些我必须与之交互的神奇 API 或工具?或者它比我想象的更简单?
我不太确定 .exe 中有什么,或者汇编代码和 .exe 本身之间有多少抽象。
我的“编译器”是用 Java 编写的,但我也想知道如何用 C++ 编写。
请注意,如果我使用生成的程序集,它会编译为 .exe,例如使用 vc++。
编辑:更准确地说,我已经知道如何使用编译器编译汇编代码。我想要的是让我的程序基本上输出 .exe。
I've been learning compiler theory and assembly and have managed to create a compiler that generates x86 assembly code.
How can I take this assembly code and turn it into a .exe? Is there some magical API or tool I have to interact with? Or is it simpler than I think?
I'm not really sure what's in a .exe, or how much abstraction lies between assembly code and the .exe itself.
My 'compiler' was written in Java, but I'd like to know how to do this in C++ as well.
Note that if I take the generated assembly, it compiles to a .exe just fine for example with vc++.
Edit: To be more precise, I already know how to compile assembly code using a compiler. What I'm wanting is to have my program to basically output a .exe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您需要生成一个汇编器进程和一个链接器进程。在 UNIX 上,它就像调用
fork()
函数(该函数将创建一个新进程)和exec()
函数(指定汇编器和链接器可执行文件名称)一样简单作为函数的参数,为这些可执行文件提供合适的参数,这将是生成的程序集和目标文件的名称。这就是您在 UNIX 系统上需要做的全部事情。It looks like you need to spawn an assembler process and a linker process. On UNIX, it's as simple as invoking the
fork()
function, which would create a new process, and theexec()
function, specifying the assembler and the linker executable names as the function's parameter, with suitable arguments to those executables, which would be the names of your generated assembly and object files. That's all you'd need to do on a UNIX system.通常,您使用汇编器和链接器来创建 exe 文件。不涉及任何魔法。组装不同的部分,添加标头和其他样板,以便操作系统知道程序的引导代码位于何处并组织内存。
VC++ 编译器在幕后执行此操作。您可能会考虑在 Linux 上玩一下,因为您可以更好地了解在 Unix 平台上运行的机器。本质上是一样的,只是在windows上查看UI比较困难。
Normally you use an assembler and a linker to create an exe file. There is no magic involved. The different parts are assembled, a header and other boilerplate is added so the OS knows where the bootstrap code is located for the program and to organize the memory.
The VC++ compiler does that under the hood. You might consider playing a bit on Linux as you can better see the machinery working on Unix platforms. It is fundamentally the same, but it s just difficult to look through to UI on windows.
原则上,每条流水线对应一条机器指令,它只是exe文件中的几个字节。您可以在处理器的规格中找到它们。因此,如果您知道代码和 exe 格式(标头、重定位信息等),您就可以编写自己的汇编器。
汇编程序如何将 x86 指令助记符映射到二进制机说明?
In principle, each assembly line corresponds to a machine instruction which is just a few bytes in the exe file. You can find them in the specs for the processor. So you can write your own asembeler if you know the codes and the exe format (header, relocation info etc).
How Do Assemblers Map x86 Instruction Mnemonics to Binary Machine Instructions?