zoukankan      html  css  js  c++  java
  • 一篇旧文章,结合汇编探索this指针

    //VC6.0下成功编译
    #include <iostream.h>
    
    class X{
    public:
        void foo(int b,int c){
            this->a=b*c;
            cout<<a<<endl;
        }
        int a;
    };
    
    int main(){
        void (X::*pXfoo)(int,int);
        void (__stdcall*pfoo)(int,int);
        //协调调用约定 让被调函数进行栈的清理
        
        pXfoo = X::foo;
        __asm{
            push eax
            mov eax,dword ptr pXfoo
            mov dword ptr pfoo,eax
            pop  eax
            //pfoo = pXfoo
        }
        X x;
    
        //使用对象地址给this指针赋值
        __asm push ecx
        __asm lea ecx,x
        pfoo(3,4);
        __asm pop ecx
        //另一种类似__thiscall调用约定的是__fastcall
        //但是使用__fastcall,则所有参数将被放入寄存器,则成员函数产生异常
    }
    
    /*
        C++的成员函数就是个普通函数与C
        的函数的不同之处也就是调用约定的不同
    
        VC的__thiscall的特征:
        1)对象地址通过寄存器传递
        2)其他参数通过栈传递
        3)进栈方式 右 -> 左
        4)被调函数进行栈清理
    */
    //BCB6.0 下成功编译
    #include <iostream.h>
    
    class X{
    public:
        void foo(int b,int c){
                a = b*c;
                cout<<"a="<<a<<endl;
        }
        int a;
    };
    
    int main(){
        X x; //04
        void (X::*pXfoo)(int,int);//08
        void (*pfoo)(X* THIS,int,int);//0c~14
    
        pXfoo=X::foo;
       __asm{
            push eax
            mov eax,dword ptr pXfoo
            mov dword ptr pfoo,eax
            pop  eax
            //pfoo = pXfoo
        }
    
        pfoo(&x,3,4);
        getchar();
        //BCB __thiscall the same to __cdecl
    } 
  • 相关阅读:
    报错处理
    MySQL8.0跟5.7分组查询表所有字段
    模拟开始时间、结束时间生成历史时间生成曲线模拟数据
    查询电脑登录过的WiFI账号密码
    Samba服务器架设
    CentOS安装GitLab
    申请域名并使用DDNS
    极路由4增强版(极企版)-刷潘多拉固件
    Git命令
    elasticsearch7.6.2 -canal1.1.4集成
  • 原文地址:https://www.cnblogs.com/code-style/p/3524474.html
Copyright © 2011-2022 走看看