Skip to content

CPUID

CPUID opcode는 x86 아키텍처를 위한 프로세서 기계 명령어이다. (CPUID는 CPU IDentification에서 비롯한다.) 인텔이 펜티엄과 SL 강화 486 프로세서를 내세운 1993년에 도입하였다.

CPUID opcode를 사용하여 소프트웨어는 프로세서 종류와 MMX/SSE와 같은 기능들을 결정할 수 있다. CPUID opcode는 0FA2h이며 EAX 레지스터 값은 어떠한 정보를 반환할지를 결정한다.

CPUID 명령을 일반적으로 사용하기까지 프로그래머들은 프로세서 메이커와 모델을 결정하기 위하여 CPU 동작에 사소한 차이를 불러일으킬 수 있는 비밀스러운 기계어를 기록하여야 했다.

Obtain processor manufacturer using CPUID

I was looking for a piece of code to simply obtain the manufacturer of the CPU in your computer (might also be known under vendor ID). I found it difficult to obtain a code as basic as possible. I had already coded this once in Assembly language, still I could not manage very well. Anyway... I finally worked it out (I began with c++ 2 days ago..) and thought why not post my result for others, maybe it is helpful since it is kept quiet basic.

This code executes a 2 byte small function called CPUID with parameter 0 (stored in EAX). The function then returns it values to EBX, ECX and EDX memory registers. With al and ah I extract the characters of the values stored in these memory registers. You can only access the last two bytes of a DWORD (by calling al and ah you get last 2 bytes of EAX separately). And each byte represents one character. So to access the other 2 bytes in the DWORD I have used a bitwise operation to shift out bits to the right, SHR. SHR has two parameters, its memory register the shift should be operated on and the second parameter represents how many bits it should shift, two shift out 2 bytes you need to shift 16 bits (1 byte is 8 bit of course). Finally each byte is moved (MOV array, character) to the char array which finally is added up to a string.

// a.cpp : Defines the entry point for the console application.
// The code is written in Visual Studio 2005.

#include <stdafx.h>
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

//Creating a function to obtain the CPUID
string GetCpuID()
{
    //Initialize used variables
    char SysType[13]; //Array consisting of 13 single bytes/characters
    string CpuID; //The string that will be used to add all the characters to
    //Starting coding in assembly language
    _asm
    {
        //Execute CPUID with EAX = 0 to get the CPU producer
        XOR EAX, EAX
        CPUID

        //MOV EBX to EAX and get the characters one by one by using shift out right bitwise operation.
        MOV EAX, EBX
        MOV SysType[0], al
        MOV SysType[1], ah
        SHR EAX, 16
        MOV SysType[2], al
        MOV SysType[3], ah

        //Get the second part the same way but these values are stored in EDX
        MOV EAX, EDX
        MOV SysType[4], al
        MOV SysType[5], ah
        SHR EAX, 16
        MOV SysType[6], al
        MOV SysType[7], ah

        //Get the third part
        MOV EAX, ECX
        MOV SysType[8], al
        MOV SysType[9], ah
        SHR EAX, 16
        MOV SysType[10], al
        MOV SysType[11], ah
        MOV SysType[12], 00
    }
    CpuID.assign(SysType,12);
    return CpuID;
}

int _tmain(int argc, _TCHAR* argv[])
{
    string CpuID;
    CpuID = GetCpuID();
    cout << CpuID;
    return 0;
}

See also

Favorite site

Programming

References


  1. Wikipedia_-ko-_CPUID.pdf 

  2. How_to_identify_the_operating_system_and_the_processor_does_the_CPUID.pdf 

  3. Getting_the_machine_serial_number_and_CPU_ID_using_C_in_Linux_-_Stack_Overflow.pdf 

  4. CPUID_for_x64_Platforms_and_Microsoft_Visual_Studio.pdf