使用 'asm' 指令编译 c 源代码

Compile c source with 'asm' directive

我正在尝试使用 cl.exe(来自 Visual Studio)编译 this c source file。编译失败的具体问题代码是:

#include <stdio.h>

static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
                                unsigned int *ecx, unsigned int *edx)
{
        /* ecx is often an input as well as an output. */
        asm volatile("cpuid"
            : "=a" (*eax),
              "=b" (*ebx),
              "=c" (*ecx),
              "=d" (*edx)
            : "0" (*eax), "2" (*ecx));
}

我看到这个失败:

C:\>cl sgx.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24213.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

sgx.c
sgx.c(7): error C2065: 'asm': undeclared identifier
sgx.c(7): error C2143: syntax error: missing ';' before 'volatile'

您可以使用编译器内部函数来代替直接汇编。 Microsoft 编译器支持 __cpuid(),记录在此处:https://msdn.microsoft.com/en-us/library/hskdteyh.aspx

GCC 改为通过 cpuid.h 支持 __get_cpuid(),如此答案 () 所述。