Visual C++中使用内联纯汇编及其它

以下代码通过Visual Studio 2013的C语言编译器的编译与连接,并且最终能正常执行。

关于有些注意的地方代码里注释已经写了。

#include <stdio.h>

#define MY_TRACE(format, ...)   printf(format, ##__VA_ARGS__)

static int __declspec(naked) __fastcall MyAdd(int a, int b)
{
    __asm
    {
        mov     eax, ecx
        add     eax, edx
        ret
    }
}

static int __declspec(naked) __fastcall ASMTest(int a, int b)
{
    __asm
    {
        // The following registers should be preserved
            push    esi
            push    edi
            push    ebx
            push    ebp

            mov     eax, ecx
            sub     eax, edx
            mov     esi, 1
            mov     edi, 2
            mov     ebx, 3
            mov     ebp, 4

            pop     ebp
            pop     ebx
            pop     edi
            pop     esi

            ret
    }
}

int main(void)
{
    MY_TRACE("Hello, world!\n");

    int i = MyAdd(10, -1);
    MY_TRACE("My addition result is: %d\n", i);

    int a = ASMTest(10, 20);
    printf("The value is: %d\n", a);
}

 

 

 

 

posted @ 2013-06-11 13:10  zenny_chen  Views(442)  Comments(0Edit  收藏  举报