do while

效率是最高的

#include "pch.h"
#include <iostream> int main()
{
int nVarTemp = ;
int nSum = ;
do {
nSum += nVarTemp;
nVarTemp++;
} while (nVarTemp < );
printf("nSum = %d;nVarTemp = %d", nSum, nVarTemp);
std::cout << "Hello World!\n";
}

用ida打开

.text:                 push    ebp
.text: mov ebp, esp
.text: sub esp, 0D8h
.text: push ebx
.text:0041264A push esi
.text:0041264B push edi
.text:0041264C lea edi, [ebp+var_D8]
.text: mov ecx, 36h
.text: mov eax, 0CCCCCCCCh
.text:0041265C rep stosd
.text:0041265E mov ecx, offset unk_41E008
.text: call sub_41127B
.text: mov [ebp+var_8],
.text:0041266F mov [ebp+var_14],
.text:
.text: loc_412676: ; CODE XREF: sub_412640+4C↓j
.text: mov eax, [ebp+var_14]
.text: add eax, [ebp+var_8]
.text:0041267C mov [ebp+var_14], eax
.text:0041267F mov eax, [ebp+var_8]
.text: add eax,
.text: mov [ebp+var_8], eax
.text: cmp [ebp+var_8], 65h
.text:0041268C jl short loc_412676
.text:0041268E mov eax, [ebp+var_8]
.text: push eax
.text: mov ecx, [ebp+var_14]
.text: push ecx
.text: push offset aNsumDNvartempD ; "nSum = %d;nVarTemp = %d"
.text:0041269B call sub_411055
.text:004126A0 add esp, 0Ch
.text:004126A3 push offset Str ; "Hello World!\n"
.text:004126A8 mov eax, ds:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::basic_ostream<char,std::char_traits<char>> std::cout
.text:004126AD push eax ; int
.text:004126AE call sub_411212
.text:004126B3 add esp,
.text:004126B6 xor eax, eax
.text:004126B8 pop edi
.text:004126B9 pop esi
.text:004126BA pop ebx
.text:004126BB add esp, 0D8h
.text:004126C1 cmp ebp, esp
.text:004126C3 call sub_411285
.text:004126C8 mov esp, ebp
.text:004126CA pop ebp
.text:004126CB retn
.text:004126CB sub_412640 endp

while

int main()
{
int nVarTemp = ;
int nSum = ;
while (nVarTemp < )
{
nSum += nVarTemp;
nVarTemp++;
}
printf("nSum = %d;nVarTemp = %d", nSum, nVarTemp);
std::cout << "Hello World!\n";
}

debug版本

.text:
.text: var_D8 = byte ptr -0D8h
.text: var_14 = dword ptr -14h
.text: var_8 = dword ptr -
.text:
.text: push ebp
.text: mov ebp, esp
.text: sub esp, 0D8h
.text: push ebx
.text:0041264A push esi
.text:0041264B push edi
.text:0041264C lea edi, [ebp+var_D8]
.text: mov ecx, 36h
.text: mov eax, 0CCCCCCCCh
.text:0041265C rep stosd
.text:0041265E mov ecx, offset unk_41E009
.text: call sub_41127B
.text: mov [ebp+var_8],
.text:0041266F mov [ebp+var_14],
.text:
.text: loc_412676: ; CODE XREF: sub_412640+4E↓j
.text: cmp [ebp+var_8], 65h
.text:0041267A jge short loc_412690
.text:0041267C mov eax, [ebp+var_14]
.text:0041267F add eax, [ebp+var_8]
.text: mov [ebp+var_14], eax
.text: mov eax, [ebp+var_8]
.text: add eax,
.text:0041268B mov [ebp+var_8], eax
.text:0041268E jmp short loc_412676
.text: ; ---------------------------------------------------------------------------
.text:
.text: loc_412690: ; CODE XREF: sub_412640+3A↑j
.text: mov eax, [ebp+var_8]
.text: push eax
.text: mov ecx, [ebp+var_14]
.text: push ecx
.text: push offset aNsumDNvartempD ; "nSum = %d;nVarTemp = %d"
.text:0041269D call sub_411055
.text:004126A2 add esp, 0Ch
.text:004126A5 push offset Str ; "Hello World!\n"
.text:004126AA mov eax, ds:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@@A ; std::basic_ostream<char,std::char_traits<char>> std::cout
.text:004126AF push eax ; int
.text:004126B0 call sub_411212
.text:004126B5 add esp,
.text:004126B8 xor eax, eax
.text:004126BA pop edi
.text:004126BB pop esi
.text:004126BC pop ebx
.text:004126BD add esp, 0D8h
.text:004126C3 cmp ebp, esp
.text:004126C5 call sub_411285
.text:004126CA mov esp, ebp
.text:004126CC pop ebp
.text:004126CD retn
.text:004126CD sub_412640 endp

release版本,编译器对while语句进行优化,优化成do while结构

text: _main           proc near               ; CODE XREF: __scrt_common_main_seh+F5↓p
.text: push esi ; _Val
.text: xor edx, edx
.text: xor esi, esi
.text: xor eax, eax
.text:
.text: loc_401047: ; CODE XREF: _main+↓j
.text: inc esi
.text: add edx, eax
.text:0040104A add esi, eax
.text:0040104C add eax,
.text:0040104F cmp eax, 64h
.text: jl short loc_401047
.text: cmp eax, 65h
.text: lea ecx, [eax+]
.text:0040105A cmovge ecx, eax
.text:0040105D push ecx
.text:0040105E xor ecx, ecx
.text: cmp eax, 65h
.text: cmovge eax, ecx
.text: add eax, esi
.text: add eax, edx
.text:0040106A push eax
.text:0040106B push offset _Format ; "nSum = %d;nVarTemp = %d"
.text: call _printf
.text: mov ecx, ds:__imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@@A.gap0 ; _Ostr
.text:0040107B add esp, 0Ch
.text:0040107E call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@@AAV10@PBD@Z ; std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char const *)
.text: xor eax, eax
.text: pop esi
.text: retn
.text: _main endp

for

效率比较低

int main()
{
int nVarTemp = ;
int nSum = ;
for (;nVarTemp < ;)
{
nSum += nVarTemp;
nVarTemp++;
}
printf("nSum = %d;nVarTemp = %d", nSum, nVarTemp);
std::cout << "Hello World!\n";
}

debug

.text:
.text: var_D8 = byte ptr -0D8h
.text: var_14 = dword ptr -14h
.text: var_8 = dword ptr -
.text:
.text: push ebp
.text: mov ebp, esp
.text: sub esp, 0D8h
.text: push ebx
.text:0041264A push esi
.text:0041264B push edi
.text:0041264C lea edi, [ebp+var_D8]
.text: mov ecx, 36h
.text: mov eax, 0CCCCCCCCh
.text:0041265C rep stosd
.text:0041265E mov ecx, offset unk_41E008
.text: call sub_41127B
.text: mov [ebp+var_8],
.text:0041266F mov [ebp+var_14],
.text:
.text: for_if: ; CODE XREF: sub_412640+4E↓j
.text: cmp [ebp+var_8], 65h
.text:0041267A jge short for_end
.text:0041267C mov eax, [ebp+var_14]
.text:0041267F add eax, [ebp+var_8]
.text: mov [ebp+var_14], eax
.text: mov eax, [ebp+var_8]
.text: add eax,
.text:0041268B mov [ebp+var_8], eax
.text:0041268E jmp short for_if
.text: ; ---------------------------------------------------------------------------
.text:
.text: for_end: ; CODE XREF: sub_412640+3A↑j
.text: mov eax, [ebp+var_8]
.text: push eax
.text: mov ecx, [ebp+var_14]
.text: push ecx
.text: push offset aNsumDNvartempD ; "nSum = %d;nVarTemp = %d"
.text:0041269D call sub_411055
.text:004126A2 add esp, 0Ch
.text:004126A5 push offset Str ; "Hello World!\n"
.text:004126AA mov eax, ds:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@@A ; std::basic_ostream<char,std::char_traits<char>> std::cout
.text:004126AF push eax ; int
.text:004126B0 call sub_411212
.text:004126B5 add esp,
.text:004126B8 xor eax, eax
.text:004126BA pop edi
.text:004126BB pop esi
.text:004126BC pop ebx
.text:004126BD add esp, 0D8h
.text:004126C3 cmp ebp, esp
.text:004126C5 call sub_411285
.text:004126CA mov esp, ebp
.text:004126CC pop ebp
.text:004126CD retn
.text:004126CD sub_412640 endp

同样编译器对release版本,将for循环优化为 do while 结构

.text: ; int __cdecl main()
.text: _main proc near ; CODE XREF: __scrt_common_main_seh+F5↓p
.text: push esi ; _Val
.text: xor edx, edx
.text: xor esi, esi
.text: xor eax, eax
.text:
.text: loc_401047: ; CODE XREF: _main+12↓j
.text: inc esi
.text: add edx, eax
.text:0040104A add esi, eax
.text:0040104C add eax,
.text:0040104F cmp eax, 64h
.text: jl short loc_401047
.text: cmp eax, 65h
.text: lea ecx, [eax+]
.text:0040105A cmovge ecx, eax
.text:0040105D push ecx
.text:0040105E xor ecx, ecx
.text: cmp eax, 65h
.text: cmovge eax, ecx
.text: add eax, esi
.text: add eax, edx
.text:0040106A push eax
.text:0040106B push offset _Format ; "nSum = %d;nVarTemp = %d"
.text: call _printf
.text: mov ecx, ds:__imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A.gap0 ; _Ostr
.text:0040107B add esp, 0Ch
.text:0040107E call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char const *)
.text: xor eax, eax
.text: pop esi
.text: retn
.text: _main endp
.text:

C++反汇编中的循环语句的更多相关文章

  1. js中的循环语句

    js中的循环语句可分为三种:1.while:2.do……while:3.for. while的语法为 while (exp) {    //statements;} var a=1,b=0; whil ...

  2. shell脚本中select循环语句用法

    shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...

  3. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  4. Swift中的循环语句

    循环语句能够使程序代码重复执行.Swift编程语言支持4种循环构造类型:while.do while.for和for in.for和while循环是在执行循环体之前测试循环条件,而do while是在 ...

  5. shell编程中的循环语句

    while循环直接从文件中读取 while read line do command done < filename until循环 until 条件 do command done for循环 ...

  6. 洗礼灵魂,修炼python(10)--有趣的判断分支+从实例中掌握循环语句

    所有的编程语言里都有判断语句和循环语句. 判断语句则是用来分支程序流程的 循环语句则是为了实现一个效果,让程序的规律性的重复操作 不用说,分支和循环自然在python里也是有的 一,条件判断:if,i ...

  7. Shell编程-08-Shell中的循环语句

    目录 while语句 until语句 for语句 select语句 循环中断控制 循环语句总结     循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行.在Shell中常见的 ...

  8. Shell中的循环语句实例

    1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do     echo number $x done 注:" ...

  9. Oracle--存储过程中之循环语句

    一般循环语句有两种: 1)使用for循环实现 declare  cursor cur is    select * from tablename;   aw_row  tablename%rowtyp ...

随机推荐

  1. 同一个Tomcat部署多个springboot项目问题

    2018-12-13 10:37:21,412 ERROR [localhost-startStop-2] c.a.d.s.DruidDataSourceStatManager [DruidDataS ...

  2. python 基础(实例1——登陆小游戏)

    一个简单的登陆小游戏,输入用户名和密码,如果和user_passwd.txt中内容匹配,则打印“welcome to login...”,如果三次输入错误则将该用户列入黑名单,无法再用该用户名登陆. ...

  3. jq + 面向对象实现拼图游戏

    jq + 面向对象实现拼图游戏 知识点 拖拽事件 es6面向对象 jquery事件 效果图 html: <div class="wraper"> <div cla ...

  4. mysql 数据库 规范

    目录 mysql 数据库 规范 基础规范 命名规范 表设计规范 字段设计规范 索引设计规范 SQL编写规范 行为规范 mysql 数据库 规范 基础规范 必须使用InnoDB存储引擎 解读:支持事务. ...

  5. SIM900 HTTP POST

    AT+SAPBR=3,1,"CONTYPE","GPRS" OK AT+SAPBR=3,1,"APN","CMNET" ...

  6. C# 利用特性(Attribute)实现通用实体类数据合法校验

    用过asp.net mvc 的都应该知道,在实体类上添加一些特性,可以实现后端实体的数据校验,这里简单实现一下 实现原理:利用反射获取实体的每一个属性,并通过属性获取属性上标注的特性,调用特性的Val ...

  7. 背部——哑铃&杠铃

  8. DELL R730 做raid10

    1.服务器开机,在出现下图提示时,同时按着<ctrl >+ < R >键,即可进入配置界面 2.会进入下图 3.按上下键到第一项PERC H730P MINI ,按F2,选择c ...

  9. edgex简述

    一.概述 Edgex foundry是一个Linux 基金会运营的开源边缘计算物联网软件框架项目,该项目的核心是基于与硬件和操作系统完全无关的参考软件平台建立的互操作框架,使能即插即用的组件生态系统, ...

  10. 运维开发笔记整理-Django模型语法

    运维开发笔记整理-Django模型语法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模型基本概念 1>.什么是模型 模型是你的数据唯一的,权威的信息源.它包含你所存储数 ...