Getting 64-bit CPUID sample code to compile in VS2008

好久不见. 提交于 2019-12-21 05:16:31

问题


I'm trying to get some c & ASM sample code I found running in Visual Studio 2008. I don't think the problem is the difference between VS 2005-2008. This example is supposed to get the CPUID on 64-bit systems. (My attempts getting the ASM-only 32-bit examples to compile failed too)

I can copy and paste this code into a new project, but I have not been able to build it. I've tried a few different VS project templates with no success. I believe I followed the instructions all the way. Can someone provide a step-by-step to get this running in Visual Studio 2008 with the project template and project settings?

One thing I've noticed is that although I can make the environment be 64-bit, I can't seem to target x64 for this project - the only options for adding new platforms are mobile platforms. And I've had to manually define _M_X64 in the command-line options, which I suspect I shouldn't have to do.

Not to be debugged directly, but just for your info - Errors, as far as I got, are as follows:

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

回答1:


Well, if you can't target x64, you have a slight problem because you're not using the x64 toolchain. I strongly suggest you use the VS install wizard to add in the x64 tools. You should be able to go to new platform, call it x64 and base it on win32.

Having said that, I actually recommend using YASM because it allows you to integrate with VS, and YASM is by far a nicer assembler than Microsoft's.

Since I happen to have a project that would benefit from this anyway, I thought I'd do it using 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;
}

Following readme.txt in the vs2010 archive supplied on the links download page, it was a doddle to get vs to assemble this, and this solution is much clearer, I think, than intels.

As to what you do with the cpuinfo parameter, well, you can try various masks to find out all sorts of info about the cpu type. I might update if I finish the implementation.



来源:https://stackoverflow.com/questions/3208083/getting-64-bit-cpuid-sample-code-to-compile-in-vs2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!