程式語言 - MinGW - Inline Assembly



main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
 
int _main(int argc, char**argv)
{
    uint32_t a=0x11223344, b=0x55667788;

    printf("a:0x%08x, b:0x%08x\n", a, b);

    __asm__(
        ".intel_syntax noprefix\n"
        "mov ecx, %[in_eax]\n"
        "mov edx, %[in_ebx]\n"
        "mov %[out_eax], edx\n"
        "mov %[out_ebx], ecx\n"
        ".att_syntax\n"
        : [out_eax]"=r"(a), [out_ebx]"=r"(b)
        : [in_eax]"r"(a), [in_ebx]"r"(b)
        : "ecx", "edx"
    );

    printf("a:0x%08x, b:0x%08x\n", a, b);
    return 0;
}

編譯、執行

$ i686-w64-mingw32-gcc main.c -o main.exe -lkernel32 -lmsvcrt -nostdlib
$ wine main.exe
    a:0x11223344, b:0x55667788
    a:0x55667788, b:0x11223344