goto模拟循环

来源:互联网 发布:怎样查看淘宝价格曲线 编辑:程序博客网 时间:2024/05/17 09:17

0为false 1为true

for的汇编代码
for (int i = 0; i < 5; i++){}
00401048   mov         dword ptr [ebp-4],0    //dword ptr 表示i为整型,ebp-4偏移到变量i
0040104F   jmp         forGoto+2Ah (0040105a) //goto偏移2A位到0040105A
00401051   mov         eax,dword ptr [ebp-4]  //eax = i
00401054   add         eax,1                  //eax++
00401057   mov         dword ptr [ebp-4],eax  //i = eax
0040105A   cmp         dword ptr [ebp-4],5    //比较整型i和5
0040105E   jge         forGoto+32h (00401062) //为偶则执行goto偏移32
00401060   jmp         forGoto+21h (00401051) //goto偏移21

C语言GOTO
{
    int i = 0;
    goto CMP;
MOV:
    i++;   
CMP:
    if(i >= 5)
    {
        goto JMP:
    }
    goto MOV;
JMP:
}

  

while的汇编代码
int i = 0;
00401098   mov         dword ptr [ebp-4],0
while (i < 5)
0040109F   cmp         dword ptr [ebp-4],5
004010A3   jge         whileGoto+30h (004010b0)
{
    i++;
004010A5   mov         eax,dword ptr [ebp-4]
004010A8   add         eax,1
004010AB   mov         dword ptr [ebp-4],eax
}
004010AE   jmp         whileGoto+1Fh (0040109f)

C语言GOTO
{
    int i = 0;
CMP:
    if(i >= 5)
    {
        goto JMP;
    }
    i++;
    goto CMP
JMP:
}

do while汇编代码
int i = 0;
004010E8   mov         dword ptr [ebp-4],0
do
{
    i++;
004010EF   mov         eax,dword ptr [ebp-4]
004010F2   add         eax,1
004010F5   mov         dword ptr [ebp-4],eax
} while (i < 5);
004010F8   cmp         dword ptr [ebp-4],5
004010FC   jl          doWhileGoto+1Fh (004010ef) //小于则jmp

C语言GOTO
{
    int i = 0;
MOV:
    i++;
    if(i < 5)
    {
        goto MOV;
    }
}

总结do while是效率最高的.