使用&as;' asm'编译c源代码。指示

时间:2016-08-29 18:27:46

标签: c visual-studio inline-assembly cl.exe

我尝试使用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'

1 个答案:

答案 0 :(得分:2)

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

GCC代之以__get_cpuid()支持cpuid.h,如此答案(https://stackoverflow.com/a/14266932/1401351)所述。