AT&T将在Visual Studio 2017中内联asm

时间:2018-08-20 14:47:28

标签: visual-studio assembly inline-assembly att dll-injection

我试图将用AT&T语法编写的程序集从DevC ++项目转换为Visual Studio中的内联程序集。

这是我要转换的AT&T:

void Painter::drawRectangle(int surface, int x, int y, int width, int height, int red, int green, int blue) {
    asm("mov %0, %%eax":: "r" (0x004EAA90));
    asm("call *%eax");
    asm("mov %eax, %ecx");
    asm("mov (%ecx), %eax");
    asm("push %0":: "m" (blue));
    asm("push %0":: "m" (green));
    asm("push %0":: "m" (red));
    asm("push %0":: "m" (height));
    asm("push %0":: "m" (width));
    asm("push %0":: "m" (y));
    asm("push %0":: "m" (x));
    asm("push %0":: "m" (surface));
    asm("call *0x14(%eax)");
}

到目前为止我做了什么:

void _drawrectangle(int surface, int x, int y, int width, int height, int red, int green, int blue)
{    
    __asm
    {
        mov eax, 0x004eaa90
        call dword ptr [eax]
        mov ecx, eax
        mov eax, [ecx]
        push blue
        push green
        push red
        push height
        push width
        push y
        push x
        push surface
        call dword ptr [eax + 0x14]
    }
}

我正在我已经注入游戏的DLL中编写此代码。游戏在打开时崩溃。而且我已经在C ++中钩住了另一个绘图函数,该函数起作用了。

希望您能帮助我/指导我正确的方向。谢谢。

1 个答案:

答案 0 :(得分:4)

这是不使用内联汇编就可以用C ++编写函数的方法:

#ifndef _MSC_VER
/* For GCC and clang */
#undef __thiscall
#define __thiscall __attribute__((thiscall))
#endif

struct some_interface {
    virtual void _unknown_0() = 0;
    virtual void _unknown_4() = 0;
    virtual void _unknown_8() = 0;
    virtual void _unknown_C() = 0;
    virtual void _unknown_10() = 0;
    virtual void __thiscall drawRectangle(int surface, int x, int y,
                          int width, int height,
                          int red, int green, int blue) = 0;
};

const auto get_interface = (some_interface *(*)()) 0x4EAA90;

void 
drawRectangle(int surface, int x, int y, int width, int height,
          int red, int green, int blue) {
    get_interface()->drawRectangle(surface, x, y, width, height,
                       red, green, blue);
}

您首先尝试翻译的代码将调用一个函数,该函数返回一个指向至少定义了6个虚拟方法的类对象的指针。然后,它将调用该对象的第6个虚拟方法。 some_interface结构最少地重新创建该类,以便可以调用第6个虚拟方法。 get_interface常量是指向位于0x4EAA90处的函数的函数指针,在C ++中,函数指针可以像函数一样使用。

上面的代码generates the following assembly in GCC 8.2

drawRectangle(int, int, int, int, int, int, int, int):
        subl    $12, %esp
        movl    $5155472, %eax
        call    *%eax
        movl    (%eax), %edx
        movl    %eax, %ecx
        pushl   44(%esp)
        pushl   44(%esp)
        pushl   44(%esp)
        pushl   44(%esp)
        pushl   44(%esp)
        pushl   44(%esp)
        pushl   44(%esp)
        pushl   44(%esp)
        call    *20(%edx)
        addl    $12, %esp
        ret

following assembly with Visual C++ 2017

void drawRectangle(int,int,int,int,int,int,int,int) PROC                  ; drawRectangle, COMDAT
        mov     eax, 5155472                          ; 004eaa90H
        call    eax
        push    DWORD PTR _blue$[esp-4]
        mov     ecx, eax
        push    DWORD PTR _green$[esp]
        mov     edx, DWORD PTR [eax]
        push    DWORD PTR _red$[esp+4]
        push    DWORD PTR _height$[esp+8]
        push    DWORD PTR _width$[esp+12]
        push    DWORD PTR _y$[esp+16]
        push    DWORD PTR _x$[esp+20]
        push    DWORD PTR _surface$[esp+24]
        call    DWORD PTR [edx+20]
        ret     0
void drawRectangle(int,int,int,int,int,int,int,int) ENDP                  ; drawRectangle