for
#include <iostream>
int main()
{
int j = 0;
for (int i = 0; i < 100; i++)
{
j++;
std::cout << j;
}
}
for汇编
//开栈
00007FF722991002 sub rsp,20h
//清空ebx
00007FF722991006 xor ebx,ebx
//对齐
00007FF722991008 nop dword ptr [rax+rax+0000000000000000h]
//下面CALL的第二个参数 RCX
00007FF722991010 mov rcx,qword ptr [00007FF722992088h]
//EBX = EBX + 1
00007FF722991017 inc ebx
//下面CALL的第一参数EDX = EBX + 1
00007FF722991019 mov edx,ebx
//调用std::cout这个方法
00007FF72299101B call qword ptr [00007FF722992080h]
//EBX和100比大小
00007FF722991021 cmp ebx,64h
//如果EBX低于100就跳到00007FF722991010
00007FF722991024 jl 00007FF722991010
//清空eax
00007FF722991026 xor eax,eax
//清栈
00007FF722991028 add rsp,20h
//出栈 还原RBX
00007FF72299102C pop rbx
//返回
00007FF72299102D ret
while
#include <iostream>
int main()
{
int j = 0;
while (j < 100)
{
j++;
std::cout << j;
}
}
while汇编
00007FF7BD8C1002 sub rsp,20h
00007FF7BD8C1006 xor ebx,ebx
00007FF7BD8C1008 nop dword ptr [rax+rax+0000000000000000h]
00007FF7BD8C1010 mov rcx,qword ptr [00007FF7BD8C2088h]
00007FF7BD8C1017 inc ebx
00007FF7BD8C1019 mov edx,ebx
00007FF7BD8C101B call qword ptr [00007FF7BD8C2080h]
00007FF7BD8C1021 cmp ebx,64h
00007FF7BD8C1024 jl 00007FF7BD8C1010
00007FF7BD8C1026 xor eax,eax
00007FF7BD8C1028 add rsp,20h
00007FF7BD8C102C pop rbx
00007FF7BD8C102D ret
dowhile
#include <iostream>
int main()
{
int j = 0;
do
{
j++;
std::cout << j;
} while (j < 100);
}
dowhile汇编
00007FF68DA91002 sub rsp,20h
00007FF68DA91006 xor ebx,ebx
00007FF68DA91008 nop dword ptr [rax+rax+0000000000000000h]
00007FF68DA91010 mov rcx,qword ptr [00007FF68DA92088h]
00007FF68DA91017 inc ebx
00007FF68DA91019 mov edx,ebx
00007FF68DA9101B call qword ptr [00007FF68DA92080h]
00007FF68DA91021 cmp ebx,64h
00007FF68DA91024 jl 00007FF68DA91010
00007FF68DA91026 xor eax,eax
00007FF68DA91028 add rsp,20h
00007FF68DA9102C pop rbx
00007FF68DA9102D ret
总结
在Visual Studio 2019 (v142)下 for while dowhile 的生成的指令是一样的 ,所以用新的编译器生成的循环代码其实是一样的了,所以在新版本的编译器下使用FOR WHILE dowhile没有必要在意了
|