flat assembler
Message board for the users of flat assembler.

Index > Windows > My first Win32 dialog app, CPU-Y

Author
Thread Post new topic Reply to topic
Mat Quasar



Joined: 15 Dec 2024
Posts: 54
Mat Quasar 09 Jan 2025, 08:15
Work in progress.....

I don't know how to make the EDIT control locked, but not disabled. If disabled, cannot right click for menu. I still want the text can be copied, but not deleted.

Uh oh, web search shows that there is an Android sysinfo app already used this name, CPU-Y, back in 2020. Maybe I should just name it something else.

UPDATE #1, 9 Jan 2025, 1:56PM UTC: Added ES_READONLY style to EDIT controls (Thanks @Hrstka)
Code:
;CPU-Y, developed by Mat Quasar (aka fliermate), 2025/1
;
;Reference:
; - https://board.flatassembler.net/topic.php?t=15628
; - https://codeby.net/threads/cpuid-identifikacija-processora.78279/
; - https://board.flatassembler.net/topic.php?t=23697

format PE GUI
include 'win32a.inc'

IDD_MAIN        = 100
IDC_CPUNAME     = 101
IDC_CORES       = 102
IDM_MAIN        = 200

section '.text' code readable executable

  entry $

        mov     eax, 0x80000002
        cpuid
        mov     dword [_name], eax
        mov     dword [_name + 4], ebx
        mov     dword [_name + 8], ecx
        mov     dword [_name + 12], edx
        mov     eax, 0x80000003
        cpuid
        mov     dword [_name + 16], eax
        mov     dword [_name + 20], ebx
        mov     dword [_name + 24], ecx
        mov     dword [_name + 28], edx
        mov     eax, 0x80000004
        cpuid
        mov     dword [_name + 32], eax
        mov     dword [_name + 36], ebx
        mov     dword [_name + 40], ecx
        mov     dword [_name + 44], edx

        mov     eax,1
        cpuid
        movzx   ecx,bl
        movzx   edx,bh
        shl     edx,3
        shr     ebx,16
        movzx   esi,bl
        movzx   edi,bh

        mov     [_core], esi
        mov     eax, [_core]
        call    itoa
        mov     [_len], ebx

 ;       cinvoke  printf,<10,' ***** EAX=1 ****************',\
 ;                        10,'   Family & Model.:  0x%x',\
 ;                        10,'   Brand index....:  0x%02x',\
 ;                        10,'   Cache line.....:  %d byte',\
 ;                        10,'   CPU cores......:  %d',\
 ;                        10,'   APIC id........:  %d',\
 ;                        10,'   Feature flags..:  ',0>, eax,ecx,edx,esi,edi

        invoke  GetModuleHandle,0
        invoke  DialogBoxParam,eax,IDD_MAIN,HWND_DESKTOP,DialogProc,0
        or      eax,eax
        jz      exit
  exit:
        invoke  ExitProcess,0

itoa:                      Wink Nice code snippet by Pauli Lindgren  (https://pkl.paldex.fi/)
; INPUT:  EAX
; OUTPUT: _number
; LEN OF OUTPUT: EBX
         mov     ecx, 10
         xor     ebx, ebx

.divide:
         xor     edx, edx
         div     ecx
         push    edx
         inc     ebx
         test    eax, eax
         jnz     .divide

         mov     ecx, ebx
         lea     esi, [_number]
.next:
         pop     eax
         add     al, '0'
         mov     [esi], al
         inc     esi
         loop    .next
         ret

proc DialogProc hwnddlg,msg,wparam,lparam
        push    ebx esi edi
        cmp     [msg],WM_INITDIALOG
        je      .wminitdialog
        cmp     [msg],WM_CLOSE
        je      .wmclose
        xor     eax,eax
        jmp     .finish
  .wminitdialog:
        ;invoke  GetDlgItem,[hwnddlg],IDC_CPUNAME
        ;mov     [hwndcpuname],eax
        ;invoke  GetDlgItem,[hwnddlg],IDC_CORES
        ;mov     [hwndcores],eax

        invoke  SetDlgItemText,[hwnddlg],IDC_CPUNAME,_name
        invoke  SetDlgItemText,[hwnddlg],IDC_CORES,_number

        ;invoke  SendMessage,[hwndcpuname],EM_SETSEL,0,-1
        ;invoke  SetFocus,[hwndcpuname]
        jmp     .processed
  .wmclose:
        invoke  EndDialog,[hwnddlg],0
  .processed:
        mov     eax,1
  .finish:
        pop     edi esi ebx
        ret
endp

section '.data' data readable writeable

  _core       dd ?
  _len        dd ?
  hwndcpuname dd ?
  hwndcores   dd ?
  _number     rb 10
  _name       rb 48

section '.idata' import readable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import kernel,\
         GetModuleHandle,'GetModuleHandleA',\
         ExitProcess,'ExitProcess'

  import user,\
         DialogBoxParam,'DialogBoxParamA',\
         EndDialog,'EndDialog' ,\
         SetDlgItemText,'SetDlgItemTextA',\
         GetDlgItemText,'GetDlgItemTextA',\
         GetDlgItem,'GetDlgItem',\
         SendMessage,'SendMessageA',\
         SetFocus,'SetFocus'

section '.rsrc' resource data readable

  directory RT_DIALOG,dialogs

  resource dialogs,\
           IDD_MAIN,LANG_ENGLISH+SUBLANG_DEFAULT,main_dialog

  dialog main_dialog,'CPU-Y',0,0,500,250,WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_MODALFRAME
     dialogitem 'STATIC','CPU Name',-1,10,10,70,15,WS_VISIBLE
     dialogitem 'EDIT','0',IDC_CPUNAME,80,10,350,15,WS_VISIBLE+WS_BORDER+WS_TABSTOP+ES_AUTOHSCROLL+ES_READONLY 
     dialogitem 'STATIC','CPU Cores',-1,10,30,70,15,WS_VISIBLE
     dialogitem 'EDIT','0',IDC_CORES,80,30,50,15,WS_VISIBLE+WS_BORDER+WS_TABSTOP+ES_AUTOHSCROLL+ES_READONLY 
  enddialog
    


Description: I plan to make it like this....
Filesize: 7.32 KB
Viewed: 493 Time(s)

cpuy_746.png


Description: Currently it is like this...
Filesize: 4.57 KB
Viewed: 493 Time(s)

cpuy.PNG




Last edited by Mat Quasar on 09 Jan 2025, 13:57; edited 1 time in total
Post 09 Jan 2025, 08:15
View user's profile Send private message Reply with quote
Hrstka



Joined: 05 May 2008
Posts: 61
Location: Czech republic
Hrstka 09 Jan 2025, 12:13
Quote:
I don't know how to make the EDIT control locked, but not disabled. If disabled, cannot right click for menu. I still want the text can be copied, but not deleted.
Try ES_READONLY attribute.


Anyway, my CPU has 4 cores, but your program shows 16.
Post 09 Jan 2025, 12:13
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 732
Ali.Z 09 Jan 2025, 12:40
Hrstka wrote:

Anyway, my CPU has 4 cores, but your program shows 16.

free upgrade Laughing

_________________
Asm For Wise Humans
Post 09 Jan 2025, 12:40
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 54
Mat Quasar 09 Jan 2025, 13:55
Hrstka wrote:
Try ES_READONLY attribute.


Thanks! It works correctly now!


Hrstka wrote:
Anyway, my CPU has 4 cores, but your program shows 16.


Embarassed First of all, thanks for testing! Very much appreciated.

I checked the code (from @Core i7 member) and checked the manual, it seems the code is correct, i.e. call EAX=1 CPUID instruction, and then extract bits 23:16 from EBX register (shr 16, then take value of bl).

But Intel said this value is the maximum number of logical processor supported....

Both AMD and Intel manual:

Quote:
AMD:

CPUID Fn0000_0001_EBX LocalApicId, LogicalProcessorCount, CLFlush
Bits 23:16

LogicalProcessorCount: logical processor count.
If CPUID Fn0000_0001_EDX[HTT] = 1 then LogicalProcessorCount is the number of cores per
processor.
If CPUID Fn0000_0001_EDX[HTT] = 0 then LogicalProcessorCount is reserved.
See 3.1 [Legacy Method]

Intel:

Maximum number of logical processors per package (EBX[23:16]) - If the processor feature flags indicate the processor supports Multi-threading (see Section 5.1.2.4) then this is the maximum number of logical processors supported by the physical package.


Is your CPU Intel brand? I have no clue what's wrong here, except the code didn't check for Multi-threading feature bit.
Post 09 Jan 2025, 13:55
View user's profile Send private message Reply with quote
Hrstka



Joined: 05 May 2008
Posts: 61
Location: Czech republic
Hrstka 09 Jan 2025, 14:07
Yes, it's Intel(R) Core(TM) i5-4570 CPU.

Later I can try it on AMD processor.
Post 09 Jan 2025, 14:07
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 54
Mat Quasar 09 Jan 2025, 14:32
Hrstka wrote:
Yes, it's Intel(R) Core(TM) i5-4570 CPU.

Later I can try it on AMD processor.


Then I wait for your further feedback... Razz
Post 09 Jan 2025, 14:32
View user's profile Send private message Reply with quote
Hrstka



Joined: 05 May 2008
Posts: 61
Location: Czech republic
Hrstka 09 Jan 2025, 16:20
Works correctly on AMD.
Post 09 Jan 2025, 16:20
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 54
Mat Quasar 09 Jan 2025, 16:54
Hrstka wrote:
Works correctly on AMD.


Very Happy
Post 09 Jan 2025, 16:54
View user's profile Send private message Reply with quote
Core i7



Joined: 14 Nov 2024
Posts: 28
Location: Socket in motherboard
Core i7 09 Jan 2025, 16:57
Basic information about the CPU can be obtained in other ways:

1. In the system registry at the path: HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor (there is even a Features mask here). Reading is possible using WinApi RegOpenKey() + RegGetValue() and others from Advapi32.dll.

2. Windows environment variables, using WinApi GetEnvironmentString\Variable() from Kernel32.dll.
Here are some of the most interesting parameters that can be passed to this function:
Code:
        COMPUTERNAME           = xxx
        NUMBER_OF_PROCESSORS   = 2
        OS                     = Windows_NT
        PROCESSOR_ARCHITECTURE = AMD64
        PROCESSOR_IDENTIFIER   = Intel64 Family 6 Model 23 Stepping 10, GenuineIntel
        PROCESSOR_LEVEL        = 6
        PROCESSOR_REVISION     = 170a
        USERDOMAIN             = xxx-PC
        USERNAME               = xxx    

3. Direct query to the WMI toolkit.
The WinApi WinExec() function can launch processes for execution.
If you pass it the string "cmd.exe wmic cpu get /format:list > log.txt | exit" in the argument, then we will get a log file in the current directory, which can be displayed in the dialog box using the SetWindowText() function. However, the "log.txt" file is saved in UTF-16 encoding, and it will have to be converted to UTF-8 using the WideCharToMultiByte() function. The result will be something like this:
Code:
AddressWidth = 64
Architecture = 9
Availability = 3
Caption = Intel64 Family 6 Model 23 Stepping 10
CpuStatus = 1
CurrentClockSpeed = 2499
CurrentVoltage = 13
DataWidth = 64
Description = Intel64 Family 6 Model 23 Stepping 10
ExtClock = 200
Family = 2
L2CacheSize = 2048
L3CacheSize = 0
Level = 6
LoadPercentage = 1
Manufacturer = GenuineIntel
MaxClockSpeed = 2499
Name = Pentium(R) Dual-Core CPU E5200 @ 2.50GHz
NumberOfCores = 2
NumberOfLogicalProcessors = 2
ProcessorId = BFEBFBFF0001067A
ProcessorType = 3
Revision = 5898    

4. The number of cores can be read from the PEB structure:
in the "NumberOfProcessors" field, or in the binary mask "ActiveProcessAffinityMask" (see WinApi GetProcessAffinityMask()). However, in different versions of Windows the field offsets are different, so this option is questionable, since it is difficult to calculate offsets dynamically. But in the KUSER_SHARED_DATA structure the field with the number of cores always has a static address.
Post 09 Jan 2025, 16:57
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 54
Mat Quasar 09 Jan 2025, 17:34
Useful info for future reference. Thanks @Core i7.
Post 09 Jan 2025, 17:34
View user's profile Send private message Reply with quote
Core i7



Joined: 14 Nov 2024
Posts: 28
Location: Socket in motherboard
Core i7 09 Jan 2025, 18:11
Such programs as CPU-Z, Aspia, AIDA and others, in addition to displaying in the main window, allow you to save data to log files from the menu. These files contain much more information than in the window, and the software collects it from all possible pockets of the system. Also pay attention to the GetLogicalProcessorInformation() function from Kernel32.dll - there you can find the value of caches L1,2,3 and others.
Post 09 Jan 2025, 18:11
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1041
Location: Russia
macomics 09 Jan 2025, 18:40
5. Reading /proc/cpuinfo...
Post 09 Jan 2025, 18:40
View user's profile Send private message Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 54
Mat Quasar 10 Jan 2025, 11:16


Description: Extracted CPUID manual from Intel SDM
Download
Filename: CPUID_Intel_Oct2024.pdf
Filesize: 407.93 KB
Downloaded: 13 Time(s)



Last edited by Mat Quasar on 14 Jan 2025, 17:42; edited 1 time in total
Post 10 Jan 2025, 11:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 10 Jan 2025, 12:02
Note that "reserved" means that the bits has no defined value that is reliable. If the CPU is older then those bits should be ignored by the code. Only read the value of the bits if the CPU is newer and defines the values.

You might be able to get away with assuming the reserved bits are set to zeros, but you might also find that they get some other value that isn't valid. It isn't trivial to decode the CPUID bits Sad
Post 10 Jan 2025, 12:02
View user's profile Send private message Visit poster's website Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 54
Mat Quasar 10 Jan 2025, 12:10
revolution wrote:
Note that "reserved" means that the bits has no defined value that is reliable. If the CPU is older then those bits should be ignored by the code. Only read the value of the bits if the CPU is newer and defines the values.

You might be able to get away with assuming the reserved bits are set to zeros, but you might also find that they get some other value that isn't valid. It isn't trivial to decode the CPUID bits Sad


OMG, Shocked you pointed out my mistake about assumption that reserved bits are zero.....
Post 10 Jan 2025, 12:10
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.