the switch statements are used by programmers & malware authors to make a decision based on a character or integer.
SWITCH CASES
IF STYLE
from the disassembly, it is difficult to know whether the original code was a switch statement or a sequence of if statements because a compiled switch statement looks like a group of if statements.
the disassembly in this example switch statement is commonly found with large, contiguous switch statements. in this type, the compiler optimizes the code to avoid needing to make so many comparisons.
#C CODE SNIPPET
switch(i)
{
case 1:
printf("i = %d", i+1);
break;
case 2:
printf("i = %d", i+2);
break;
case 3:
printf("i = %d", i+3);
break;
case 4:
printf("i = %d", i+3);
break;
default:
break;
}
#ASSEMBLY CODE SNIPPET
00401016 sub ecx, 1
00401019 mov [ebp+var_8], ecx
0040101C cmp [ebp+var_8], 3
00401020 ja short loc_401082
00401022 mov edx, [ebp+var_8]
00401025 jmp ds:off_401088[edx*4] ❶
0040102C loc_40102C:
...
00401040 jmp short loc_401082
00401042 loc_401042:
...
00401056 jmp short loc_401082
00401058 loc_401058:
...
0040106C jmp short loc_401082
0040106E loc_40106E:
...
00401082 loc_401082:
00401082 xor eax, eax
00401084 mov esp, ebp
00401086 pop ebp
00401087 retn
00401087 _main endp
00401088 ❷off_401088 dd offset loc_40102C
0040108C dd offset loc_401042
00401090 dd offset loc_401058
00401094 dd offset loc_40106E
* the ecx in this example contains the switch variable, and 1 is subtracted from it in the first line