debug
- do···while
23: int nSum = 0; 00A572AE C7 45 F8 00 00 00 00 mov dword ptr [nSum],0 24: int nIndex = 0; 00A572B5 C7 45 EC 00 00 00 00 mov dword ptr [nIndex],0 25: do 26: { 27: nSum += nIndex; 00A572BC 8B 45 F8 mov eax,dword ptr [nSum] 00A572BF 03 45 EC add eax,dword ptr [nIndex] 00A572C2 89 45 F8 mov dword ptr [nSum],eax 28: nIndex++; 00A572C5 8B 45 EC mov eax,dword ptr [nIndex] 00A572C8 83 C0 01 add eax,1 00A572CB 89 45 EC mov dword ptr [nIndex],eax 29: } while(nIndex <= nCount); 00A572CE 8B 45 EC mov eax,dword ptr [nIndex] 00A572D1 3B 45 08 cmp eax,dword ptr [nCount]//先执行循环体,后判断 00A572D4 7E E6 jle LoopDO+2Ch (0A572BCh) 30: return nSum; 00A572D6 8B 45 F8 mov eax,dword ptr [nSum]
- while
34: int nSum = 0; 00A5738E C7 45 F8 00 00 00 00 mov dword ptr [nSum],0 35: int nIndex = 0; 00A57395 C7 45 EC 00 00 00 00 mov dword ptr [nIndex],0 36: while (nIndex <= nCount) 00A5739C 8B 45 EC mov eax,dword ptr [nIndex] 00A5739F 3B 45 08 cmp eax,dword ptr [nCount] //先判断,后循环 00A573A2 7F 14 jg LoopWhile+48h (0A573B8h) 37: { 38: nSum += nIndex; 00A573A4 8B 45 F8 mov eax,dword ptr [nSum] 00A573A7 03 45 EC add eax,dword ptr [nIndex] 00A573AA 89 45 F8 mov dword ptr [nSum],eax 39: nIndex++; 00A573AD 8B 45 EC mov eax,dword ptr [nIndex] 00A573B0 83 C0 01 add eax,1 00A573B3 89 45 EC mov dword ptr [nIndex],eax 40: } 00A573B6 EB E4 jmp LoopWhile+2Ch (0A5739Ch) 41: return nSum; 00A573B8 8B 45 F8 mov eax,dword ptr [nSum]
- for
46: int nSum = 0; 00A5731E C7 45 F8 00 00 00 00 mov dword ptr [nSum],0 47: for (int nIndex = 0; nIndex <= nCount; ++nIndex) 00A57325 C7 45 EC 00 00 00 00 mov dword ptr [ebp-14h],0 //先初始化计数器变量 00A5732C EB 09 jmp LoopFor+37h (0A57337h) 00A5732E 8B 45 EC mov eax,dword ptr [ebp-14h] 00A57331 83 C0 01 add eax,1 //步长 00A57334 89 45 EC mov dword ptr [ebp-14h],eax 00A57337 8B 45 EC mov eax,dword ptr [ebp-14h] 00A5733A 3B 45 08 cmp eax,dword ptr [nCount] //判断循环条件 00A5733D 7F 0B jg LoopFor+4Ah (0A5734Ah) 48: { 49: nSum += nIndex; 00A5733F 8B 45 F8 mov eax,dword ptr [nSum] 00A57342 03 45 EC add eax,dword ptr [ebp-14h] 00A57345 89 45 F8 mov dword ptr [nSum],eax 50: } 00A57348 EB E4 jmp LoopFor+2Eh (0A5732Eh) 51: return nSum; 00A5734A 8B 45 F8 mov eax,dword ptr [nSum]
release
int GoToDo(int nCount)
{
int nSum = 0;
int nIndex = 0;
GOTO_DO:
nSum += nIndex;
nIndex++;
if (nIndex <= nCount)
{
goto GOTO_DO;
}
return nSum;
}
printf("%d ", GoToDo(5));
00D01143 | 33C9 | xor ecx,ecx | looptype.cpp:83 00D01145 | 33C0 | xor eax,eax | 00D01147 | 03C8 | add ecx,eax | 00D01149 | 40 | inc eax | 00D0114A | 83F8 05 | cmp eax,0x5 | 00D0114D | 7E F8 | jle looptype.D01147 | 00D0114F | 51 | push ecx | 00D01150 | 68 9401D400 | push looptype.D40194 | D40194:"%d " 00D01155 | E8 76000000 | call <looptype.printf> | 00D0115A | 83C4 08 | add esp,0x8 |
int LoopDO(int nCount)
{
int nSum = 0;
int nIndex = 0;
do
{
nSum += nIndex;
nIndex++;
} while(nIndex <= nCount);
return nSum;
}
printf("%d ", LoopDO(5));
00D0115D | 33C9 | xor ecx,ecx | looptype.cpp:84 00D0115F | 33C0 | xor eax,eax | 00D01161 | 03C8 | add ecx,eax | 00D01163 | 40 | inc eax | 00D01164 | 83F8 05 | cmp eax,0x5 | 00D01167 | 7E F8 | jle looptype.D01161 | 00D01169 | 51 | push ecx | 00D0116A | 68 9401D400 | push looptype.D40194 | D40194:"%d " 00D0116F | E8 5C000000 | call <looptype.printf> |
// 强度降低
void DoRate(int argc)
{
int t = 0;
int i = 0;
while (t < argc)
{
t = i * 99;
i++;
}
printf("%d", t);
}
00D011A6 | 8B55 08 | mov edx,dword ptr ss:[eb | looptype.cpp:88 00D011A9 | 83C4 08 | add esp,0x8 | looptype.cpp:87 00D011AC | 33C9 | xor ecx,ecx | 00D011AE | 85D2 | test edx,edx |//while优化为do···while结构,添加一个判断 00D011B0 | 7E 0B | jle looptype.D011BD | 00D011B2 | 33C0 | xor eax,eax | 00D011B4 | 8BC8 | mov ecx,eax | 00D011B6 | 83C0 63 | add eax,0x63 |循环体, *99优化为+99 00D011B9 | 3BCA | cmp ecx,edx | 00D011BB | 7C F7 | jl looptype.D011B4 | 00D011BD | 51 | push ecx | 00D011BE | 68 9001D400 | push looptype.D40190 | D40190:"%d" 00D011C3 | E8 08000000 | call <looptype.printf> | 00D011C8 | 83C4 08 | add esp,0x8 |