简体   繁体   English

C ++中的汇编代码

[英]Assembly Code in C++

I would like to learn reading the assembly code generated by the compiler. 我想学习阅读编译器生成的汇编代码。 Where and how could I assess the assembly code generated from C++ ? 我在哪里以及如何评估从C ++生成的汇编代码?

Thanks 谢谢

Your compiler likely has an option to generate assembly code output, optionally interleaved with the corresponding source code. 您的编译器可能有生成汇编代码输出的选项,可选择与相应的源代码交错。 In Microsoft Visual C++ v10 this is /Fa . 在Microsoft Visual C ++ v10中,这是/ Fa

Or, just look at the two side-by-side in your debugger. 或者,只需在调试器中并排查看两个。

However you look at this, be sure to compare the versions built with and without optimization. 无论如何,请务必比较使用和不使用优化构建的版本。 It's amazing to see how much can be discarded by today's compilers without affecting the operation of the program. 令人惊讶的是,今天的编译器可以丢弃多少而不会影响程序的运行。

From your object file: 从您的目标文件:

$ g++ -g -c -Wall yourfile.cpp -o yourfile.o

Then: 然后:

$ gdb yourfile.o

Once in GDB you could use disassemble command to view the generated assembly. 进入GDB后,您可以使用disassemble命令查看生成的程序集。

So, if your C++ source is: 所以,如果您的C ++源代码是:

int f() { return 1; }

You can do in GDB : 你可以在GDB中做:

(gdb) disassemble f

And the output will be: 输出将是:

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

如果您使用的是gcc,请使用-S参数,编译器的输出将不会通过汇编程序。

For GCC and objdump. 对于GCC和objdump。 Using GCC to produce readable assembly? 使用GCC生成可读组件?

For Visual Studio, if you are using the IDE, you can modify the C/C++ 'Output Files' property in your project's properties, and change 'Assembler Output' to 'Assembly with Source Code' 对于Visual Studio,如果使用IDE,则可以修改项目属性中的C / C ++“输出文件”属性,并将“汇编程序输出”更改为“使用源代码进行汇编”

This is also the '/Fas' flag for Visual C++ compiler. 这也是Visual C ++编译器的'/ Fas'标志。

//a.cpp
#include <iostream>

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

On gcc you can use the -S option, ie gcc -S a.cpp generates as 在gcc上你可以使用-S选项,即gcc -S a.cppas

(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM