C++反汇编中的循环语句
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++反汇编中的循环语句的更多相关文章
- js中的循环语句
js中的循环语句可分为三种:1.while:2.do……while:3.for. while的语法为 while (exp) { //statements;} var a=1,b=0; whil ...
- shell脚本中select循环语句用法
shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...
- 详解Python中的循环语句的用法
一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...
- Swift中的循环语句
循环语句能够使程序代码重复执行.Swift编程语言支持4种循环构造类型:while.do while.for和for in.for和while循环是在执行循环体之前测试循环条件,而do while是在 ...
- shell编程中的循环语句
while循环直接从文件中读取 while read line do command done < filename until循环 until 条件 do command done for循环 ...
- 洗礼灵魂,修炼python(10)--有趣的判断分支+从实例中掌握循环语句
所有的编程语言里都有判断语句和循环语句. 判断语句则是用来分支程序流程的 循环语句则是为了实现一个效果,让程序的规律性的重复操作 不用说,分支和循环自然在python里也是有的 一,条件判断:if,i ...
- Shell编程-08-Shell中的循环语句
目录 while语句 until语句 for语句 select语句 循环中断控制 循环语句总结 循环语句常用于重复执行一条命令或一组命令等,直到达到结束条件后,则终止执行.在Shell中常见的 ...
- Shell中的循环语句实例
1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do echo number $x done 注:" ...
- Oracle--存储过程中之循环语句
一般循环语句有两种: 1)使用for循环实现 declare cursor cur is select * from tablename; aw_row tablename%rowtyp ...
随机推荐
- TZOJ5255: C++实验:三角形面积
#include<iostream> #include<iomanip> #include<math.h> #include<cmath> using ...
- 保研经验帖----江西师范大学 to 华中科技大学
呼,距离拿到华科cs的offer也有一段时间,有好几次准备动手写这篇经验帖,但就是理不清自己想表达什么,今早起来状态还不错,洗漱的时候思路居然通透了,哈哈哈~ 一.基本情况 先简单介绍笔者的一些基本情 ...
- spring boot 使用GraphQL
在此之前需要简单了解GraphQL的基本知识,可通过以下来源进行学习 GraphQL官方中文网站 :https://graphql.cn GraphQL-java 官网:https://www.gra ...
- js --策略模式
策略模式的定义: 将算法一个个的单独进行封装,并且使他们可以相互替换.此模式让算法的变化不会影响到使用算法的客户. 先回顾一下,我们在做项目的过程中,是不是经常会遇见因为业务逻辑的关系,我们会写好多的 ...
- 7.nth-of-type | nth-child?【CSS】
举例说明: <ul> <p>111</p> <span>222</span> <li>1</li> <li& ...
- MFC_选择目录对话框_选择文件对话框_指定目录遍历文件
选择目录对话框 void C资源共享吧视频广告清理工具Dlg::OnBnClickedCls() { // 清空编辑框内容 m_Edit.SetWindowTextW(L""); ...
- js求对象数组的交集/并集/差集/去重
1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] ...
- Python——元组
是为了满足,某些值当被定义以后就不可修改或删除而出现的元组形式. 特点: 元组中的元素不可被修改或删除 没有独立的功能 可以进行嵌套,当嵌套方可以修改删除时,可以对嵌套方进行. 元组可以进行公共功能中 ...
- 初识面向对象(钻石继承,super,多态,封装,method,property,classmethod,staticmethod)
组合 什么有什么的关系 一个类的对象作为另一个类的对象继承 子类可以使用父类中的名字(静态属性 方法)抽象类和接口类 只能不继承,不能被实例化 子类必须实现父类中的同名方法———规范代码 metacl ...
- mac 下enable mysql的load data in file
1)使用root用户登录mysql 2)将 local_infile 变量设置为true SET GLOBAL local_infile = true; 3)重启数据库 在系统偏好设置中找到MySql ...