2009-09-26 51 views
10

我一直在用C語言編寫語句,用「gcc -S」編譯它們並研究輸出結果,一直在教自己的GNU程序集。這工作好了在x86(以及使用-m32編譯),但在我的AMD64中,此代碼(就像一個例子):使用gcc清潔x86_64程序集輸出?

int main() 
{ 
    return 0; 
} 

GCC給我:

 .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

則爲:

 .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

在x86上。

有沒有辦法讓GCC -S在x86_64上輸出Assembly沒有絨毛?

+0

GCC 4.8現在默認產生更清晰的輸出。總結清潔你的意思也很酷:你刪除的東西會產生不同的ELF輸出;-) – 2015-10-16 07:58:12

回答

5

進入.eh_frame部分的內容是展開描述符,您只需展開堆棧(例如使用GDB)即可。在學習裝配時,你可以簡單地忽略它。這裏有一種方法來做你想要的「清理」:

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 
+0

太棒了!謝謝:-) – John 2009-09-28 10:11:15

+0

爲什麼不使用-fno-exceptions -fno-rtti? – Trass3r 2014-02-26 13:45:35

+2

刪除所有'.L'本地標籤對於具有循環或分支的函數不會很好:/ – 2016-09-05 20:43:04

5

您可以嘗試將您想要學習的代碼放入函數中。

例如爲:

int ftest(void) 
{ 
    return 0; 
} 

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

如果你看一下彙編源測試會因爲你需要清潔。

..snip.. 
test: 
.LFB2: 
     pushq %rbp 
.LCFI0: 
     movq %rsp, %rbp 
.LCFI1: 
     movl $0, %eax 
     leave 
     ret 
..snip.. 
+0

main的程序集已經很乾淨了...... – 2009-09-26 16:23:30

+0

是的,但他正在學習(與我)&越少'混亂'越好。 – slashmais 2009-09-26 16:30:24

3

我發現使用-Os標誌可以讓事情更清楚。我嘗試了你的小例子,但它沒有什麼區別。

這就是說,我記得它在我學習裝配(在Sparc上)時很有幫助。