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. 网络 TCP三次握手,四次挥手详解

    三次握手,四次挥手可以说是炙手可热的面试题了,来看看它究竟长什么样子吧! 我们先把流程图贴上来 : 为什么这么复杂? 因为TCP是可靠性传输. 确认可靠传输的前提:  TCP连接管理机制 用TCP首部 ...

  2. 计数器的Verilog写法

    计数器是非常基本的使用,没有计数器就无法处理时序.我在学习时发现市面上有几种不同的计数器写法,非常有趣,在此记录下来: 一.时序逻辑和组合逻辑彻底分开(by锆石科技FPGA教程) 1.代码 //=== ...

  3. Intel realSense ubuntu 16.04+python 环境配置指南

    1. 安装librealsense2-dkms 以及librealsense2-utils 1.Register the server's public key: sudo apt-key adv - ...

  4. Java的集合整理

    1.List和Set都是接口,他们都继承于接口Collection,List是一个有序的可重复的集合,而Set的无序的不可重复的集合.Collection是集合的顶层接口,Collections是一个 ...

  5. IdentityServer4:发布环境的数字签名证书

    一,jwt的三个组成部件 先来看一个由IdentityServer颁发的一个标准令牌 eyJhbGciOiJSUzI1NiIsImtpZCI6IjBiNTE3ZjIzYWY0OGM4ZjkyZjExM ...

  6. java 使用GraphQL-关联对象

    GraphQL并不会实现关联查询,数据关联需要程序自己实现 官网首页有介绍获取多个资源只需要一个请求,如想获取用户信息和身份证信息,原来需要先查用户信息,再通过用户id查询身份证信息,而在GraphQ ...

  7. 用JS实现输出两个数范围内的随机数

    const rs = require("readline-sync"); function roundNum(min = 0, max = 0) { if (!isNaN(min) ...

  8. Web应用的生命周期(客户端)

    典型的一个Web应用的生命周期从用户在浏览器输入一串URL,或者单击一个链接开始(就是访问一个页面).而这个生命周期的结束就是我们关闭这个页面. 响应流程: 用户输入一个 URL(生命周期开始) 客户 ...

  9. 结合模板导出PDF文件

    @Action("report_exportJasperPdf")    public String exportJasperPdf() throws Exception{     ...

  10. thinkphp5中使用phpmailer实现发送邮件功能

    一.开启SMTP服务(使用php发送邮件需要用到SMTP服务,这里以163邮箱的SMTP服务为例). 1.登录163邮箱,在首页上找到“设置”. 2.选择开启的服务,一般都全选,POP3/SMTP/I ...