问题re:从我的C + +由gcc生成的程序集 [英] Questions re: assembly generated from my C++ by gcc

查看:120
本文介绍了问题re:从我的C + +由gcc生成的程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译此代码:

int main ()
{
    return 0;
}

使用:

gcc -S filename.cpp

...生成此程序集:

...generates this assembly:

        .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

我的问题:


  1. 是之后的所有内容。

  2. 什么是.LFB0:?

  3. 什么是.LFE0:?
  4. 这么大的代码只为int main()和return 0;?
  1. Is everything after "." a comment?
  2. What is .LFB0:?
  3. What is .LFE0:?
  4. Why is it so big code only for "int main ()" and "return 0;"?

PS我读了大量的汇编网书,很多(至少30)的教程,我可以做的是复制代码,粘贴或重写它。现在我正在尝试一种不同的方法,试图学习它。问题是我明白什么是movl,pop等等,但不明白如何组合这些东西使代码流。我不知道在哪里或如何正确开始编写程序在asm是。我仍然是静态不动态的,因为在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.

推荐答案

$ c> .file , .text ,...是汇编器伪指令, .LFB0 .LFE0 是本地标签。生成的代码中唯一的指令是:

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

是函数序言。帧指针存储在堆栈上并更新。下一个intuction存储在 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.

您的代码与 -O3 -fomit-frame-pointer ,代码将编译为只有两个指令:

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(它只需要两个字节进行编码,而 movl 0,%eax 需要5个字节) ,第二个是 ret 指令。帧指针操作是为了减轻调试(可以获得backtrace没有它,但它是更困难的)。

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).

这篇关于问题re:从我的C + +由gcc生成的程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆