4

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
starblue
  • 55,348
  • 14
  • 97
  • 151
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
  • I just posted a related question, but not exactly the same as this (different approach) http://stackoverflow.com/questions/3216535/x86-x64-cpuid-in-c – Jason Kleban Jul 09 '10 at 21:01

1 Answers1

4

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.

  • I have not had a chance to verify this answer, but I appreciate the effort you put into answering it. I'll award you the points. – Jason Kleban Mar 23 '11 at 17:41
  • @JasonKleban Can you help me to get cpu **id** using this code? Not flags or vendor. – Woland Oct 13 '17 at 21:29
  • @JasonKleban I have found intel manual [link](http://www.jaist.ac.jp/iscenter-new/mpc/altix/altixdata/opt/intel/vtune/doc/IA_32_Instructions_2A.pdf) see 25th page, it is about cpuid syntax. But it is unreadable... – Woland Oct 13 '17 at 21:32