【问题标题】:Compile c source with 'asm' directive [duplicate]用'asm'指令编译c源[重复]
【发布时间】:2017-01-05 20:53:01
【问题描述】:

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

【问题讨论】:

  • 看起来像 gcc 扩展的内联汇编。我不认为VS可以理解它。使用不同的编译器。
  • 你试过__asm而不是asm吗?
  • 这似乎是 GCC 的扩展内联汇编器,它与 MSVC 不兼容。 MSVC 现在有编译器内在 __cpuid 记录在这里:msdn.microsoft.com/en-us/library/hskdteyh.aspx。我会将您的 cpuid 函数转换为使用内部而不是内联汇编程序。
  • 典型的 MSVC 语法是__asm { .... }
  • 你的内联汇编是 GCC 兼容的。 MS不同。您必须转换语法和约束。

标签: c visual-studio visual-c++ inline-assembly


【解决方案1】:

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

GCC 改为通过 cpuid.h 支持 __get_cpuid(),如此答案 (https://stackoverflow.com/a/14266932/1401351) 所述。

【讨论】:

    猜你喜欢
    • 2018-08-09
    • 2011-10-22
    • 2013-10-09
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2018-08-14
    相关资源
    最近更新 更多