Jump to content
Welcome, welcome! Come in and register, and have some developer coffee. 👨‍💻 ×

Let's create a DLL which return CPU identification


Recommended Posts

Today I will demonstrate how to create something like this in C# using DLL (Dynamic Linking Library) created in Assembly language.

CPU.png.81e9a10058c8057c8fae6d2d4733606f.png

 

CPU instructions set has CPUID to get the CPU identification.  I will show just one of them which is very basic info such as CPU family, CPU model and CPU stepping.

CPUID.png.aa393603376d80f167d036e92624c417.png

This is the source code to build a DLL in FASM which return CPU identification.

format PE GUI 4.0 DLL
entry DllEntryPoint

include 'win32a.inc'

section '.code' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
     mov     eax, TRUE
     ret
endp

proc CPUType
       push ebx
       xor      eax, eax
       inc      eax
       cpuid
       mov      edx, eax
       shl      edx, 32-14
       shr      edx, 32-2
       mov      word [_cpu_type], dx
       movzx    eax, word [_cpu_type]
       pop      ebx
       ret
endp

proc CPUModel
       push ebx
       xor      eax, eax
       inc      eax
       cpuid
       mov      edx, eax
       shl      edx, 32-8
       shr      edx, 32-4
       mov      dword [_model], edx
       mov      eax, dword [_model]
       pop      ebx
       ret
endp

proc CPUExtModel
       push ebx
       xor      eax, eax
       inc      eax
       cpuid
       mov      edx, eax
       shl      edx, 32-20
       shr      edx, 32-4
       mov      dword [_ext_model_id], edx
       mov      eax, dword [_ext_model_id]
       pop      ebx
       ret
endp

proc CPUFamily
       push ebx
       xor      eax, eax
       inc      eax
       cpuid
       mov      edx, eax
       shl      edx, 32-12
       shr      edx, 32-4
       mov      dword [_family_id], edx
       mov      eax, dword [_family_id]
       pop      ebx
       ret
endp

proc CPUExtFamily
       push ebx
       xor      eax, eax
       inc      eax
       cpuid
       mov      edx, eax
       shl      edx, 32-28
       shr      edx, 32-4
       mov      dword [_ext_family_id], edx
       mov      eax, dword [_ext_family_id]
       pop      ebx
       ret
endp

proc CPUStepping
       push ebx
       xor      eax, eax
       inc      eax
       cpuid
       mov      edx, eax
       shl      edx, 32-4
       shr      edx, 32-4
       mov      dword [_stepping_id], edx
       mov      eax, dword [_stepping_id]
       pop      ebx
       ret
endp

section '.data' readable writable

       _stepping_id     dd      ?
       _model           dd      ?
       _family_id       dd      ?
       _cpu_type        dw      ?
       _ext_model_id    dd      ?
       _ext_family_id   dd      ?

section '.edata' export data readable

  export 'CPU.DLL',\
         CPUType, 'CPUType',\
         CPUModel, 'CPUModel',\
         CPUExtModel, 'CPUExtModel',\
         CPUFamily, 'CPUFamily',\
         CPUExtFamily, 'CPUExtFamily',\
         CPUStepping, 'CPUStepping'

section '.reloc' fixups data readable discardable

  if $=$$
    dd 0,8              ; if there are no fixups, generate dummy entry
  end if

 

And this is how we use it in higher-level programming language, such as C#:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CPUInfo
{
    public partial class Form1 : Form
    {
        [DllImport(@"C:\FASM\DLL\CPU\CPU.DLL")]
        private static extern uint CPUType();

        [DllImport(@"C:\FASM\DLL\CPU\CPU.DLL")]
        private static extern uint CPUModel();

        [DllImport(@"C:\FASM\DLL\CPU\CPU.DLL")]
        private static extern uint CPUExtModel();

        [DllImport(@"C:\FASM\DLL\CPU\CPU.DLL")]
        private static extern uint CPUFamily();

        [DllImport(@"C:\FASM\DLL\CPU\CPU.DLL")]
        private static extern uint CPUExtFamily();

        [DllImport(@"C:\FASM\DLL\CPU\CPU.DLL")]
        private static extern uint CPUStepping();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "CPU Type: " + CPUType().ToString();
            textBox1.Text += "\r\nCPU Ext Family: " + CPUExtFamily().ToString();
            textBox1.Text += "\r\nCPU Family: " + CPUFamily().ToString();
            textBox1.Text += "\r\nCPU Ext Model: " + CPUExtModel().ToString();
            textBox1.Text += "\r\nCPU Model: " + CPUModel().ToString();
            textBox1.Text += "\r\nCPU Stepping: " + CPUStepping().ToString();
        }
    }
}

 

If you compare the CPU identification returned by my CPU.DLL with that returned by Intel Processor Identification Utility, you will see its Model ID already combined with Extended Model ID, but mine is not.

CPUInfo.png.adc40700fda332914add6a513861a45f.png

If you do not want to compile the Assembly source code yourself, you can download the CPU.zip below (sign in to download) and use the CPU.DLL directly.

Enjoy!

CPU.zip

Edited by FlierMate
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...