我正在尝试获取一些我发现在 Visual Studio 2008 中运行的 c & ASM 示例代码。我认为问题不是 VS 2005-2008 之间的差异。 This example 应该在 64 位系统上获取 CPUID。 (我尝试编译仅 ASM 的 32 位示例也失败了)
我可以将此代码复制并粘贴到新项目中,但我无法构建它。我尝试了几个不同的 VS 项目模板,但没有成功。我相信我一直按照说明进行操作。有人可以提供一步一步的使用项目模板和项目设置在 Visual Studio 2008 中运行它吗?
我注意到的一件事是,虽然我可以将环境设为 64 位,但我似乎无法为这个项目定位 x64 - 添加新平台的唯一选择是移动平台。我不得不在命令行选项中手动定义 _M_X64,我怀疑我不应该这样做。
不直接调试,仅供引用 - 据我所知,错误如下:
1> Assembling: .\cpuid64.asm
1>.\cpuid64.asm(4) : error A2013:.MODEL must precede this directive
1>.\cpuid64.asm(5) : error A2034:must be in segment block
1>.\cpuid64.asm(7) : error A2034:must be in segment block : cpuid64
1>.\cpuid64.asm(11) : error A2034:must be in segment block
1>.\cpuid64.asm(12) : error A2008:syntax error : .
1>.\cpuid64.asm(13) : error A2034:must be in segment block
1>.\cpuid64.asm(14) : error A2008:syntax error : .
1>.\cpuid64.asm(15) : error A2008:syntax error : .
1>.\cpuid64.asm(17) : error A2034:must be in segment block
1>.\cpuid64.asm(18) : error A2085:instruction or register not accepted in current CPU mode
1>.\cpuid64.asm(19) : error A2085:instruction or register not accepted in current CPU mode
1>.\cpuid64.asm(20) : error A2085:instruction or register not accepted in current CPU mode
1>.\cpuid64.asm(21) : error A2085:instruction or register not accepted in current CPU mode
1>.\cpuid64.asm(22) : error A2085:instruction or register not accepted in current CPU mode
1>.\cpuid64.asm(23) : error A2085:instruction or register not accepted in current CPU mode
1>.\cpuid64.asm(24) : error A2085:instruction or register not accepted in current CPU mode
1>.\cpuid64.asm(26) : error A2034:must be in segment block
1>.\cpuid64.asm(27) : error A2034:must be in segment block
1>.\cpuid64.asm(29) : error A2034:must be in segment block
1>.\cpuid64.asm(30) : error A2034:must be in segment block
1>.\cpuid64.asm(31) : fatal error A1010:unmatched block nesting : cpuid64
最佳答案
好吧,如果您不能定位 x64
,那么您会遇到一个小问题,因为您没有使用 x64
工具链。我强烈建议您使用 VS 安装向导添加 x64
工具。您应该能够转到新平台,将其命名为 x64
并基于 win32
。
话虽如此,我实际上建议使用 YASM,因为它允许您与 VS 集成,而 YASM 是迄今为止比 Microsoft 更好的汇编程序。
由于我碰巧有一个无论如何都会从中受益的项目,我想我会使用 yasm 来做到这一点:
gcpuid.asm :
; CPUID on x64-Win32
; Ref:
section .code
global gcpuid
; void cpuid(uint32_t* cpuinfo, char* vendorstr);
gcpuid:
push rbp
mov rbp, rsp
mov r8, rcx ; capture the first and second arguments into 1-> r8, 2-> r9
mov r9, rdx ; cause cpuid will obliterate the lower half of those regs
; retrive the vendor string in its
; three parts
mov eax, 0
cpuid
mov [r9+0], ebx
mov [r9+4], edx
mov [r9+8], ecx
; retrieve the cpu bit string that tells us
; all sorts of juicy things
mov eax, 1
cpuid
mov [r8], eax
mov eax, 0
leave
ret
cpuid-w32.c:
#include <stdio.h>
#include <stdint.h>
void gcpuid(uint32_t* cpuinfo, char* vendorstr);
int main(int argc, char** argv)
{
char vendor[13] = { 0 };
uint32_t flags;
gcpuid(&flags, vendor);
printf("Flags: %u, Vendor: %s\n", flags, vendor);
return 0;
}
在链接下载页面上提供的 vs2010 存档中的 readme.txt 之后,让 vs 组装它是一件轻而易举的事,我认为这个解决方案比英特尔更清晰。
至于你用cpuinfo参数做什么,嗯,你可以尝试各种掩码来找出有关cpu类型的各种信息。如果我完成实现,我可能会更新。
关于c - 获取 64 位 CPUID 示例代码在 VS2008 中编译,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3208083/