News:

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

Determining Which instructions CPU supports

Started by ASMManiac, February 15, 2012, 06:59:52 PM

Previous topic - Next topic

ASMManiac

I am looking for a way to determine which instructions my CPU supports e.g. SSE, SSE2 ... AVX  etc.  I am especially interested in AVX
How can I determine this by looking in my cpu settings (I am using windows 7)?

If I don't have an instruction but I write assembly code using that instruction, will it assemble into code that my machine will not understand or misinterpret and crash?

jj2007

Try this...

include \masm32\include\masm32rt.inc
.code
start:
  db 0Fh, 0A2h
  bt ecx, 28 ; edx bit 26, SSE2
  .if Carry?
  MsgBox 0, "AVX supported", "Your CPU:", MB_OK
  .else
  MsgBox 0, "No AVX, sorry", "Your CPU:", MB_OK
  .endif
  exit
end start

clive

Quote from: ASMManiacI am looking for a way to determine which instructions my CPU supports e.g. SSE, SSE2 ... AVX  etc.  I am especially interested in AVX. How can I determine this by looking in my cpu settings (I am using windows 7)?

You'd use CPUID (JJ encoded as bytes), and use the mechanisms Intel and AMD have documented to determine the processors feature set. You'd typically do this once, and change your code paths accordingly.

QuoteIf I don't have an instruction but I write assembly code using that instruction, will it assemble into code that my machine will not understand or misinterpret and crash?

The assembler doesn't know what instructions will be present on the machine where the code is run, to handle a faulting instruction you'd put a SEH (structured exception handler) around it. Without one the machine will not explode, but the OS will kill your application.
It could be a random act of randomness. Those happen a lot as well.

baltoro

ASM MANIAC,
Here is an excellent MASM Forum thread that explores the full glory of the CPUID Instruction (author: DAVE): Another CPU ID Program
I discovered through experience writing one of my own MASM programs that using the Windows API: IsProcessorFeaturePresent produces unreliable results.
The DAVE technique is optimal. :eek
...And, if you're interested, here is the OFFICIAL technical description from Intel: Intel® Processor Identification and the CPUID Instruction
Baltoro

dedndave

i might add....
Jochen hard-coded the CPUID instruction so that it would work in all cases
you do not have to hard-code it if the processor is .586 or higher

however, his comment on this line is a little confusing   :red
  bt ecx, 28  ; edx bit 26, SSE2
also, EAX should be set to 1
        .586

        push    1
        pop     eax
        cpuid
        bt      ecx,28       ;AVX ?
        jc      avx_present

jj2007


dedndave

yah - but i stole the PUSH 1, POP EAX from you   :lol

ASMManiac

why is
  push    1
  pop     eax
better than
  mov eax,1
?

ASMManiac

How do I get the functions included in the line "include \masm32\include\masm32rt.inc"
I am using jWasm not Masm

dedndave

PUSH 1, POP EAX is a few bytes smaller than MOV EAX,1   :P
it's also slower - but, CPUID is slow anyways, so you may as well go for small

you should still be able to use the masm32rt.inc file with JwAsm
to see what it does, open it with NotePad or QE - it is a text file

also - a note about include files...
Andreas (Japheth) also has a set of "wininc" include files that differ from those with masm32
he went for a 1:1 translation of the MS C header files - a bit different than using the masm32 includes
also - masm32 has a library of functions and macros added that wininc does not
i would have to say that using Andreas' wininc files is for intermediate or advanced ASM programmers, at least
with some considerable effort, you can mix the two   :P