问题回复:我的C组件产生++由GCC [英] Questions re: assembly generated from my C++ by gcc
问题描述
此编译code:
int main ()
{
return 0;
}
使用:
GCC -S filename.cpp
...生成该组件:
.file "heloworld.cpp"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
.cfi_personality 0x0,__gxx_personality_v0
pushl %ebp
.cfi_def_cfa_offset 8
movl %esp, %ebp
.cfi_offset 5, -8
.cfi_def_cfa_register 5
movl $0, %eax
popl %ebp
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
.section .note.GNU-stack,"",@progbits
我的问题:
- 之后,是一切。评论?
- 什么是.LFB0?
- 什么是.LFE0?
- 为什么会这么大code仅适用于整数的main()和返回0;?
P.S。我读的组装净书籍,教程的很多(至少30)了很多,我所能做的就是复制code和粘贴或重写。现在我想要一种不同的方法,试图以某种方式学习。问题是我不明白什么是MOVL,流行音乐等,但不知道如何把这些东西结合起来,使code流。我不知道在哪里或如何正确地开始汇编编写一个程序。我还是静不宜动如C ++,但我想学习汇编。
P.S. I read alot of assembly net books, a lot (at least 30) of tutorials and all I can do is copy code and paste it or rewrite it. Now I'm trying a different approach to try to learn it somehow. The problem is I do understand what are movl, pop, etc, but don't understand how to combine these things to make code "flow". I don't know where or how to correctly start writing a program in asm is. I'm still static not dynamic as in C++ but I want to learn assembly.
推荐答案
至于其他已经说过, .file
,的.text
,...是汇编指令和 .LFB0
, .LFE0
是本地的标签。在生成code的唯一指令是:
As other have said, .file
, .text
, ... are assembler directives and .LFB0
, .LFE0
are local labels. The only instruction in the generated code are:
pushl %ebp
movl %esp, %ebp
movl $0, %eax
popl %ebp
ret
前两个指令是函数序幕。帧指针存储在堆栈上和更新。接下来按照压店0 EAX
寄存器(I386 ABI指出,整数返回值通过 EAX
注册返回)。最后两个指令是函数尾声。帧指针被恢复,然后通过 RET
指令函数返回到其调用者。
The first two instruction are the function prologue. The frame pointer is stored on the stack and updated. The next intruction store 0 in eax
register (i386 ABI states that integer return value are returned via the eax
register). The two last instructions are function epilogue. The frame pointer is restored, and then the function return to its caller via the ret
instruction.
如果您编译code与 -O3 -fomit-frame-pointer的
中,code将被编译到只有两个指令:
If you compile your code with -O3 -fomit-frame-pointer
, the code will be compiled to just two instructions:
xorl %eax,%eax
ret
第一套 EAX
0(只需要两个字节来恩code,而 MOVL 0%EAX
坐5个字节),第二个是 RET
指令。帧指针操作有缓解调试(这是可能得到回溯没有它,但它是比较困难的)。
The first set eax
to 0 (it only takes two bytes to encode, while movl 0,%eax
take 5 bytes), and the second is the ret
instruction. The frame pointer manipulation is there to ease debugging (it is possible to get backtrace without it, but it is more difficult).
这篇关于问题回复:我的C组件产生++由GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!