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

c++类的反汇编代码的实现【DEBUG】

2013-07-04 23:02 239 查看
int  main( ){

01104810  push        ebp  

01104811  mov         ebp,esp  

01104813  sub         esp,0E8h  

01104819  push        ebx  

0110481A  push        esi  

0110481B  push        edi  

0110481C  lea         edi,[ebp-0E8h]  

01104822  mov         ecx,3Ah  

01104827  mov         eax,0CCCCCCCCh  

0110482C  rep stos    dword ptr es:[edi]  

0110482E  mov         eax,dword ptr ds:[0110F0E0h]  

01104833  xor         eax,ebp  

01104835  mov         dword ptr [ebp-4],eax  
fruit apple = fruit("apple",3.1f,100.00f);   //类调用构造函数进行初始化,apple 是类fruit 的一个实例,为main函数内的一个局部变量
01104838  push        ecx    ;ecx = 00000000 ,作用是提高栈顶位置用于存放构造函数的参数
01104839  movss       xmm0,dword ptr ds:[110CD50h]
  ; 将0000c842 复制到xmm0 寄存器的低32 位
01104841  movss       dword ptr [esp],xmm0  ;将xmm0 的低32 位存放到栈顶位置,参数3
01104846  push        ecx  ;作用如上,保存构造函数的参数
01104847  movss       xmm0,dword ptr ds:[110CD4Ch]  
  ;参数2
fruit apple = fruit("apple",3.1f,100.00f);
0110484F  movss       dword ptr [esp],xmm0  
01104854  push        110CC98h  ; 将apple 字符串的首地址压栈作为构造函数的参数  ,参数1
01104859  lea         ecx,[apple]  ;此处是一个特殊的地方,构造函数只有三个参数,此处使用ecx寄存器保存apple对象地址的作用是什么?
0110485C  call        fruit::fruit (011011B8h)   ;调用构造函数初始化apple 对象
apple.show_fruit();
01104861  lea         ecx,[apple]  
01104864  call        fruit::show_fruit (01101217h)  
return 0;

01104869  xor         eax,eax  
}

// 类定义部分的反汇编代码

#include<iostream>

#include<cstdio>

class fruit {

private:
char  name[20];
float price;
float weight;

public:
fruit(char  name_[] , float price_ , float weight_){

01102BE0  push        ebp   ;自动从栈中获取保存局部变量的地址空间并使用cccccccc 填充该部分空间

01102BE1  mov         ebp,esp  

01102BE3  sub         esp,0CCh  

01102BE9  push        ebx  

01102BEA  push        esi  

01102BEB  push        edi  

01102BEC  push        ecx  

01102BED  lea         edi,[ebp-0CCh]  

01102BF3  mov         ecx,33h  

01102BF8  mov         eax,0CCCCCCCCh  

01102BFD  rep stos    dword ptr es:[edi]  
01102BFF  pop         ecx  ;
01102C00  mov         dword ptr [this],ecx  ;由此处可以看出ecx中apple的地址是为了初始化this 指针。
strcpy_s(name,name_);

01102C03  mov         eax,dword ptr [name_]   ;将name_ ,name 入栈,然后调用strcpy_s 函数初始化apple 对象的name 成员

01102C06  push        eax  

01102C07  mov         ecx,dword ptr [this]  


01102C0A  push        ecx  

01102C0B  call        strcpy_s<20> (011014A6h)  

01102C10  add         esp,8   ; 保持栈平衡
price = price_;

01102C13  mov         eax,dword ptr [this]  
;初始化price 对象成员

01102C16  movss       xmm0,dword ptr [price_]  

01102C1B  movss       dword ptr [eax+14h],xmm0    ; eax + 14 h = eax + 20 (20 是name字符数组的大小) 是apple 对象中price 成员的地址 ;eax = this ,this 在C++ 中代表对象的地址
weight = weight_;

01102C20  mov         eax,dword ptr [this]  
;初始化weight 对象成员

01102C23  movss       xmm0,dword ptr [weight_]  

01102C28  movss       dword ptr [eax+18h],xmm0  ; eax + 18h 是对象中weight 的地址
}

01102C2D  mov         eax,dword ptr [this]  

01102C30  pop         edi  

01102C31  pop         esi  

01102C32  pop         ebx  

01102C33  add         esp,0CCh  

01102C39  cmp         ebp,esp  

01102C3B  call        __RTC_CheckEsp (01101339h)  

01102C40  mov         esp,ebp  

01102C42  pop         ebp  

01102C43  ret         0Ch  

思考:this 指针存放的位置?

// source code

#include<iostream>
#include<cstdio>
class fruit {
private:
char  name[20];
float price;
float weight;
public:
fruit(char  name_[] , float price_ , float weight_){
strcpy_s(name,name_);
price = price_;
weight = weight_;
}
void set_name(char m_name[]){
strcpy_s(name,m_name);
}
void set_price(float m_price){
price = m_price;
}
void set_weight(float m_weight){
weight = m_weight;
}
void show_fruit(){
std::cout << "name: ";
std::cout << name << std::endl;
std::cout << "price: " << price << std::endl;
std::cout << "weight: "<< weight << std::endl;
}
};
int  main(){
fruit apple = fruit("apple",3.1f,100.00f);
apple.show_fruit();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  汇编语言 C++