.Trust our experience in High-Power Network Tools and Communication Tools.

Using Syscall

This is a complete working example which will make a Syscall call to retrieve the number of processors.

  • Note1: It will only run on a 64 bit Operating System.
  • Note2: the system service 33h used in the example is only suitable for Windows 7 – 64 bit. For Windows 10 – 64 bit (at least until Redstone 2) the system service number is 36h. For other editions look up here (function NtQuerySystemInformation).
; ml64 -c -Zp8 numprocessors.asm
; link /entry:main /SUBSYSTEM:console numprocessors.obj

option casemap :None

includelib \masm32\lib64\kernel32.lib
ExitProcess proto :dword
includelib \masm32\lib64\msvcrt.lib
printf proto :ptr, :vararg

_SYSTEM_BASIC_INFORMATION struct 8
    Reserved1 byte 24 dup (?) ; total=24
    Reserved2 qword 4 dup (?) ; total=32
    NumberOfProcessors sbyte ? ; total=8
_SYSTEM_BASIC_INFORMATION ends

.data
format0 db "Number of processors: %d retlen: 0x%x retval: 0x%x",13,10,0

.code

main proc
LOCAL basicinfo : _SYSTEM_BASIC_INFORMATION
LOCAL retlen : qword

    mov retlen,0
    lea r9, retlen
    mov r8, sizeof basicinfo
    lea rdx, basicinfo
    mov r10, 0
    mov eax, 33h ; Windows 7=0x0033, Windows 10 until redstone2=0x0036

    syscall

    lea rcx, format0
    xor rdx, rdx
    mov dl, basicinfo.NumberOfProcessors
    mov r8, retlen
    mov r9d, eax
    sub rsp, 20h
    call printf
    add rsp, 20h
    mov rcx,0
    call ExitProcess

main endp
end

Result obtained in the test computer:

Number of processors: 12 retlen: 0x40 retval: 0x0

Note: The number of processors corresponds to the number of threads of the CPU, not the number of cores, which in this case is 6.