我正在使用此论坛主题中的代码来获取 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;
}
但是,Visual Studio 2013 给了我一个 'InteliSense:expected an expression' 错误:
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 */
);
正如 Visual Studio 2013 告诉我的那样error C2290: C++ 'asm' syntax ignored. Use __asm.
,我asm
改为__asm
.
我遇到的每个错误都与上面的代码块有关:
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 : ')'
由于我实际上是在使用上面提到的线程提供的代码而没有任何更改(除了__asm
编辑),我假设我不包括不需要包含在早期版本中的必需库或头文件的 Visual Studio。
如果是这样,我错过了哪些标题/库?如果没有,我做错了什么?