C++ C+中的汇编代码+;

C++ C+中的汇编代码+;,c++,C++,我想学习阅读编译器生成的汇编代码。我可以在哪里和如何评估由C++生成的汇编代码? 感谢您的目标文件: $ g++ -g -c -Wall yourfile.cpp -o yourfile.o 然后: 进入GDB后,可以使用反汇编命令查看生成的程序集 因此,如果您的C++源代码是: int f() { return 1; } 您可以在GDB中执行以下操作: (gdb) disassemble f 输出将是: Dump of assembler code for function f: 0x0

我想学习阅读编译器生成的汇编代码。我可以在哪里和如何评估由C++生成的汇编代码? 感谢您的目标文件:

$ g++ -g -c -Wall yourfile.cpp -o yourfile.o
然后:

进入GDB后,可以使用
反汇编
命令查看生成的程序集

因此,如果您的C++源代码是:

int f() { return 1; }
您可以在GDB中执行以下操作:

(gdb) disassemble f
输出将是:

Dump of assembler code for function f:
0x00000000 <f+0>:       push   %ebp
0x00000001 <f+1>:       mov    %esp,%ebp
0x00000003 <f+3>:       mov    $0x1,%eax
0x00000008 <f+8>:       pop    %ebp
0x00000009 <f+9>:       ret
函数f的汇编程序代码转储: 0x00000000:推送%ebp 0x00000001:mov%esp,%ebp 0x00000003:mov$0x1,%eax 0x00000008:弹出%ebp 0x00000009:ret
如果您使用的是gcc,请使用-S参数,编译器的输出将不会通过汇编程序。

您的编译器可能有一个选项来生成汇编代码输出,可以选择与相应的源代码交错。在微软Visual C++ V10中,这是. 或者,只需在调试器中并排查看这两个

无论您如何看待这一点,请务必比较有优化和无优化构建的版本。看到今天的编译器可以在不影响程序运行的情况下丢弃多少代码,真是令人惊讶。

对于GCC和objdump。

对于Visual Studio,如果您使用的是IDE,则可以修改项目属性中的C/C++“输出文件”属性,并将“汇编程序输出”更改为“带源代码的汇编”


这也是Visual C++编译器的'/fas’标志。< /P>编译程序集的生成选项比运行反汇编程序要有用得多,因为它将用汇编显示源代码。@ Ben Voigt:100%。但这并不意味着我的答案是错的。这只是另一种方式。例如,如果您没有源代码,这可能会很有帮助。这就是我一直在寻找的问题!

//a.cpp
#include <iostream>

int main()
{
   std::cout << "hello";
} 
//a.cpp
#include <iostream>

int main()
{
   std::cout << "hello";
} 
(a.s):

        .file   "a.cpp"
.lcomm __ZStL8__ioinit,1,1
        .def    ___main;        .scl    2;      .type   32;     .endef
        .section .rdata,"dr"
LC0:
        .ascii "hello\0"
        .text
.globl _main
        .def    _main;  .scl    2;      .type   32;     .endef
_main:
        pushl   %ebp
        movl    %esp, %ebp
        andl    $-16, %esp
        subl    $16, %esp
        call    ___main
        movl    $LC0, 4(%esp)
        movl    $__ZSt4cout, (%esp)
        call    __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
        movl    $0, %eax
        leave
        ret
        .def    ___tcf_0;       .scl    3;      .type   32;     .endef
___tcf_0:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $24, %esp
        movl    $__ZStL8__ioinit, (%esp)
        call    __ZNSt8ios_base4InitD1Ev
        leave
        ret
        .def    __Z41__static_initialization_and_destruction_0ii;       .scl
3;      .type   32;     .endef
__Z41__static_initialization_and_destruction_0ii:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $24, %esp
        cmpl    $1, 8(%ebp)
        jne     L3
        cmpl    $65535, 12(%ebp)
        jne     L3
        movl    $__ZStL8__ioinit, (%esp)
        call    __ZNSt8ios_base4InitC1Ev
        movl    $___tcf_0, (%esp)
        call    _atexit
L3:
        leave
        ret
        .def    __GLOBAL__I_main;       .scl    3;      .type   32;     .endef
__GLOBAL__I_main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $24, %esp
        movl    $65535, 4(%esp)
        movl    $1, (%esp)
        call    __Z41__static_initialization_and_destruction_0ii
        leave
        ret
        .section        .ctors,"w"
        .align 4
        .long   __GLOBAL__I_main
        .def    __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;
.scl    2;      .type   32;     .endef
        .def    __ZNSt8ios_base4InitD1Ev;       .scl    2;      .type   32;
.endef
        .def    __ZNSt8ios_base4InitC1Ev;       .scl    2;      .type   32;
.endef
        .def    _atexit;        .scl    2;      .type   32;     .endef