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

Using Sysenter

(First published in The Masm Forum)

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

  • Note1: It will only run on a 32 bit Operating System (Will not run on Wow64)
  • Note2: The system service 105h is only suitable for Windows 7 – 32 bit and it was only tested on Windows 7 – 32 bit. For other editions look up here (function NtQuerySystemInformation).
  • Note3: The point that is usually disregarded is that the stacked arguments begin 8 bytes above top of stack.
; ml" -c -coff numprocessors.asm
; link" /SUBSYSTEM:console numprocessors.obj

.686

.model flat, stdcall
option casemap :None

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

_SYSTEM_BASIC_INFORMATION struct 4
    Reserved1 byte 24 dup (?)
    Reserved2 dword 4 dup (?)
    NumberOfProcessors sbyte ?
_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 : dword

 mov retlen,0
 lea eax, retlen
 push eax
 push sizeof basicinfo
 lea eax, basicinfo
 push eax
 push 0
 mov eax, 105h ; Value for Windows 7=0x105
 push 0 ; Any value (see below)
 push offset @f ; The convention is that the stacked arguments begin 8 bytes above top of stack
 mov edx, esp

 sysenter

@@:
 add esp, 20
 invoke printf, addr format0, basicinfo.NumberOfProcessors, retlen, eax
 invoke ExitProcess,0

main endp

end main

Result obtained in the test computer:

Number of processors: 4 retlen: 0x2c retval: 0x0