给定 c++ 行的转储程序集

Dump Assembly of given c++ line(s)

假设我有这样的代码(注意在编辑器中我可以看到行号)

//File example_optimization.cpp

#1 int spreadi = (d_ai - d_bi);
#2 int transactionCosti = spreadi / (spreadi < 10 ? 1 : 10);

如果编译器是优化编译器并且设置了正确的标志,我想向自己证明除以 1 实际上并没有发生。

linux 中是否有工具(或可通过管道使用的工具组合),我可以这样说:

ctoassembler -c g++ -l 1 2 example_optimization.cpp 

所以这个命令说,调用 ctoassembler 使用编译器和链接器 gnu g++,并转储 c++ 文件的第 1 行到第 2 行 example_optimization.cpp。默认是将汇编代码输出到stdout。

我真的不在乎用法是什么。我有兴趣能够看到我的代码被编译器转换成什么,而不必自己搜索它。

我不介意使用目标文件,如果这样更容易的话,但不知何故最好显示 c++ 源代码和相应的汇编代码 "side-by-side"。

GCC 将停止生成汇编文件:

-S Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified. By default, the assembler file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, etc., with ‘.s’.

Input files that don't require compilation are ignored.

来源:https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options

你的另一个选择是调试程序并查看汇编程序。

运行代码通过gdb,在你要的C行打断点,运行,然后输入disassemble查看后面几行代码。