As we all know, any program can be composed of three kinds of basic control structure, namely sequential structure, selection structure and cyclic structure.
What is the translation of these three structures into assembly languages? The main consideration here is the debug version. For release versions, the results are different after various optimizations and are not considered. The compiler here uses the Visual Studio
There is no suspense in the order structure, here does not mention, first of all look at the selection structure.
There are two main ways to choose a structure:if{}else if{} else{} and switch{case:case:default:}
First of all, look.
if judgment branch
if (a > 0 && B < 0) 00182DCC cmp dword ptr [a],0; two judgements, non-canonical jumps to the next else if 00182dd0 J Le foo+51h (182df1h) 00182DD2 cmp dword ptr [b],0 00182dd6 jge foo+51h (182df1h) { ; Do not skip, execute the contents of the Code block printf ("if (a > 0 && B < 0)"); 00182DD8 mov esi,esp 001 82DDA Push Offset string "if (a > 0 && B < 0)" (185974h) 00182DDF call DWORD ptr [__IMP__PRI NTF (1882b4h)] 00182DE5 add esp,4 00182de8 cmp esi,esp 00182DEA call @ILT +450 (__RTC_CHECKESP) (1 811c7h) 00182DEF jmp foo+87h (182e27h); Execute, jump out of If}else if (a < 0) Or judge, the irregularities will jump to the next ELSE00182DF1 cmp dword ptr [a],0 00182df5 jge foo+70h (182e10h) {printf ("else If" (a < ; 0); 00182DF7 mov esi,esp 00182df9 push offset string "else if (a < 0)" (1857a8h) 00182DFE call DWORD ptr [__IMP__PRINTF (1882b4h)] 00182E04 add esp,4 00182E07 cmp esi,esp 00182E09 call @ILT +450 (__RTC_CHECKESP) ( 1811c7h)}else00182e0e jmp foo+87h (182e27h); I have this statement in the previous else if it is more appropriate {printf ("else"); 00182E10 mov Esi,esp 00182E12 Push offset string "else" (1857a0h) 00182E17 call DWORD ptr [__imp__printf (1882b4h)] 00 182e1d add esp,4 00182E20 cmp esi,esp 00182E22 call @ILT +450 (__RTC_CHECKESP) (1811c7h)}
An if loop can be written casually , and a comparison of if will make the code less, but for the sake of presentation, it doesn't matter. Code analysis also feel simple, is the beginning to see a little trouble.
CMP < conditions >; How many criteria to judge jump Jle < Next branch > ; here is usually the opposite of what you would see in C + + ... cmp < conditions >jle < Next branch > (code block) JMP < If outer >; last if (else) code block does not have this one
Switch-caseJudging Branch
Switch (a) 00E82DC5 mov eax,dword ptr [a] 00e82dc8 mov dword ptr [Ebp-0d0h],eax 00e82dce CMP DWORD ptr [ebp-0d0h],0 ; Judge Jump very frequently, first consider is Switch00e82dd5 je foo+4bh (0e82debh) 00E82DD7 cmp DWORD ptr [ebp-0d0h],1 00e82dde je foo+52h (0e82df2h) ; These are eligible to jump to the corresponding code block 00E82DE0 cmp DWORD ptr [ebp-0d0h],2 00e82de7 je foo+5bh (0e82dfbh) 00e82de9 jmp foo+64h (0e82e04h) ; without qualifying, skip to Default{case 0:a = 0;00e82deb mov dword ptr [a],0 ; There is no break, continue down execution case 1:a =1;00E82DF2 mov DWORD ptr [a],1 break;00e82df9 jmp foo+6bh (0E82E0BH) ; break, jump out switchcase 2:a =2;00e82dfb mov DWORD ptr [a],2 break;00e82e02 jmp foo+6bh (0E82E0BH) default:a =3;00e82e04 mov dword ptr [a ],3}
Continuous comparison and conditional jump, easily reminiscent of switch for the code block is also relatively simple has a break will add an unconditional jump
Next look at the loop structure,
There are three main types of loop structure: for loop, while loop , Do-while loop.
As for some repeat until in other languages , do not consider it, please analyze it yourself.
For Loop
for (int i = 0;i< 5;i++) 00db17ce mov dword ptr [i],0 00db17d5 jmp foo+30h (0db17e0h) 00db17d7 mov Eax,dword ptr [i] 00db17da add eax,1 00db17dd mov dword ptr [I],eax 00DB17E0 CMP DWORD ptr [i],5 00db17e4 jge foo+53h (0db1803h) {printf ("%d", i); 00db17e6 mov esi,esp 00db17e8 mov eax,dword ptr [i] 00db17eb push eax 00db17ec push offset string "%d" (0db573ch) 00DB17F1 Call dword ptr [__imp__printf (0db82b4h)] 00db17f7 add esp,8 00db17fa cmp Esi,esp 00db17fc Call @ILT +450 (__RTC_CHECKESP) (0db11c7h) 00db1801 jmp foo+27h (0db17d7h)}
The code is very simple, translated into a compilation, you have to change the thinking, here summed up a rule out will become simple. The following is a basic framework:
for (Part one; Part two; Part III) {loop body;}
mov < loop variable >,< initial value >; first part. Assign the initial value to the loop variable jmp B; jumps to the first loop, executes the second part A: (changes the loop variable), and the third part. Modify loop variable b:cmp < loop variable >,< limit variable >; second part. Check the loop variable Jge jump out of the loop; Here the judging conditions are usually the opposite of what you see in for ... (Loop body) ... jmp A; skip back to the third part, modify the variable loop
WhileLoops
while (a > 0) 00852DC5 cmp dword ptr [a],0 ; first, the condition jumps out of the while code block 00852DC9 jle foo+36h (852dd6h) { A--;00852DCB mov eax,dword ptr [a] 00852DCE sub eax,1 00852dd1 mov dword ptr [A],eax} 00852dd4 jmp foo+25h (852dc5h) ; forced jump to the start of the transfer judgment
Frame is very simple, first judge, do not meet the criteria to jump out of the code block, or continue to execute, the code block finally jumped back to continue to judge.
a:cmp < loop variable >,< limit variable >jge b ; jump out of code block (loop body) ... jmp A ; jump back B: Loop over
Do-whileLoops
DO{A--;01362DC5 mov eax,dword ptr [a] 01362dc8 sub eax,1 01362DCB mov dword ptr [A],eax} while (a > 0); 01362DCE cmp dword ptr [a],0 01362dd2 JG
Do-while is much simpler, just move the judgment directly behind the code block.
a:cmp < loop variable >,< limit variable >jge B (loop body) ... jmp AB: End of cycle
Well, the basic introduction is over, this time just to warn us, in the Assembly, the Code of understanding and high-level language is a bit out of the way, to change the thinking. It also shows that the same logic can be expressed in a variety of ways.
In addition, this is just debug release version, the code will be changeable, for example, switch will probably be implemented using jump tables, and some if will be directly optimized out, after all, the use of pipeline speed will be greatly accelerated, suddenly a jump will break the pipeline.