【问题标题】:Getting 64-bit CPUID sample code to compile in VS2008获取 64 位 CPUID 示例代码在 VS2008 中编译
【发布时间】:2011-03-13 14:13:51
【问题描述】:

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

【问题讨论】:

标签: c visual-studio-2008 assembly


【解决方案1】:

好吧,如果你不能定位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 组装它是一件轻而易举的事,而且我认为这个解决方案比 intel 更清晰。

至于你使用 cpuinfo 参数做什么,好吧,你可以尝试各种掩码来找出有关 cpu 类型的各种信息。如果我完成实施,我可能会更新。

【讨论】:

  • 我没有机会验证这个答案,但我感谢您为回答它付出的努力。我会给你积分。
  • @JasonKleban 你能帮我用这段代码获取 cpu id 吗?不是标志或供应商。
  • @JasonKleban 我找到了英特尔手册link 见第 25 页,它是关于 cpuid 语法的。但它是不可读的......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多