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 ...
随机推荐
- 有关java中的try{}catch(){}的讲解
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_38225558/article/d ...
- nginx配置比较杂乱的总结
常用变量 demo uri www.example.com/mock/interface?param1=203¶m2=test $args uri中的参数值 ?后面的部分 param1 ...
- 记一次在 Get 请求参数为 Null 值的折腾
先说主要原因,是因为一个 NgZerro 的 Select 组件,需要显示 placeHolder 文字,初始值为 null,然后直接绑定到查询参数中,传输到后端导致 BadRequest,参数解析失 ...
- 深入剖析Linux IO原理和几种零拷贝机制的实现
深入剖析Linux IO原理和几种零拷贝机制的实现 来源 https://zhuanlan.zhihu.com/p/83398714 零壹技术栈 公众号[零壹技术栈] 前言 零拷贝(Zero ...
- isolate sqflite demo
main.dart import 'package:flutter/material.dart'; import 'demo_isolates.dart'; import 'package:rxdar ...
- C# Attribute 名称和使用的问题
如果定义Attribute时, 名字是以Attribute结尾的, 在使用的时候, 就可以省略Attribute, 直接写前面的名字, 但是这样真的好吗? 自以为帮程序员省了一个单词, 然而 真理不再 ...
- 社交类app开发( 仿陌陌 客户端+服务器端)
一.开发所需要的技术 手机端需要Android/iOS开发人员,服务器端需要php/数据库开发人员, 如果再做网页版的话,WEB开发也是要的. 即时通讯 GPS地图 群聊 差不多 对 http so ...
- TypeScript_基础数据类型
TypeScript 的基础数据类型包含: string.number.boolean.array .object.null.undefined.enmu.void.never.any.tuple 注 ...
- net 与或非
&& op1 && op2 当op1和op2都是true时,返回true :如果op1的值是false,则不运算右边的操作数 || op1 || op2 当op1和op ...
- React系列,初识
学习react对于新手来说,还没有学react往往就会被webpack,npm等搞的晕头转向,所以我们今天就从最简单的方式入手 <script src="react.js"& ...