循环指令 | 结构 | 说明 |
---|---|---|
while | while(表达式){语句;} | 计算while后面括号里表达式的值,若其结果非0,则执行循环,否则不执行 |
do…while | do{语句;}while(表达式); | 先执行一次语句,然后计算while后面括号里表达式的值,若其结果非0,则执行循环,否则不执行 |
for | for(表达式1;表达式2;表达式3) 或 for( ; ; ) | for循环主要判断表达式2是否成立,表达式2成立则继续循环,不成立则跳出循环,for( ; ; )表达式2默认为真,一直循环 |
while:
void Function()
{
int b = 0;
int a = 0;
while(a<=5)
{
b++;
a++;
}
}
0040C3B8 mov dword ptr [ebp-4],0 b = 0
0040C3BF mov dword ptr [ebp-8],0 a = 0
0040C3C6 cmp dword ptr [ebp-8],5 if(a <= 5)
0040C3CA jg fu+40h (0040c3e0) 若ebp-8大于5则跳出循环部分
0040C3CC mov eax,dword ptr [ebp-4] eax = b = 0
0040C3CF add eax,1 eax = eax + 1
0040C3D2 mov dword ptr [ebp-4],eax b = eax = 1
0040C3D5 mov ecx,dword ptr [ebp-8] ecx = a = 0
0040C3D8 add ecx,1 ecx = ecx + 1
0040C3DB mov dword ptr [ebp-8],ecx a = ecx = 1
0040C3DE jmp fu+26h (0040c3c6) 强制跳转到0040c3c6
do…while:
void Function()
{
int a = 0;
int b = 0;
do
{
b++;
a++;
}while(a <= 5);
}
0040C3B8 mov dword ptr [ebp-4],0
0040C3BF mov dword ptr [ebp-8],0
0040C3C6 mov eax,dword ptr [ebp-8]
0040C3C9 add eax,1
0040C3CC mov dword ptr [ebp-8],eax
0040C3CF mov ecx,dword ptr [ebp-4]
0040C3D2 add ecx,1
0040C3D5 mov dword ptr [ebp-4],ecx
0040C3D8 cmp dword ptr [ebp-4],5
0040C3DC jle Function+26h (0040c3c6)
for
0040C3B8 mov dword ptr [ebp-4],0 b = 0
0040C3BF mov dword ptr [ebp-8],0 a = 0
0040C3C6 jmp Function+31h (0040c3d1) 跳转到0040C3D1 (i<9)
0040C3C8 mov eax,dword ptr [ebp-8]
0040C3CB add eax,1
0040C3CE mov dword ptr [ebp-8],eax
0040C3D1 cmp dword ptr [ebp-8],9 a=0 和 9 比较
0040C3D5 jge Function+42h (0040c3e2) 若大于等于则跳转到0040c3e2(跳出循环)
0040C3D7 mov ecx,dword ptr [ebp-4] ecx = b
0040C3DA add ecx,1 b = b+1
0040C3DD mov dword ptr [ebp-4],ecx b=ecx
0040C3E0 jmp Function+28h (0040c3c8) 跳转到0040c3c8(i++)
来源:CSDN
作者:初级猿人
链接:https://blog.csdn.net/qq_34250536/article/details/103951312