码迷,mamicode.com
首页 > 其他好文 > 详细

想了解下 GCC 对 switch 的优化,但是不懂 AT&T 伪汇编指令,帮忙分析下

时间:2014-11-08 11:44:33      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   sp   div   on   art   

C 源码:

  1 void fun(char *ch){
  2     switch(*ch){
  3     case -:
  4         *ch = __LINE__; break;
  5     case 0:
  6         *ch = __LINE__; break;
  7     case 1:
  8         *ch = __LINE__; break;
  9     case 2:
 10         *ch = __LINE__; break;
 11     case 3:
 12         *ch = __LINE__; break;
 13     case 4:
 14         *ch = __LINE__; break;
 15     case 5:
 16         *ch = __LINE__; break;
 17     case 6:
 18         *ch = __LINE__; break;
 19     case 7:
 20         *ch = __LINE__; break;
 21     case 8:
 22         *ch = __LINE__; break;
 23     case 9:
 24         *ch = __LINE__; break;
 25     case A:
 26         *ch = __LINE__; break;
 27     case B:
 28         *ch = __LINE__; break;
 29     case C:
 30         *ch = __LINE__; break;
 31     case D:
 32         *ch = __LINE__; break;
 33     case E:
 34         *ch = __LINE__; break;
 35     case F:
 36         *ch = __LINE__; break;
 37     case G:
 38         *ch = __LINE__; break;
 39     case H:
 40         *ch = __LINE__; break;
 41     case I:
 42         *ch = __LINE__; break;
 43     case J:
 44         *ch = __LINE__; break;
 45     case K:
 46         *ch = __LINE__; break;
 47     case L:
 48         *ch = __LINE__; break;
 49     case M:
 50         *ch = __LINE__; break;
 51     case N:
 52         *ch = __LINE__; break;
 53     case O:
 54         *ch = __LINE__; break;
 55     case P:
 56         *ch = __LINE__; break;
 57     case Q:
 58         *ch = __LINE__; break;
 59     case R:
 60         *ch = __LINE__; break;
 61     case S:
 62         *ch = __LINE__; break;
 63     case T:
 64         *ch = __LINE__; break;
 65     case U:
 66         *ch = __LINE__; break;
 67     case V:
 68         *ch = __LINE__; break;
 69     case W:
 70         *ch = __LINE__; break;
 71     case X:
 72         *ch = __LINE__; break;
 73     case Y:
 74         *ch = __LINE__; break;
 75     case Z:
 76         *ch = __LINE__; break;
 77     case a:
 78         *ch = __LINE__; break;
 79     case e:
 80         *ch = __LINE__; break;
 81     case f:
 82         *ch = __LINE__; break;
 83     case g:
 84         *ch = __LINE__; break;
 85     case i:
 86         *ch = __LINE__; break;
 87     case l:
 88         *ch = __LINE__; break;
 89     case o:
 90         *ch = __LINE__; break;
 91     case t:
 92         *ch = __LINE__; break;
 93     case u:
 94         *ch = __LINE__; break;
 95     case v:
 96         *ch = __LINE__; break;
 97     case x:
 98         *ch = __LINE__; break;
 99     case b:
100         *ch = __LINE__; break;
101     case c:
102         *ch = __LINE__; break;
103     case d:
104         *ch = __LINE__; break;
105     case h:
106         *ch = __LINE__; break;
107     case j:
108         *ch = __LINE__; break;
109     case k:
110         *ch = __LINE__; break;
111     case m:
112         *ch = __LINE__; break;
113     case n:
114         *ch = __LINE__; break;
115     case p:
116         *ch = __LINE__; break;
117     case q:
118         *ch = __LINE__; break;
119     case r:
120         *ch = __LINE__; break;
121     case s:
122         *ch = __LINE__; break;
123     case w:
124         *ch = __LINE__; break;
125     case y:
126         *ch = __LINE__; break;
127     case z:
128         *ch = __LINE__; break;
129     default:
130         *ch = 0;
131     }
132 }

执行 gcc -S -O2 -o switch.s switch.c 编译后得到的汇编代码:

  1     .file    "switch.c"
  2     .text
  3     .p2align 4,,15
  4     .globl    fun
  5     .type    fun, @function
  6 fun:
  7 .LFB0:
  8     .cfi_startproc
  9     movzbl    (%rdi), %eax
 10     subl    $45, %eax
 11     cmpb    $77, %al
 12     ja    .L2
 13     movzbl    %al, %eax
 14     jmp    *.L4(,%rax,8)
 15     .section    .rodata
 16     .align 8
 17     .align 4
 18 .L4:
 19     .quad    .L3
 20     .quad    .L2
 21     .quad    .L2
 22     .quad    .L5
 23     .quad    .L6
 24     .quad    .L7
 25     .quad    .L8
 26     .quad    .L9
 27     .quad    .L10
 28     .quad    .L11
 29     .quad    .L12
 30     .quad    .L13
 31     .quad    .L14
 32     .quad    .L2
 33     .quad    .L2
 34     .quad    .L2
 35     .quad    .L2
 36     .quad    .L2
 37     .quad    .L2
 38     .quad    .L2
 39     .quad    .L15
 40     .quad    .L16
 41     .quad    .L17
 42     .quad    .L18
 43     .quad    .L19
 44     .quad    .L20
 45     .quad    .L21
 46     .quad    .L22
 47     .quad    .L23
 48     .quad    .L24
 49     .quad    .L25
 50     .quad    .L26
 51     .quad    .L27
 52     .quad    .L28
 53     .quad    .L29
 54     .quad    .L30
 55     .quad    .L31
 56     .quad    .L32
 57     .quad    .L33
 58     .quad    .L34
 59     .quad    .L35
 60     .quad    .L36
 61     .quad    .L37
 62     .quad    .L38
 63     .quad    .L39
 64     .quad    .L40
 65     .quad    .L2
 66     .quad    .L2
 67     .quad    .L2
 68     .quad    .L2
 69     .quad    .L2
 70     .quad    .L2
 71     .quad    .L41
 72     .quad    .L42
 73     .quad    .L43
 74     .quad    .L44
 75     .quad    .L45
 76     .quad    .L46
 77     .quad    .L47
 78     .quad    .L48
 79     .quad    .L49
 80     .quad    .L50
 81     .quad    .L51
 82     .quad    .L52
 83     .quad    .L53
 84     .quad    .L54
 85     .quad    .L55
 86     .quad    .L56
 87     .quad    .L57
 88     .quad    .L58
 89     .quad    .L59
 90     .quad    .L60
 91     .quad    .L61
 92     .quad    .L62
 93     .quad    .L63
 94     .quad    .L64
 95     .quad    .L65
 96     .quad    .L66
 97     .text
 98     .p2align 4,,10
 99     .p2align 3
100 .L66:
101     movb    $-127, (%rdi)
102     ret
103     .p2align 4,,10
104     .p2align 3
105 .L65:
106     movb    $127, (%rdi)
107     ret
108     .p2align 4,,10
109     .p2align 3
110 .L64:
111     movb    $99, (%rdi)
112     ret
113     .p2align 4,,10
114     .p2align 3
115 .L63:
116     movb    $125, (%rdi)
117     ret
118     .p2align 4,,10
119     .p2align 3
120 .L62:
121     movb    $97, (%rdi)
122     ret
123     .p2align 4,,10
124     .p2align 3
125 .L61:
126     movb    $95, (%rdi)
127     ret
128     .p2align 4,,10
129     .p2align 3
130 .L60:
131     movb    $93, (%rdi)
132     ret
133     .p2align 4,,10
134     .p2align 3
135 .L59:
136     movb    $123, (%rdi)
137     ret
138     .p2align 4,,10
139     .p2align 3
140 .L58:
141     movb    $121, (%rdi)
142     ret
143     .p2align 4,,10
144     .p2align 3
145 .L57:
146     movb    $119, (%rdi)
147     ret
148     .p2align 4,,10
149     .p2align 3
150 .L56:
151     movb    $117, (%rdi)
152     ret
153     .p2align 4,,10
154     .p2align 3
155 .L55:
156     movb    $91, (%rdi)
157     ret
158     .p2align 4,,10
159     .p2align 3
160 .L54:
161     movb    $115, (%rdi)
162     ret
163     .p2align 4,,10
164     .p2align 3
165 .L53:
166     movb    $113, (%rdi)
167     ret
168     .p2align 4,,10
169     .p2align 3
170 .L52:
171     movb    $89, (%rdi)
172     ret
173     .p2align 4,,10
174     .p2align 3
175 .L51:
176     movb    $111, (%rdi)
177     ret
178     .p2align 4,,10
179     .p2align 3
180 .L50:
181     movb    $109, (%rdi)
182     ret
183     .p2align 4,,10
184     .p2align 3
185 .L49:
186     movb    $87, (%rdi)
187     ret
188     .p2align 4,,10
189     .p2align 3
190 .L48:
191     movb    $107, (%rdi)
192     ret
193     .p2align 4,,10
194     .p2align 3
195 .L47:
196     movb    $85, (%rdi)
197     ret
198     .p2align 4,,10
199     .p2align 3
200 .L46:
201     movb    $83, (%rdi)
202     ret
203     .p2align 4,,10
204     .p2align 3
205 .L45:
206     movb    $81, (%rdi)
207     ret
208     .p2align 4,,10
209     .p2align 3
210 .L44:
211     movb    $105, (%rdi)
212     ret
213     .p2align 4,,10
214     .p2align 3
215 .L43:
216     movb    $103, (%rdi)
217     ret
218     .p2align 4,,10
219     .p2align 3
220 .L42:
221     movb    $101, (%rdi)
222     ret
223     .p2align 4,,10
224     .p2align 3
225 .L41:
226     movb    $79, (%rdi)
227     ret
228     .p2align 4,,10
229     .p2align 3
230 .L40:
231     movb    $77, (%rdi)
232     ret
233     .p2align 4,,10
234     .p2align 3
235 .L39:
236     movb    $75, (%rdi)
237     ret
238     .p2align 4,,10
239     .p2align 3
240 .L38:
241     movb    $73, (%rdi)
242     ret
243     .p2align 4,,10
244     .p2align 3
245 .L37:
246     movb    $71, (%rdi)
247     ret
248     .p2align 4,,10
249     .p2align 3
250 .L36:
251     movb    $69, (%rdi)
252     ret
253     .p2align 4,,10
254     .p2align 3
255 .L35:
256     movb    $67, (%rdi)
257     ret
258     .p2align 4,,10
259     .p2align 3
260 .L34:
261     movb    $65, (%rdi)
262     ret
263     .p2align 4,,10
264     .p2align 3
265 .L33:
266     movb    $63, (%rdi)
267     ret
268     .p2align 4,,10
269     .p2align 3
270 .L32:
271     movb    $61, (%rdi)
272     ret
273     .p2align 4,,10
274     .p2align 3
275 .L31:
276     movb    $59, (%rdi)
277     ret
278     .p2align 4,,10
279     .p2align 3
280 .L30:
281     movb    $57, (%rdi)
282     ret
283     .p2align 4,,10
284     .p2align 3
285 .L29:
286     movb    $55, (%rdi)
287     ret
288     .p2align 4,,10
289     .p2align 3
290 .L28:
291     movb    $53, (%rdi)
292     ret
293     .p2align 4,,10
294     .p2align 3
295 .L27:
296     movb    $51, (%rdi)
297     ret
298     .p2align 4,,10
299     .p2align 3
300 .L26:
301     movb    $49, (%rdi)
302     ret
303     .p2align 4,,10
304     .p2align 3
305 .L25:
306     movb    $47, (%rdi)
307     ret
308     .p2align 4,,10
309     .p2align 3
310 .L24:
311     movb    $45, (%rdi)
312     ret
313     .p2align 4,,10
314     .p2align 3
315 .L23:
316     movb    $43, (%rdi)
317     ret
318     .p2align 4,,10
319     .p2align 3
320 .L22:
321     movb    $41, (%rdi)
322     ret
323     .p2align 4,,10
324     .p2align 3
325 .L21:
326     movb    $39, (%rdi)
327     ret
328     .p2align 4,,10
329     .p2align 3
330 .L20:
331     movb    $37, (%rdi)
332     ret
333     .p2align 4,,10
334     .p2align 3
335 .L19:
336     movb    $35, (%rdi)
337     ret
338     .p2align 4,,10
339     .p2align 3
340 .L18:
341     movb    $33, (%rdi)
342     ret
343     .p2align 4,,10
344     .p2align 3
345 .L17:
346     movb    $31, (%rdi)
347     ret
348     .p2align 4,,10
349     .p2align 3
350 .L16:
351     movb    $29, (%rdi)
352     ret
353     .p2align 4,,10
354     .p2align 3
355 .L15:
356     movb    $27, (%rdi)
357     ret
358     .p2align 4,,10
359     .p2align 3
360 .L14:
361     movb    $25, (%rdi)
362     ret
363     .p2align 4,,10
364     .p2align 3
365 .L13:
366     movb    $23, (%rdi)
367     ret
368     .p2align 4,,10
369     .p2align 3
370 .L12:
371     movb    $21, (%rdi)
372     ret
373     .p2align 4,,10
374     .p2align 3
375 .L11:
376     movb    $19, (%rdi)
377     ret
378     .p2align 4,,10
379     .p2align 3
380 .L10:
381     movb    $17, (%rdi)
382     ret
383     .p2align 4,,10
384     .p2align 3
385 .L9:
386     movb    $15, (%rdi)
387     ret
388     .p2align 4,,10
389     .p2align 3
390 .L8:
391     movb    $13, (%rdi)
392     ret
393     .p2align 4,,10
394     .p2align 3
395 .L7:
396     movb    $11, (%rdi)
397     ret
398     .p2align 4,,10
399     .p2align 3
400 .L6:
401     movb    $9, (%rdi)
402     ret
403     .p2align 4,,10
404     .p2align 3
405 .L5:
406     movb    $7, (%rdi)
407     ret
408     .p2align 4,,10
409     .p2align 3
410 .L3:
411     movb    $5, (%rdi)
412     ret
413     .p2align 4,,10
414     .p2align 3
415 .L2:
416     movb    $0, (%rdi)
417     ret
418     .cfi_endproc
419 .LFE0:
420     .size    fun, .-fun
421     .ident    "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
422     .section    .note.GNU-stack,"",@progbits

 

想了解下 GCC 对 switch 的优化,但是不懂 AT&T 伪汇编指令,帮忙分析下

标签:style   blog   io   color   ar   sp   div   on   art   

原文地址:http://www.cnblogs.com/huye/p/4082883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!