if-else依次不断比较;switch直接计算相对地址,条件较少时与if-else差不多
int main ( )
{
00F 31070 push ebp
00F 31071 mov ebp, esp
00F 31073 sub esp, 0 Ch int no = 4 ;
00F 31076 mov dword ptr [ no] , 4 switch ( no) {
00F 3107D mov eax, dword ptr [ no]
00F 31080 mov dword ptr [ ebp- 4 ] , eax
00F 31083 mov ecx, dword ptr [ ebp- 4 ]
00F 31086 sub ecx, 1
00F 31089 mov dword ptr [ ebp- 4 ] , ecx
00F 3108C cmp dword ptr [ ebp- 4 ] , 4
00F 31090 ja $LN8+ 0F h ( 0F 310E7h)
00F 31092 mov edx, dword ptr [ ebp- 4 ]
00F 31095 jmp dword ptr [ edx* 4 + 0F 31104h]
case 1 : printf ( "no is 1\n" ) ;
00F 3109C push 0F 32100h
00F 310A1 call printf ( 0F 31010h)
00F 310A6 add esp, 4 break ;
00F 310A9 jmp $LN8+ 1 Ch ( 0F 310F4h) case 2 : printf ( "no is 2\n" ) ;
00F 310AB push 0F 3210Ch
00F 310B0 call printf ( 0F 31010h)
00F 310B5 add esp, 4 break ;
00F 310B8 jmp $LN8+ 1 Ch ( 0F 310F4h) case 3 : printf ( "no is 3\n" ) ;
00F 310BA push 0F 32118h
00F 310BF call printf ( 0F 31010h)
00F 310C4 add esp, 4 break ;
00F 310C7 jmp $LN8+ 1 Ch ( 0F 310F4h) case 4 : printf ( "no is 4\n" ) ;
00F 310C9 push 0F 32124h
00F 310CE call printf ( 0F 31010h)
00F 310D3 add esp, 4 break ;
00F 310D6 jmp $LN8+ 1 Ch ( 0F 310F4h) case 5 : printf ( "no is 5\n" ) ;
00F 310D8 push 0F 32130h
00F 310DD call printf ( 0F 31010h)
00F 310E2 add esp, 4 break ;
00F 310E5 jmp $LN8+ 1 Ch ( 0F 310F4h) default : printf ( "no is other\n" ) ;
00F 310E7 push 0F 3213Ch
00F 310EC call printf ( 0F 31010h)
00F 310F1 add esp, 4 break ; } int b = 3 ;
00F 310F4 mov dword ptr [ b] , 3 return 0 ;
00F 310FB xor eax, eax
}
int no = 4 ; switch ( no) { case 3 : printf ( "no is 3\n" ) ; break ; case 5 : printf ( "no is 5\n" ) ; break ; case 7 : printf ( "no is 7\n" ) ; break ; case 9 : printf ( "no is 9\n" ) ; break ; case 10 : printf ( "no is 10\n" ) ; break ; default : printf ( "no is other\n" ) ; break ; } 009E107 D mov eax, dword ptr [ no]
009E1080 mov dword ptr [ ebp- 4 ] , eax
009E1083 mov ecx, dword ptr [ ebp- 4 ]
009E1086 sub ecx, 3
009E1089 mov dword ptr [ ebp- 4 ] , ecx
009E108 C cmp dword ptr [ ebp- 4 ] , 7
009E1090 ja $LN8+ 0F h ( 09E10 E7h)
009E1092 mov edx, dword ptr [ ebp- 4 ]
009E1095 jmp dword ptr [ edx* 4 + 9E1104 h]
switch ( no) { case 3 : printf ( "no is 3\n" ) ; break ; case 5 : printf ( "no is 5\n" ) ; break ; case 20 : printf ( "no is 20\n" ) ; break ; case 22 : printf ( "no is 22\n" ) ; break ; case 23 : printf ( "no is 23\n" ) ; break ; default : printf ( "no is other\n" ) ; break ; } switch ( no) {
004 A107D mov eax, dword ptr [ no]
004 A1080 mov dword ptr [ ebp- 4 ] , eax
004 A1083 mov ecx, dword ptr [ ebp- 4 ]
004 A1086 sub ecx, 3
004 A1089 mov dword ptr [ ebp- 4 ] , ecx
004 A108C cmp dword ptr [ ebp- 4 ] , 14 h
004 A1090 ja $LN9 ( 04 A10EEh)
004 A1092 mov edx, dword ptr [ ebp- 4 ]
004 A1095 movzx eax, byte ptr [ edx+ 4 A1120h]
004 A109C jmp dword ptr [ eax* 4 + 4 A1108h]
跨度非常大switch和if-else差不多,不优化