flat assembler
Message board for the users of flat assembler.

flat assembler > Windows > CPUID MM Info

Author
Thread Post new topic Reply to topic
bitshifter



Joined: 04 Dec 2007
Posts: 754
Location: Massachusetts, USA
Hello
I made app that test for MMX/SSE/SSE2/SSE3.
Maybe someone knows a bit and could review it?
Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

format PE GUI 4.0
entry start

include 'win32ax.inc'

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

section '.code' code readable executable

    start:

        ; INVOKE CPUID FUNCTION 0x00000000

        xor     eax,eax
        cpuid

        ; EXTRACT VENDOR STRING

        mov     [g_buffer.vendor_ebx],ebx
        mov     [g_buffer.vendor_edx],edx
        mov     [g_buffer.vendor_ecx],ecx

        ; CHECK FOR FUNCTION 0x00000001

        cmp     eax,0x00000001
        jl      .NoCPUID_FN1

        ; INVOKE CPUID FUNCTION 0x00000001

        mov     eax,0x00000001
        cpuid

        ; TEST FOR MMX

        test    edx,00000000100000000000000000000000b
        jz      .NoMMX
        mov     [g_buffer.mmx_ach4],'YES '
    .NoMMX:

        ; TEST FOR SSE

        test    edx,00000010000000000000000000000000b
        jz      .NoSSE
        mov     [g_buffer.sse_ach4],'YES '
    .NoSSE:

        ; TEST FOR SSE2

        test    edx,00000100000000000000000000000000b
        jz      .NoSSE2
        mov     [g_buffer.sse2_ach4],'YES '
    .NoSSE2:

        ; TEST FOR SSE3

        test    ecx,00000000000000000000000000000001b
        jz      .NoSSE3
        mov     [g_buffer.sse3_ach4],'YES '
    .NoSSE3:

    .NoCPUID_FN1:

        ; DISPLAY RESULTS AND QUIT

        invoke  MessageBox,NULL,g_buffer,'CPUID',MB_OK+MB_ICONINFORMATION
        invoke  ExitProcess,0
        jmp     $

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

section '.data' data readable writeable

    g_buffer:
                        db 'VENDOR',9,9,'- '
      .vendor_ebx       dd 0
      .vendor_edx       dd 0
      .vendor_ecx       dd 0
                        db 9,13,10

                        db 'MMX',9,9,'- '
      .mmx_ach4         dd 'NO  '
                        db 13,10

                        db 'SSE',9,9,'- '
      .sse_ach4         dd 'NO  '
                        db 13,10

                        db 'SSE2',9,9,'- '
      .sse2_ach4        dd 'NO  '
                        db 13,10

                        db 'SSE3',9,9,'- '
      .sse3_ach4        dd 'NO  '
                        db 13,10

                        db 0 ; end g_buffer

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

section '.idata' import data readable

    library kernel32,'kernel32.dll',\
            user32,'user32.dll'

    include 'api\kernel32.inc'
    include 'api\user32.inc'

    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 29 Sep 2009, 03:07
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
Works fine for me.


I made something just like this a few months back, but it only tested for all the SSE's.

_________________
----> * <---- My star, won HERE


Last edited by windwakr on 29 Sep 2009, 03:17; edited 2 times in total
Post 29 Sep 2009, 03:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16733
Location: In your JS exploiting you and your system
It is not enough just to test the CPUID flags for extensions. You also need to check that the OS supports them. A reasonable way to do that is to execute one of the instructions with dummy parameters and catch any exceptions. Indeed by doing the exception catching you can test both the CPU capability and the OS capability at the same time so using the CPUID method becomes redundant.

However, if you do still want to use CPUID then you should also check that the CPU supports it before trying to execute it. Or perhaps instead, just execute it blindly and catch any exception there also.
Post 29 Sep 2009, 03:17
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
If you want to be sure your code runs good on early 90's computers that OH SO many people use nowadays(right revolution?), then read up on this document.
Post 29 Sep 2009, 03:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16733
Location: In your JS exploiting you and your system
windwakr wrote:
If you want to be sure your code runs good on early 90's computers that OH SO many people use nowadays(right revolution?)
One never knows the intended application. Many embedded systems still use 386sx CPUs, and people are still writing software to run on them. How can we know who uses what?
Post 29 Sep 2009, 03:25
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 754
Location: Massachusetts, USA
windwakr wrote:
I made something just like this a few months back...

Cool!
You sure got your moneys worth out of all those lstrcat's Shocked

Thanks for the info revolution.
I thought all Win32 would be ok?

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 29 Sep 2009, 03:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16733
Location: In your JS exploiting you and your system
bitshifter wrote:
I thought all Win32 would be ok?
Unfortunately, no.
Post 29 Sep 2009, 03:32
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16733
Location: In your JS exploiting you and your system
Here is a small snippet from some really old code:
Code:
macro _try {
          local   .handler,.catch,.finally
            push    .handler
            push    dword[fs:0]
             mov     [fs:0],esp
      try@finally equ .finally
        macro _catch \{
           jmp     .finally        
        .handler:
           mov     eax,pContext
                mov     [eax+CONTEXT.Eip],.catch
          if EXCEPTION_CONTINUABLE<>0
             mov     eax,EXCEPTION_CONTINUABLE
         else
          xor     eax,eax
           end if
                ret     16
      .catch:
             mov     esp,[fs:0]
          \}
}
macro _finally {
     try@finally:
            pop     dword[fs:0]
             lea     esp,[esp+4]
 restore try@finally
 purge _catch
}

macro _assert_sse_capable_OS exec
{   _try
    or      eax,-1
      exec    xmm0,xmm0
    _catch
     xor     eax,eax
    _finally
     test    eax,eax
}

       ;SSE
        _assert_sse_capable_OS xorps
        jz      .not_support_SSE
    ;SSE2
       _assert_sse_capable_OS xorpd
        jz      .not_support_SSE2
   ;SSE3
       _assert_sse_capable_OS addsubpd
     jz      .not_support_SSE3    
Post 29 Sep 2009, 03:41
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
(blah blah blah, snipped a long semi-useless bunch of boring info)

Code:
.global _is_cpuid_supported

_is_cpuid_supported:
  push ebx
  pushfd                               # get extended flags
  pop eax
  mov ebx,eax                          # save current flags
#  xor eax,1 shl CPUID_FLAG
  xor eax,0x200000
  push eax                             # put new flags on stack
  popfd                                # flags updated now in flags
  pushfd                               # get extended flags
  pop eax
  xor eax,ebx                          # if bit 21 r/w then supports CPUID
  mov eax,0
  jz _is_cpuid_supported_bye
  inc eax
_is_cpuid_supported_bye:
  pop ebx
  ret
    


Code:
.global _get_cpu_flags

_get_cpu_flags:
  pushad                               # save all (better safe than sorry!)
  xor eax,eax
  inc eax
  cpuid
  xor eax,eax
#  bt edx,MMX_FLAG                     # bit 23
  bt edx,23
  adc eax,0                            # if Carry mov al, 1
#  bt edx,SSE2_FLAG                    # bit 26
  bt edx,26
  adc eax,0                            # if Carry, set eax = 2
  mov [esp+0x1C],eax                   # replace soon-popped EAX on stack
  popad                                # restore all standard 386 registers
  ret
    



(suggested by Agner Fog, works for me)
Code:
_sse_supported:
  fxsave [xmm_save]
  mov dword ptr [xmm_save+160],0x45443043   # 'C0DE'
  fxrstor [xmm_save]
  mov dword ptr [xmm_save+160],0x44303044   # 'D00D'
  fxsave [xmm_save]
  cmp dword ptr [xmm_save+160],0x45443043   # 'C0DE'
  mov eax,0
  jnz _sse_supported_bye
  inc al                     # if OSFXSR not turned on, XMM* are not saved
_sse_supported_bye:
  ret
    


EDIT: Oh well, attachment of my conversion of fastpaq2.s (from PAQ7/PAQ8 series) didn't work. You guys probably don't need/want it anyways.
Post 29 Sep 2009, 08:56
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
windwakr wrote:
If you want to be sure your code runs good on early 90's computers that OH SO many people use nowadays(right revolution?), then read up on this document.


Wow, you have a very common name. Last Sunday I also was watching the Family Guy etc. shows on Fox, and immediately after American Dad (whose son's name you share) the news mentioned something with some Corporal (military dude) also named the same thing. Heh. Crazy world. Laughing
Post 29 Sep 2009, 09:05
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.