views:

503

answers:

3

I've been teaching myself GNU Assembly for a while now by writing statements in C, compiling them with "gcc -S" and studying the output. This works alright on x86 (and compiling with -m32) but on my AMD64 box, for this code (just as an example):

int main()
{
    return 0;
}

GCC gives me:

 .file "test.c"
 .text
.globl main
 .type main, @function
main:
.LFB2:
 pushq %rbp
.LCFI0:
 movq %rsp, %rbp
.LCFI1:
 movl $0, %eax
 leave
 ret
.LFE2:
 .size main, .-main
 .section .eh_frame,"a",@progbits
.Lframe1:
 .long .LECIE1-.LSCIE1
.LSCIE1:
 .long 0x0
 .byte 0x1
 .string "zR"
 .uleb128 0x1
 .sleb128 -8
 .byte 0x10
 .uleb128 0x1
 .byte 0x3
 .byte 0xc
 .uleb128 0x7
 .uleb128 0x8
 .byte 0x90
 .uleb128 0x1
 .align 8
.LECIE1:
.LSFDE1:
 .long .LEFDE1-.LASFDE1
.LASFDE1:
 .long .LASFDE1-.Lframe1
 .long .LFB2
 .long .LFE2-.LFB2
 .uleb128 0x0
 .byte 0x4
 .long .LCFI0-.LFB2
 .byte 0xe
 .uleb128 0x10
 .byte 0x86
 .uleb128 0x2
 .byte 0x4
 .long .LCFI1-.LCFI0
 .byte 0xd
 .uleb128 0x6
 .align 8
.LEFDE1:
 .ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
 .section .note.GNU-stack,"",@progbits

Compared with:

 .file "test.c"
 .text
.globl main
 .type main, @function
main:
 leal 4(%esp), %ecx
 andl $-16, %esp
 pushl -4(%ecx)
 pushl %ebp
 movl %esp, %ebp
 pushl %ecx
 movl $0, %eax
 popl %ecx
 popl %ebp
 leal -4(%ecx), %esp
 ret
 .size main, .-main
 .ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
 .section .note.GNU-stack,"",@progbits

on x86.

Is there a way to make GCC -S on x86_64 output Assembly without the fluff?

+2  A: 

You can try placing the code you want to study in a function.

E.g.:

int ftest(void)
{
    return 0;
}

int main(void)
{
    return ftest();
}

If you look at the assembly-source for test it will be as clean as you need.

..snip..
test:
.LFB2:
        pushq   %rbp
.LCFI0:
        movq    %rsp, %rbp
.LCFI1:
        movl    $0, %eax
        leave
        ret
..snip..
slashmais
The assembly for main is already pretty clean...
Stephen Canon
slashmais
A: 

I've found that using the -Os flag makes things clearer. I tried it your tiny example, but it made very little difference.

That said, I remember it being helpful when I was learning assembly (on a Sparc).

Paul Biggar
A: 

The stuff that goes into .eh_frame section is unwind descriptors, which you only need to unwind stack (e.g. with GDB). While learning assembly, you could simply ignore it. Here is a way to do the "clean up" you want:

gcc -S -o - test.c | sed -e '/^\.L/d' -e '/\.eh_frame/Q'
        .file   "test.c"
        .text
.globl main
        .type   main,@function
main:
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $0, %eax
        leave
        ret
        .size   main,.Lfe1-main
Employed Russian
Fantastic! Thanks :-)
John