gpt4 book ai didi

c - 获取64位CPUID示例代码在VS2008中编译

转载 作者:太空狗 更新时间:2023-10-29 15:33:55 26 4
gpt4 key购买 nike

我正在尝试获取一些在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com