如何做到这一点呢?

如果我想分析某些东西是如何编译的,我该如何获得发出的程序集代码?


当前回答

下面是一个使用GCC的C语言解决方案:

gcc -S program.c && gcc program.c -o output

这里的第一部分将程序的程序集输出存储在与程序相同的文件名中,但是通过更改扩展名.s,您可以将其作为任何普通文本文件打开。 这里的第二部分编译您的程序以供实际使用,并为您的程序生成具有指定文件名的可执行文件。

上面使用的program.c是程序的名称,输出是要生成的可执行文件的名称。

其他回答

使用-S开关:

g++ -S main.cpp

或者也可以用gcc:

gcc -S main.c

还有这个。

使用-S选项:

gcc -S program.c

正如每个人都指出的那样,对GCC使用-S选项。我还想补充一点,根据是否添加优化选项(-O0表示没有,-O2表示积极优化),结果可能会有所不同(很大程度上!)

特别是在RISC架构上,编译器在进行优化时经常会把代码转换得几乎认不出来。结果令人印象深刻,令人着迷!

下面是在Windows上查看/打印任何C程序的汇编代码的步骤:

在控制台/终端命令提示符中:

Write a C program in a C code editor like Code::Blocks and save it with filename extension .c Compile and run it. Once run successfully, go to the folder where you have installed your GCC compiler and enter the following command to get a ' .s ' file of the ' .c' file cd C:\gcc gcc -S complete path of the C file ENTER An example command (as in my case) gcc -S D:\Aa_C_Certified\alternate_letters.c This outputs a '.s' file of the original '.c' file. After this, type the following command cpp filename.s ENTER Example command (as in my case) cpp alternate_letters.s <enter>

这将打印/输出C程序的整个汇编语言代码。

如果您正在寻找LLVM程序集:

llvm-gcc -emit-llvm -S hello.c