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\n",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\n",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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。