VC++ 汇编相关的东西

Tips: VC++在新建一个.asm文件后必须重新导入project中才能进行编译。

下面是一个汇编与C++相互调用的例子:

Main.cpp

#include <stdio.h>
#include <Windows.h>
#include <string.h>

class CTest
{
public:
    void Init()
    {
        m_nSize = 100;
        m_pContent = new char[m_nSize];
        strcpy(m_pContent,"hello world!");
    }
    void Show()
    {
        printf("%s
",m_pContent);
    }
    void Destory()
    {
        if (m_pContent)
        {
            delete[] m_pContent;
            m_pContent = NULL;
        }
        m_nSize = 0;
    }
private:
    int m_nSize;
    char* m_pContent;
};

extern "C" void __stdcall InitTest(DWORD pThis,DWORD pFunc);
extern "C" int __stdcall bswap_func(int InputVar);

int main(int agrc,char* argv[])
{
    CTest* pTest = new CTest();
#ifdef TEST_ASM_INFILE
    _asm
    {
        mov eax,pTest
        call CTest::Init
    }
#else
    void (__thiscall CTest::* pFunc)(void) = &CTest::Init;
    InitTest((DWORD)pTest,*(DWORD*)&pFunc);
#endif
    pTest->Show();
    pTest->Destory();
    delete pTest;

    int nTest = 0x12345678;
    int nResult = bswap_func(nTest);
    printf("Original number:0x%x reverse number:0x%x
",nTest,nResult);
    return 0;
}

Func.asm

.686
.model flat, stdcall

.code
InitTest proc pThis:DWORD,pFunc:DWORD
    mov ecx,pThis
    call pFunc
    ret
InitTest endp

bswap_func proc InVar:DWORD
    mov        eax,    InVar
    bswap    eax
    ret
bswap_func endp

end
原文地址:https://www.cnblogs.com/xylc/p/3540438.html