提問於cszone, LCamel透過google在djgpp的網站找到解法:
gcc -c -g -Wa,-a,-ad test.c
這個方法相當巧妙, 原理是利用 -g 產生 C source level debug symbols,
再透過 -Wa 把 -a -ad 傳給 gnu assembler, 其中:
-a requests high-level, assembly, and symbols listing.
-ad omits debugging directives from the listing.
亦可搭配-O2或-O3使用, 觀察optimized的code.
test.c:
int foo(int a) {
if (a==1)
return 1;
return a*foo(a-1);
}
輸出結果:
ARM GAS /tmp/ccc0kCsW.s page 1
1 @ Generated by gcc 2.95.2 19991024 (release) for ARM/elf
2 .file "test.c"
5 .text
6 .Ltext0:
27 .align 2
30 .global foo
32 foo:
1:test.c **** int foo(int a) {
34 .LM1:
35 @ args = 0, pretend = 0, frame = 4
36 @ frame_needed = 1, current_function_anonymous_args = 0
37 0000 0DC0A0E1 mov ip, sp
38 0004 00D82DE9 stmfd sp!, {fp, ip, lr, pc}
39 0008 04B04CE2 sub fp, ip, #4
40 000c 04D04DE2 sub sp, sp, #4
41 0010 10000BE5 str r0, [fp, #-16]
2:test.c **** if (a==1)
43 .LM2:
44 0014 10301BE5 ldr r3, [fp, #-16]
45 0018 010053E3 cmp r3, #1
46 001c 0800001A bne .L3
3:test.c **** return 1;
48 .LM3:
49 0020 0100A0E3 mov r0, #1
50 0024 110000EA b .L2
51 .L3:
4:test.c **** return a*foo(a-1);
53 .LM4:
54 0028 10201BE5 ldr r2, [fp, #-16]
55 002c 013042E2 sub r3, r2, #1
56 0030 0300A0E1 mov r0, r3
57 0034 FEFFFFEB bl foo
58 0038 0030A0E1 mov r3, r0
59 003c 10201BE5 ldr r2, [fp, #-16]
60 0040 920303E0 mul r3, r2, r3
61 0044 0300A0E1 mov r0, r3
62 0048 110000EA b .L2
5:test.c **** }
64 .LM5:
65 .L2:
66 004c 00A81BE9 ldmea fp, {fp, sp, pc}
67 .Lfe1:
69 .Lscope0:
71 .text
73 .Letext:
74 .ident "GCC: (GNU) 2.95.2 19991024 (release)"
ARM GAS /tmp/ccc0kCsW.s page 2
DEFINED SYMBOLS
*ABS*:00000000 test.c
/tmp/ccc0kCsW.s:32 .text:00000000 foo
NO UNDEFINED SYMBOLS