虚函数调用 asm

逆向工程 反汇编者
2021-07-10 11:12:31

这似乎是一个虚函数调用。这段代码在像 C++ 这样的高级语言中看起来如何?

.text:0053A745 loc_53A745:                             ; CODE XREF: sub_53A690+CEj
.text:0053A745                 mov     ecx, [edi+esi*4]
.text:0053A748                 test    ecx, ecx
.text:0053A74A                 jz      short loc_53A751
.text:0053A74C                 mov     edx, [ecx]
.text:0053A74E                 call    dword ptr [edx+4]
.text:0053A751
.text:0053A751 loc_53A751:                             ; CODE XREF: sub_53A690+BAj
.text:0053A751                 mov     eax, dword_83C020
.text:0053A756                 test    eax, eax
.text:0053A758                 jnz     short loc_53A78D
.text:0053A75A                 inc     esi
.text:0053A75B                 cmp     esi, 25h
.text:0053A75E                 jl      short loc_53A745

1个回答

这可能是一个包含虚函数的 37 个对象的循环,简化版本可能看起来与此类似(用 C++ 编写)

#include <iostream>

class Animal
{
public:
    virtual void makeSound() = 0;
    virtual char* getColor() = 0;
    virtual ~Animal() = 0;
};

class Cat : public Animal
{
public:
    Cat();
    void makeSound() { std::cout << "meow" << std::endl; }
    char* getColor() { return "red"; };
    ~Cat() {};
};

Animal* animals[] = {new Cat(), new Cat(), new Cat()};
int main()
{
    for (int i = 0; i < 3; i++)
        if (animals[i])
            std::cout << animals[i]->getColor();

    for (int i = 0; i < 3; i++)
        delete animals[i];
    return 0;
}

核心线路:

if (animals[i])
  std::cout << animals[i]->getColor();

编译为clang 8.0.0

cmp     dword ptr [animals + eax*4], 0
je      .LBB2_4
mov     eax, dword ptr [ebp - 8]
mov     eax, dword ptr [animals + eax*4]
mov     ecx, dword ptr [eax]
mov     dword ptr [esp], eax
call    dword ptr [ecx + 4]

此外,您的代码似乎遵循 Microsoft__thiscall调用约定,因为this指针存储在ecx寄存器中。