News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how can get the cpu type, 64 bit or 32 bit in asm?

Started by feicong, September 03, 2009, 01:09:25 PM

Previous topic - Next topic

feicong

how can get the cpu type, 64 bit or 32 bit in asm?

my system is windows xp 32bit ,how can i do it ?
any api or x86 Instruction ?

thanks!

dedndave

the registry has the information
or you can use cpuid, if that instruction is supported
if it isn't, you don't have a 64-bit processor
identifying a 64-bit cpu is different than identifying a 64-bit OS
also, some processors support "ia64" or "EM64T", which is an address-limited version of 64-bit
the exact answer to your question depends on what you want to use the information for


dedndave

before you use the CPUID instruction, you should test to see if it is supported
otherwise, the program will crash with an invalid opcode exception
here is the code i use to see if the CPU supports CPUID:

;verify CPUID supported

        pushfd
        pop     eax
        mov     edx,eax                 ;edx=original
        xor     eax,200000h             ;eax=toggled
        push    eax
        popfd                           ;eflags=new
        pushfd
        pop     eax                     ;eax=new
        push    edx
        popfd                           ;eflags=original
        xor     eax,edx                 ;eax=toggle result
        test    eax,200000h
        jz      cpuid_instruction_not_supported

there are a number of documents regarding the cpuid instruction
it is implemented a little differently on different processors
some cyrix cpu's even require the instruction to be enabled - very messy

http://www.intel.com/Assets/PDF/appnote/241618.pdf
http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25481.pdf
http://www.sandpile.org/ia32/cpuid.htm

thomas_remkus

OK, so I'm a "dedndave" fan. I think I must print out more code from your snippets than from anyone else and try and figure out what they do. Like this for instance, right now it looks like goop, pushing and popping for something that seems meaningless ... I think I'll comment this code like I have some other stuff of yours. But man, that code for your big number conversion ... wow. Thanks for all your replies ... I can't wait to see what you do when you get buy into the 64-bit masm stuff.

Slugsnack

lmao didn't know there was a way to test whether the CPUID instruction exists. i was always setting up an SEH before trying to use it, then handling the generated exception if it came

dedndave

the technique is described in the CPUID documents i mentioned above
all they are doing is testing to see whether bit 21 of the eflags register can be altered
if it can be, it means the cpu supports the cpuid function
on older cpu's (i.e before cpuid), the bit was unused and always stuck in one state (i think it was a 1 bit)
most processors support cpuid, even later versions of 386's, i think
now, i did add something to that
my code also returns it to it's initial value
i have learned a little more about multi-processing and i think that step really isn't neccessary
but, i was trying to avoid collisions if some other program is running concurrently and trying to test the same thing
now, i know the other processes get their own version of the eflags register
so the last push/pop pair in the code above can be removed...

;verify CPUID supported

        pushfd
        pop     eax
        mov     edx,eax                 ;edx=original
        xor     eax,200000h             ;eax=toggled
        push    eax
        popfd                           ;eflags=new
        pushfd
        pop     eax                     ;eax=new
        xor     eax,edx                 ;eax=toggle result
        test    eax,200000h
        jz      cpuid_instruction_not_supported

you might be able to make it a little smaller by using BSWAP...

        xor     eax,edx                 ;eax=toggle result
        bswap   eax
        test    ah,20h
        jz      cpuid_instruction_not_supported

EDIT - here's an even better version - it only uses the AL register

;verify CPUID supported

        pushfd
        mov     al,[esp+2]
        xor     al,20h
        mov     [esp+2],al
        popfd
        pushfd
        xor     al,[esp+2]
        popfd
        test    al,20h
        jnz     cpuid_instruction_not_supported

as for being a dedndave fan - lol
i am a newbie to 32-bit code, as well - there are a bunch of guys in here that are way more advanced than i am
i do have a few old tricks up my sleeve from 16-bit days, though - lol
i used to write device drivers and specialized math and graphics routines (old code)
much of my old experience no longer applies

2-Bit Chip

#7
Greetings feicong!

I have written a procedure that will get the processor architecture (x86 or x64), and you or anyone else are welcomed to use it.

IFNDEF KERNEL32_INC
    include \masm32\include\kernel32.inc
    includelib \masm32\lib\kernel32.lib
ENDIF

X86_ARCH equ <1>
X64_ARCH equ <NOT X86_ARCH>

.code

GetSysArch proc
    LOCAL _SysArch_buffer[5]: BYTE

    fn GetEnvironmentVariable, "PROCESSOR_ARCHITECTURE", ADDR _SysArch_buffer, SIZEOF _SysArch_buffer

    fn szCmp, ADDR _SysArch_buffer, "x86"

    .IF eax != 0
   
        xor eax, eax
        inc eax

    .ENDIF

    ret
   
GetSysArch endp



It can be used like so:
    invoke GetSysArch

    .IF eax == X86_ARCH

        print chr$("x86",13,10)

    .ELSE

        print chr$("NOT x86 (x64)",13,10)

    .ENDIF

FORTRANS

Quote from: dedndave on September 04, 2009, 06:46:35 PM
the technique is described in the CPUID documents i mentioned above
all they are doing is testing to see whether bit 21 of the eflags register can be altered
if it can be, it means the cpu supports the cpuid function
on older cpu's (i.e before cpuid), the bit was unused and always stuck in one state (i think it was a 1 bit)
most processors support cpuid, even later versions of 386's, i think

   Intel introduced CPUID with the Pentium processor.  It
was then added to the last of the (Intel) 486'es.

Steve

BlackVortex

Better read it from the registry and avoid this craziness.

2-Bit Chip


dedndave

QuoteBetter read it from the registry and avoid this craziness
what craziness ?
oh - you must be refering to CPUID and their "whack-a-mole" technology
bang one bit with the hammer, and another one pops up ? - lol