[英]Assembly Code in C++
我想學習閱讀編譯器生成的匯編代碼。 我在哪里以及如何評估從C ++生成的匯編代碼?
謝謝
您的編譯器可能有生成匯編代碼輸出的選項,可選擇與相應的源代碼交錯。 在Microsoft Visual C ++ v10中,這是/ Fa 。
或者,只需在調試器中並排查看兩個。
無論如何,請務必比較使用和不使用優化構建的版本。 令人驚訝的是,今天的編譯器可以丟棄多少而不會影響程序的運行。
從您的目標文件:
$ g++ -g -c -Wall yourfile.cpp -o yourfile.o
然后:
$ gdb yourfile.o
進入GDB后,您可以使用disassemble
命令查看生成的程序集。
所以,如果您的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
如果您使用的是gcc,請使用-S參數,編譯器的輸出將不會通過匯編程序。
對於GCC和objdump。 使用GCC生成可讀組件?
對於Visual Studio,如果使用IDE,則可以修改項目屬性中的C / C ++“輸出文件”屬性,並將“匯編程序輸出”更改為“使用源代碼進行匯編”
這也是Visual C ++編譯器的'/ Fas'標志。
//a.cpp
#include <iostream>
int main()
{
std::cout << "hello";
}
在gcc上你可以使用-S
選項,即gcc -S a.cpp
生as
(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.