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 ...
随机推荐
- git比较本地仓库和远程仓库的差异(转)
转自:https://www.jianshu.com/p/6078a49900a4
- 摘要 - Digest
首先从md5说起,一般新进入开发行业最先接触的就是md5了,md5本质上是一个hash(谐音:哈希)算法,可以从一个大文件信息中提取出一小段信息,叫提取摘要,有的地方也有提取指纹这种说法,其实指纹这个 ...
- 3)创建,测试,发布 第一个NET CORE程序
工具:Visual Studio Code 或者 Visual Studio 环境:.NET CORE 2.0 VS Code很强大 当然支持netcore的开发,但是我还是选择更熟悉更强大的VS. ...
- cxx11emu.h 和 logprint.h
cxx11emu.h 和 logprint.h /* Start of cxx11emu.h */ #ifndef STDBP_CXX11EMU_H_ #define STDBP_CXX11EMU_H ...
- SocketException: Write failed (OS Error: Broken pipe, errno = 32
https://github.com/flutter/flutter/issues/16491
- IOS - UDID IDFA IDFV MAC keychain
在开发过程中,我们经常会被要求获取每个设备的唯一标示,以便后台做相应的处理.我们来看看有哪些方法来获取设备的唯一标示,然后再分析下这些方法的利弊. 具体可以分为如下几种: UDID IDFA IDFV ...
- vue项目启动报错You may use special comments to disable some warnings.
在build/webpack.base.conf.js文件中,注释或者删除掉:...(config.dev.useEslint ? [createLintingRule()] : []),
- 1+x学习日志——js获取随机颜色的几种实现方式
因为学习时间比较紧,所以也没多少时间发博客了.后续会慢慢补齐的,下面是代码 /// function randomColor(){ var r = parseInt(Math.random() * 2 ...
- Bootstrap框架 简单使用
目录 Bootstrap框架 简单使用 什么是Bootstrap 下载 Bootstrap 项目结构 Bootstrap 简单使用 表格格式 Bootstrap 按钮颜色 尺寸 Bootstrap框架 ...
- day34-python之进程调用
1.信号量 import threading,time class myThread(threading.Thread): def run(self): if semaphore.acquire(): ...