c++ - VS2012 支持 __asm__ __volatile__ 汇编代码

标签 c++ visual-studio-2012 assembly

我使用的是 VS2012 C++ Windows 7,我需要获取有关 CPU 多线程的信息以计算可用逻辑处理器的数量。

我正在使用此代码(来自 This SO Post)

typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;

uint32_t registers[4];
__asm__ __volatile__ ("cpuid " :
                      "=a" (registers[0]),
                      "=b" (registers[1]),
                      "=c" (registers[2]),
                      "=d" (registers[3])
                      : "a" (1), "c" (0));

unsigned CPUFeatureSet = registers[3];
bool hyperthreading = CPUFeatureSet & (1 << 28);

此程序集无法编译,出现以下错误:

error C2065: '__asm__' : undeclared identifier

我尝试更改为 __asm __volatile 并将所有内容放在一行中:

__asm __volatile ("cpuid " :   "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3])  : "a" (1), "c" (0));

这也不起作用,导致:

error C2400: inline assembler syntax error in 'opcode'; found '('

感谢帮助解决这个问题。

最佳答案

如果你坚持使用cpuid,你应该使用__cpuid()内部函数。 msdn page甚至附带示例代码。像这样:

#include <intrin.h>

void foo()
{
    uint32_t registers[4];
    __cpuid(registers, 1);
    unsigned CPUFeatureSet = registers[3];
    // ...
}

关于c++ - VS2012 支持 __asm__ __volatile__ 汇编代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35874661/

相关文章:

visual-studio-2012 - 是否可以使用比其连接的服务器更高版本的 TFS Build?

c++ - 如何在本地生成像干净组装一样的godbolt?

linux - "Hello World"函数不使用 C printf

c++ - 如何优化strtok + atoll

opencv - cvQueryFrame 产生 msvcrt 访问错误

vs2012 中的 c++/cli Windows 窗体,对象数组不可能

gcc - Linux 的 Windows 子系统中的 GNU 汇编器失败

C++ IOCP 服务器容器信息

c++ - 如何将 const 应用于 C++ 中参数列表之外的模板参数类型?

c++ - 为什么枚举会隐式转换为 std::string?