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

时间:2021-09-27 03:15:30

I'm trying to compile this c source file using cl.exe (from Visual Studio). The specific code in question that fails to compile is:

我正在尝试使用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));
}

I'm seeing this failure:

我看到了这个失败:

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 个解决方案

#1


2  

Instead of direct assembly, you can use compiler intrinsics. Microsoft compilers support __cpuid() documented here: https://msdn.microsoft.com/en-us/library/hskdteyh.aspx

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

GCC instead supports __get_cpuid() via cpuid.h as described by this answer (https://*.com/a/14266932/1401351).

相反,GCC通过cpuid.h支持__get_cpuid(),如本答案所述(https://*.com/a/14266932/1401351)。

#1


2  

Instead of direct assembly, you can use compiler intrinsics. Microsoft compilers support __cpuid() documented here: https://msdn.microsoft.com/en-us/library/hskdteyh.aspx

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

GCC instead supports __get_cpuid() via cpuid.h as described by this answer (https://*.com/a/14266932/1401351).

相反,GCC通过cpuid.h支持__get_cpuid(),如本答案所述(https://*.com/a/14266932/1401351)。