简体   繁体   English

预期在`__asm`语句中的表达式

[英]Expected an expression in `__asm` statement

I'm using code from this forum topic in order to obtain the CPU's family information: 我正在使用此论坛主题中的代码来获取CPU的系列信息:

#include <stdio.h>

struct cpuid_type {
    unsigned int eax;
    unsigned int ebx;
    unsigned int ecx;
    unsigned int edx;
};
typedef struct cpuid_type cpuid_t;

cpuid_t cpuid(unsigned int number) 
{
    cpuid_t result; 

    __asm("movl %4, %%eax; cpuid; movl %%eax, %0; movl %%ebx, %1; movl %%ecx, %2; movl %%edx, %3;"
        : "=m" (result.eax),
          "=m" (result.ebx),
          "=m" (result.ecx),
          "=m" (result.edx)               /* output */
        : "r"  (number)                   /* input */
        : "eax", "ebx", "ecx", "edx"      /* no changed registers except output registers */
        );

    return result;
}    

int main (int argc, const char * argv[]) 
{
    cpuid_t cpuid_registers;
    unsigned int cpu_family, cpu_model, cpu_stepping;

    cpuid_registers = cpuid(1);

    cpu_family   = 0xf & (cpuid_registers.eax>>8);
    cpu_model    = 0xf & (cpuid_registers.eax>>4);
    cpu_stepping = 0xf & cpuid_registers.eax;

    printf("CPUID (1): CPU is a %u86, Model %u, Stepping %u\n",
           cpu_family, cpu_model, cpu_stepping);


    return 0;
}

However, Visual Studio 2013 is giving me an 'InteliSense: expected an expression' error for this line: 但是,Visual Studio 2013在此行给我一个“ InteliSense:期望一个表达式”错误:

asm("movl %4, %%eax; cpuid; movl %%eax, %0; movl %%ebx, %1; movl %%ecx, %2; movl %%edx, %3;"
        : "=m" (result.eax), // <-- Error Here
          "=m" (result.ebx),
          "=m" (result.ecx),
          "=m" (result.edx)               /* output */
        : "r"  (number)                   /* input */
        : "eax", "ebx", "ecx", "edx"      /* no changed registers except output registers */
        );

As Visual Studio 2013 told me that error C2290: C++ 'asm' syntax ignored. Use __asm. 正如Visual Studio 2013告诉我的那样, error C2290: C++ 'asm' syntax ignored. Use __asm. error C2290: C++ 'asm' syntax ignored. Use __asm. , I changed asm to __asm . ,我将asm更改为__asm

Every error I have is related to the above block of code: 我遇到的每个错误都与上面的代码块有关:

5   IntelliSense: expected a ')'
Error   2   error C2290: C++ 'asm' syntax ignored. Use __asm.   
Error   1   error C2143: syntax error : missing ')' before ':'
Error   3   error C2059: syntax error : ')'

As I'm literally using the code provided from the thread mentioned above without any alterations (apart from the __asm edit), I'm assuming that I'm not including a required library or header that doesn't need to be included in earlier versions of Visual Studio. 因为我实际上使用了上述线程提供的代码,没有进行任何更改(除了__asm编辑),所以我假设我不包括不需要的库或标头,而该库或标头不需要包含在前面版本的Visual Studio。

If so, what headers/libraries am I missing? 如果是这样,我缺少哪些标题/库? If not, what am I doing wrong? 如果没有,我在做什么错?

Your example code is using GCC-style inline assembly syntax which isn't supported by the Microsoft compiler. 您的示例代码使用的是Microsoft编译器不支持的GCC风格的内联汇编语法。 While Microsoft has it's own inline assembly syntax, you should avoid using it wherever possible. 尽管Microsoft具有自己的内联汇编语法,但应尽可能避免使用它。 It's only supported with the 32-bit x86 compiler, it's not supported with the 64-bit compiler or compilers targetting AMD or other CPU architectures. 仅32位x86编译器支持它,而64位编译器或以AMD或其他CPU架构为目标的编译器则不支持。 Also unlike GCC's inline assembly syntax, Microsoft's syntax is subject a number of undocumented rules, and even if written "correctly" can be very fragile. 与GCC的内联汇编语法不同,Microsoft的语法受许多未记录规则的约束,即使“正确”编写也可能非常脆弱。

In your case you should be using Microsoft's intrinsic function for the CPUID instruction. 在您的情况下,您应该对CPUID指令使用Microsoft的固有函数。 It will work with both 32-bit and 64-bit versions of the compiler and won't break because you changed optimization levels or upgraded your compiler. 它可以与32位和64位版本的编译器一起使用,并且不会因更改优化级别或升级编译器而中断。 The specific function you want to use is __cpuid . 您要使用的特定功能是__cpuid The linked documentation should make it clear how you can use it to replace the inline assembly statement in your cpuid function. 链接的文档应清楚说明如何使用它替换cpuid函数中的内联汇编语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM