2010-07-08 23 views
4

我试图让一些C & ASM示例代码,我发现在运行Visual Studio 2008中,我不认为这个问题是VS 2005 - 2008年之间的差异。 This example应该在64位系统上获得CPUID。 (我尝试获得ASM-仅32位的例子编译太失败了)获得64位CPUID示例代码编译VS2008

我可以复制和将此代码粘贴到一个新的项目,但我一直没能建立它。我尝试了几个不同的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 
+0

我刚刚发布了一个相关的问题,但不完全一样,这(不同的方法)http://stackoverflow.com/questions/3216535/x86-x64-cpuid-in-c – 2010-07-09 21:01:07

回答

4

好吧,如果你不能定位x64,你有一个小问题因为你没有使用x64工具链。我强烈建议您使用VS安装向导来添加x64工具。您应该能够转到新的平台,将其称为x64并将其基于win32。尽管如此,我实际上推荐使用YASM,因为它允许你与VS集成,而YASM是比微软更好的汇编器。

因为我碰巧有一个无论如何都会受益于这个项目,我想我会用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; 
} 

的readme.txt在链接下载页面上提供的vs2010存档中,用vs来组装这个是一件轻而易举的事情,我认为这个解决方案比intels更清晰。

至于你在做了什么与cpuinfo参数,以及,你可以尝试各种掩模来找出各种关于CPU类型的信息。如果我完成实施,我可能会更新。

+0

我还没有机会验证这个答案,但我很欣赏你为回答它所付出的努力。我会给你奖励点数。 – 2011-03-23 17:41:15

+0

@JasonKleban你能帮我拿到cpu ** id **使用这段代码吗?不是标志或供应商。 – Woland 2017-10-13 21:29:04

+0

@JasonKleban我发现英特尔手册[链接](http://www.jaist.ac.jp/iscenter-new/mpc/altix/altixdata/opt/intel/vtune/doc/IA_32_Instructions_2A.pdf)见第25页,它关于cpuid语法。但它是不可读的... – Woland 2017-10-13 21:32:28