» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

Getting CPU information through inline ASM

The nice thing with ASM is that you can also do things that are impossible to do directly in VB.
For example, getting the processor capabilities. This piece of code will return various CPU information.

A simple but working example can be downloaded in one of the ASM example collections I once made (here)


'//How-to use: this is an example function, showing how to use CPUID instruction
'//Call it like:
' Dim lngPointer1 As Long, lngPointer2 As Long, lngPointer3 As Long
' Dim lngCPUFeatures(4) As Long, lngCacheInfo(4) As Long
'
' '//Get the 3 pointers to the array
' lngPointer1 = VarPtr(myLong(0))
' lngPointer2 = VarPtr(lngCPUFeatures(0))
' lngPointer3 = VarPtr(lngCacheInfo(0))
'
' '//Call the ASM
' Call asmCPUID(lngPointer1, lngPointer2, lngPointer3)
' Please see the reference manual of the instruction how to interpret the results

Public Function asmCPUID(ByVal lngPointer1 As Long, ByVal lngPointer2 As Long, ByVal lngPointer3 As Long) As Long
'#ASM_START
'.686

' push ebp
' mov ebp, esp

' push ecx
' push edx
' push edi

' mov eax, 0
' CPUID
'
' ;//Write the CPU String back to the DLL
' ;//They are in ebc,edx, and ecx
' mov edi, DWORD PTR [ebp+8] ;//Pointer to array1
' mov [edi], ebx
'
' mov edi, DWORD PTR [ebp+8] ;//Pointer to array1
' Add edi, 4
' mov [edi], edx
'
' mov edi, DWORD PTR [ebp+8] ;//Pointer to array1
' Add edi, 8
' mov [edi], ecx
' ;\
';-------------------------------------------------------------------------------------
'
' ;//Do we have additional options in the processor?
' ;//eax == 0?, if not, continue, else, goto exit
' cmp eax, 0
' je ExitASM
'
';-------------------------------------------------------------------------------------
'
' ;//Get additional features
' mov eax, 1
' CPUID
'
' mov edi, [ebp+12] ;//Pointer to array2
' mov [edi], eax
'
' Add edi, 4
' mov [edi], ebx
'
' Add edi, 4
' mov [edi], ecx
'
' Add edi, 4
' mov [edi], edx
'
';-------------------------------------------------------------------------------------
'
' ;//Do we have additional options in the processor?
' ;//eax > 1?, if not, continue, else, goto exit
'
' ;//Get value again
' mov eax, 0
' CPUID
'
' cmp eax, 1
' jng ExitASM
'
' ;//Get additional features
' mov eax, 2
' CPUID
'
' mov edi, [ebp+16] ;//Pointer to array3
' mov [edi], eax
'
' Add edi, 4
' mov [edi], ebx
'
' Add edi, 4
' mov [edi], ecx
'
' Add edi, 4
' mov [edi], edx
';-------------------------------------------------------------------------------------
'ExitASM:
' pop edi
' pop edx
' pop ecx

' mov esp, ebp ;'//MOV/POP is much faster
' pop ebp ;'//on 486 and Pentium than Leave
' ret 12
'#ASM_END
End Function


User contributed notes:

Author: Tom (hurendo_kun at hotmail dot com) Date: 05:09 23/09/2005
I tried to do something like this once, and my program kept crashing. Hmmm.

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: