簡體   English   中英

獲取64位CPUID示例代碼以在VS2008中編譯

[英]Getting 64-bit CPUID sample code to compile in VS2008

我試圖獲得一些我發現在Visual Studio 2008中運行的c&ASM示例代碼。我不認為問題是VS 2005-2008之間的區別。 這個例子應該在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是迄今為止比微軟更好的匯編程序。

因為我碰巧有一個項目可以從中受益,我想我會用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組裝它是一個輕而易舉的事情,我認為這個解決方案比intels更清晰。

至於你如何處理cpuinfo參數,你可以嘗試各種掩碼來找出有關cpu類型的各種信息。 如果我完成實施,我可能會更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM