开发者

Questions re: assembly generated from my C++ by gcc

Compiling this code:

int main ()
{
    return 0;
}

using:

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

My questions:

  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;"?

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.


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

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.

If you compile your code with -O3 -fomit-frame-pointer, the code will be compiled to just two instructions:

xorl    %eax,%eax
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).


.file, .text, etc are assembler directives.

.LFB0, .LFE0 are local labels, which are normally used as branch destinations within a function.

As for the size, there are really only a few actual instructions - most of the above listing consists of directives, etc. For future reference you might also want to turn up the optimisation level to remove otherwise redudant instructions, i.e. gcc -Wall -O3 -S ....


It's just that there's a lot going on behind your simple program.

If you intend to read assembler outputs, by no means compile C++. Use plain C, the output is far clearer for a number of reasons.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜