如何做到这一点呢?

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


当前回答

从FAQ如何让GCC生成汇编代码:

gcc -c -g -Wa,-a,-ad[其他gcc选项]foo.c > foo.lst

作为philrephly的答案的替代方案。

或者像大家说的,用-S。

其他回答

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

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

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

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

使用“-S”作为选项。它在终端中显示程序集输出。

以下命令行来自Christian Garbin的博客:

g++ -g -O -Wa,-aslh horton_ex2_05.cpp >list.txt

我从Windows XP上的DOS窗口运行g++,针对的是一个包含隐式强制转换的例程

cd C:\gpp_code
g++ -g -O -Wa,-aslh horton_ex2_05.cpp > list.txt

输出:

horton_ex2_05.cpp: In function `int main()':
horton_ex2_05.cpp:92: warning: assignment to `int' from `double'

输出是组装生成的代码,其中穿插着原始的c++代码(c++代码在生成的汇编语言流中显示为注释)。

  16:horton_ex2_05.cpp **** using std::setw;
  17:horton_ex2_05.cpp ****
  18:horton_ex2_05.cpp **** void disp_Time_Line (void);
  19:horton_ex2_05.cpp ****
  20:horton_ex2_05.cpp **** int main(void)
  21:horton_ex2_05.cpp **** {
 164                    %ebp
 165                            subl $128,%esp
?GAS LISTING C:\DOCUME~1\CRAIGM~1\LOCALS~1\Temp\ccx52rCc.s
166 0128 55                    call ___main
167 0129 89E5          .stabn 68,0,21,LM2-_main
168 012b 81EC8000      LM2:
168      0000
169 0131 E8000000      LBB2:
169      00
170                    .stabn 68,0,25,LM3-_main
171                    LM3:
172                            movl $0,-16(%ebp)

下面是在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程序的整个汇编语言代码。

使用-S开关:

g++ -S main.cpp

或者也可以用gcc:

gcc -S main.c

还有这个。