首页 > 代码库 > 想了解下 GCC 对 switch 的优化,但是不懂 AT&T 伪汇编指令,帮忙分析下
想了解下 GCC 对 switch 的优化,但是不懂 AT&T 伪汇编指令,帮忙分析下
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 3100 .L66:101 movb $-127, (%rdi)102 ret103 .p2align 4,,10104 .p2align 3105 .L65:106 movb $127, (%rdi)107 ret108 .p2align 4,,10109 .p2align 3110 .L64:111 movb $99, (%rdi)112 ret113 .p2align 4,,10114 .p2align 3115 .L63:116 movb $125, (%rdi)117 ret118 .p2align 4,,10119 .p2align 3120 .L62:121 movb $97, (%rdi)122 ret123 .p2align 4,,10124 .p2align 3125 .L61:126 movb $95, (%rdi)127 ret128 .p2align 4,,10129 .p2align 3130 .L60:131 movb $93, (%rdi)132 ret133 .p2align 4,,10134 .p2align 3135 .L59:136 movb $123, (%rdi)137 ret138 .p2align 4,,10139 .p2align 3140 .L58:141 movb $121, (%rdi)142 ret143 .p2align 4,,10144 .p2align 3145 .L57:146 movb $119, (%rdi)147 ret148 .p2align 4,,10149 .p2align 3150 .L56:151 movb $117, (%rdi)152 ret153 .p2align 4,,10154 .p2align 3155 .L55:156 movb $91, (%rdi)157 ret158 .p2align 4,,10159 .p2align 3160 .L54:161 movb $115, (%rdi)162 ret163 .p2align 4,,10164 .p2align 3165 .L53:166 movb $113, (%rdi)167 ret168 .p2align 4,,10169 .p2align 3170 .L52:171 movb $89, (%rdi)172 ret173 .p2align 4,,10174 .p2align 3175 .L51:176 movb $111, (%rdi)177 ret178 .p2align 4,,10179 .p2align 3180 .L50:181 movb $109, (%rdi)182 ret183 .p2align 4,,10184 .p2align 3185 .L49:186 movb $87, (%rdi)187 ret188 .p2align 4,,10189 .p2align 3190 .L48:191 movb $107, (%rdi)192 ret193 .p2align 4,,10194 .p2align 3195 .L47:196 movb $85, (%rdi)197 ret198 .p2align 4,,10199 .p2align 3200 .L46:201 movb $83, (%rdi)202 ret203 .p2align 4,,10204 .p2align 3205 .L45:206 movb $81, (%rdi)207 ret208 .p2align 4,,10209 .p2align 3210 .L44:211 movb $105, (%rdi)212 ret213 .p2align 4,,10214 .p2align 3215 .L43:216 movb $103, (%rdi)217 ret218 .p2align 4,,10219 .p2align 3220 .L42:221 movb $101, (%rdi)222 ret223 .p2align 4,,10224 .p2align 3225 .L41:226 movb $79, (%rdi)227 ret228 .p2align 4,,10229 .p2align 3230 .L40:231 movb $77, (%rdi)232 ret233 .p2align 4,,10234 .p2align 3235 .L39:236 movb $75, (%rdi)237 ret238 .p2align 4,,10239 .p2align 3240 .L38:241 movb $73, (%rdi)242 ret243 .p2align 4,,10244 .p2align 3245 .L37:246 movb $71, (%rdi)247 ret248 .p2align 4,,10249 .p2align 3250 .L36:251 movb $69, (%rdi)252 ret253 .p2align 4,,10254 .p2align 3255 .L35:256 movb $67, (%rdi)257 ret258 .p2align 4,,10259 .p2align 3260 .L34:261 movb $65, (%rdi)262 ret263 .p2align 4,,10264 .p2align 3265 .L33:266 movb $63, (%rdi)267 ret268 .p2align 4,,10269 .p2align 3270 .L32:271 movb $61, (%rdi)272 ret273 .p2align 4,,10274 .p2align 3275 .L31:276 movb $59, (%rdi)277 ret278 .p2align 4,,10279 .p2align 3280 .L30:281 movb $57, (%rdi)282 ret283 .p2align 4,,10284 .p2align 3285 .L29:286 movb $55, (%rdi)287 ret288 .p2align 4,,10289 .p2align 3290 .L28:291 movb $53, (%rdi)292 ret293 .p2align 4,,10294 .p2align 3295 .L27:296 movb $51, (%rdi)297 ret298 .p2align 4,,10299 .p2align 3300 .L26:301 movb $49, (%rdi)302 ret303 .p2align 4,,10304 .p2align 3305 .L25:306 movb $47, (%rdi)307 ret308 .p2align 4,,10309 .p2align 3310 .L24:311 movb $45, (%rdi)312 ret313 .p2align 4,,10314 .p2align 3315 .L23:316 movb $43, (%rdi)317 ret318 .p2align 4,,10319 .p2align 3320 .L22:321 movb $41, (%rdi)322 ret323 .p2align 4,,10324 .p2align 3325 .L21:326 movb $39, (%rdi)327 ret328 .p2align 4,,10329 .p2align 3330 .L20:331 movb $37, (%rdi)332 ret333 .p2align 4,,10334 .p2align 3335 .L19:336 movb $35, (%rdi)337 ret338 .p2align 4,,10339 .p2align 3340 .L18:341 movb $33, (%rdi)342 ret343 .p2align 4,,10344 .p2align 3345 .L17:346 movb $31, (%rdi)347 ret348 .p2align 4,,10349 .p2align 3350 .L16:351 movb $29, (%rdi)352 ret353 .p2align 4,,10354 .p2align 3355 .L15:356 movb $27, (%rdi)357 ret358 .p2align 4,,10359 .p2align 3360 .L14:361 movb $25, (%rdi)362 ret363 .p2align 4,,10364 .p2align 3365 .L13:366 movb $23, (%rdi)367 ret368 .p2align 4,,10369 .p2align 3370 .L12:371 movb $21, (%rdi)372 ret373 .p2align 4,,10374 .p2align 3375 .L11:376 movb $19, (%rdi)377 ret378 .p2align 4,,10379 .p2align 3380 .L10:381 movb $17, (%rdi)382 ret383 .p2align 4,,10384 .p2align 3385 .L9:386 movb $15, (%rdi)387 ret388 .p2align 4,,10389 .p2align 3390 .L8:391 movb $13, (%rdi)392 ret393 .p2align 4,,10394 .p2align 3395 .L7:396 movb $11, (%rdi)397 ret398 .p2align 4,,10399 .p2align 3400 .L6:401 movb $9, (%rdi)402 ret403 .p2align 4,,10404 .p2align 3405 .L5:406 movb $7, (%rdi)407 ret408 .p2align 4,,10409 .p2align 3410 .L3:411 movb $5, (%rdi)412 ret413 .p2align 4,,10414 .p2align 3415 .L2:416 movb $0, (%rdi)417 ret418 .cfi_endproc419 .LFE0:420 .size fun, .-fun421 .ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"422 .section .note.GNU-stack,"",@progbits
想了解下 GCC 对 switch 的优化,但是不懂 AT&T 伪汇编指令,帮忙分析下
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。