GCC - Passing Options to the Assembler
GNU Assembler - Command-Line Options
通常我們在用gcc時, 是直接把.c/.cpp檔案轉換成.o檔的
中間其實還有一個步驟 --- 轉換成assembly, 也就是真正的compile動作, 被gcc藏起來了
然而若要進行程式最佳化的校調, 把assembly拉出來看是免不了的
這時可以在gcc的參數加上:
-Wa,-a就會在螢幕上輸出assembly的結果
我自己是習慣使用:
-g -Wa,-adhl這樣就會有C source code / assembly 的對照
底下舉一個簡單的例子, 建立一個test.c檔:
int func (int a) { return a + 1; }
送進gcc compile (x86):
gcc -Wall -g -Wa,-adhl -c -o test.o test.c便會輸出:
1 .file "test.c" 2 .section .debug_abbrev,"dr" 3 Ldebug_abbrev0: 4 .section .debug_info,"dr" 5 Ldebug_info0: 6 .section .debug_line,"dr" 7 Ldebug_line0: 8 0000 32000000 .text 8 02001D00 8 00000101 8 FB0E0D00 8 01010101 9 Ltext0: 10 .globl _func 11 .def _func; .scl 2; .type 32; .endef 12 _func: 13 LFB0: 14 .file 1 "test.c" 1:test.c **** int func (int a) 2:test.c **** { 15 .loc 1 2 0 16 0000 55 pushl %ebp 17 LCFI0: 18 0001 89E5 movl %esp, %ebp 19 LCFI1: 3:test.c **** return a+1; 20 .loc 1 3 0 21 0003 8B4508 movl 8(%ebp), %eax 22 0006 40 incl %eax 4:test.c **** }... 23 .loc 1 4 0 24 0007 C9 leave 25 LCFI2: 26 0008 C3 ret 27 LFE0: ...
底下是一些debug symbol, 可以不用管它
當然裡面的重點就是func()的assembly, 你可以很清楚地看到x86的stack操作, +1運算, 以及call return等動作
除了方便校調程式以外, 對學習compiler / assembly 也有很大的幫助
沒有留言:
張貼留言