您的位置:首页 > 编程语言 > C语言/C++

C++反汇编一(if语句)

2012-02-10 01:32 821 查看
1:    // Dasm.cpp : Defines the entry point for the console application.
2:    //
3:
4:    #include "stdafx.h"
5:    #include <IOSTREAM>
6:    using namespace std;
7:
8:    int main(int argc, char* argv[])
9:    {
00401380   push        ebp										// 保存当前ebp,以后ebp来寻址
00401381   mov         ebp,esp										// 调整ebp指向当前栈顶
00401383   sub         esp,44h										// 为局部变量分配内存0x44个字节
00401386   push        ebx
00401387   push        esi
00401388   push        edi
00401389   lea         edi,[ebp-44h]									// 局部变量首地址放入edi
0040138C   mov         ecx,11h											// 循环次数放ecx
00401391   mov         eax,0CCCCCCCCh									// 初始化内容放eax
00401396   rep stos    dword ptr [edi]									// 重复拷贝eax中的内容到edi指向的地址处.每次拷贝4个字节,共执行ecx=0x11次,edi+=4
10:       int x=3;
00401398   mov         dword ptr [ebp-4],3								//给局部变量ebp-4赋值3
11:       if (x==5)
0040139F   cmp         dword ptr [ebp-4],5								// 局部变量与5比较
004013A3   jne         main+43h (004013c3)								// 不相等则跳至004013c3处,也就是大括号结束的下一条语句处,相等则继续向下执行
12:       {
13:           cout<<"相等"<<endl;
004013A5   push        offset @ILT+35(std::endl) (00401028)				// 004013BE处的操作符<<的参数2 endl入栈
004013AA   push        offset string "\xcf\xe0\xb5\xc8" (0043201c)		// 004013B4处的操作符<<的参数2 字符串"相等"入栈
004013AF   push        offset std::cout (00439528)						// 004013B4处的操作符<<的参数1 std::cout入栈
004013B4   call        @ILT+155(std::operator<<) (004010a0)				// 调用<<的重载方法,原型是operator<<("相等",std::endl)
004013B9   add         esp,8											// 由于<<可接受不定参数,所以应该是__cdecl调用方式,由调用者平衡堆栈
004013BC   mov         ecx,eax											// 返回的cout流对象放入ecx,也就是this指针
004013BE   call        @ILT+110(std::basic_ostream<char,std::char_traits<char> >::operator<<) (00401073)	// 调用基类<<方法,原型是operator<<(std::cout,std::endl)
14:       }
15:       return 0;
004013C3   xor         eax,eax											// eax清零,用于返回
16:   }
004013C5   pop         edi
004013C6   pop         esi
004013C7   pop         ebx
004013C8   add         esp,44h											// 释放局部变量
004013CB   cmp         ebp,esp											// Debug下的堆栈平衡检查
004013CD   call        __chkesp (00409000)								// 堆栈不平衡的话会assert
004013D2   mov         esp,ebp
004013D4   pop         ebp												// 恢复原始ebp
004013D5   ret															// 返回给调用者

由此可知,汇编语言的条件跳转指令和高级语言的跳转指令正好相反,造成此类现象的原因是高级语言的if是满足则执行,而汇编语言是满足条件则跳转.
问题是004013B4处的operator<<是调用者平衡堆栈,而004013BE处的operator<<是被调者平衡堆栈,这里还得看c++的底层实现了...下次再研究.




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息