我尝试使用 C++ 中的汇编程序读取 CPUID。我知道它在 中有功能,但我想要 asm 方式。因此,CPUID 执行后,应该用ASCII 编码的字符串填充eax、ebx、ecx 寄存器。但我的问题是,因为我只能在 asm 中寻址完整的或半个 eax 寄存器,如何将 32 位分成 4 个字节。我用过这个:
#include <iostream>
#include <stdlib.h>
int main()
{
_asm
{
cpuid
/*There I need to mov values from eax,ebx and ecx to some propriate variables*/
}
system("PAUSE");
return(0);
}
最佳答案
Linux 内核源代码 shows how使用内联汇编执行 x86 cpuid。语法是特定于 GCC 的;如果您使用的是 Windows,这可能没有帮助。
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));
}
一旦您拥有这种格式的函数(请注意,EAX、ECX 是输入,而所有四个都是输出),您可以轻松地分解调用方中的各个位/字节。
关于C++ 字到字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2420554/