如何做到这一点呢?

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


当前回答

这将生成C代码+行号交织的汇编代码,以便更容易地查看哪些行生成了什么代码(-S -fverbose-asm -g -O2):

# Create assembler code:
g++ -S -fverbose-asm -g -O2 test.cc -o test.s

# Create asm interlaced with source lines:
as -alhnd test.s > test.lst

在第3页(PDF的第15页)中找到了它。

其他回答

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

如前所述,查看-S标志。

'-fdump-tree'标志家族也值得一看,特别是-fdump-tree-all,它可以让您看到GCC的一些中间形式。这些程序通常比汇编程序更具可读性(至少对我来说),并让您了解优化传递的执行情况。

我在答案中没有看到这种可能性,可能是因为这个问题来自2008年,但在2018年,你可以使用马特·戈德博尔特的在线网站https://godbolt.org

你也可以在本地克隆git并运行他的项目https://github.com/mattgodbolt/compiler-explorer

以下命令行来自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)

这将生成C代码+行号交织的汇编代码,以便更容易地查看哪些行生成了什么代码(-S -fverbose-asm -g -O2):

# Create assembler code:
g++ -S -fverbose-asm -g -O2 test.cc -o test.s

# Create asm interlaced with source lines:
as -alhnd test.s > test.lst

在第3页(PDF的第15页)中找到了它。