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



我正在尝试使用cl.exe(从Visual Studio)编译这个c源文件。无法编译的特定代码是:

#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'

您可以使用编译器内部函数代替直接汇编。微软编译器支持__cpuid()文档在这里:https://msdn.microsoft.com/en-us/library/hskdteyh.aspx

GCC通过cpuid.h来支持__get_cpuid(),答案如下(https://stackoverflow.com/a/14266932/1401351)。

最新更新