Hi all,
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.

The code is written in Visual Studio 2005.

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.

Hopefully people found this code useful and hopefullyI made the code clear for one to understand.

Grtz Devoney

Source code:

// a.cpp : Defines the entry point for the console application.
//

#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;
}

Recommended Answers

All 4 Replies

Nice code -- works perfectly on my computer :)

An addition to my topic. This way you can also get the "Brand string" of your processor. Currently only tested on my Intel processor. The program also checks if this feature is supported by your CPU and will output an error message if it does not. Otherwise it shows you the output containing the Brand String. My brand string appears as "Intel (R) Core(Tm)2 CPU T5500 @ 1.66Ghz". You can create a new win32 console application and paste the code below.

Grtz Devoney

// BrandString.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <iostream>
#include <string>
#include <Windows.h>
#include <stdio.h>
#include <winbase.h>
#include <winuser.h>

using namespace std;

int _tmain()
{
	string BrandString;
	char Parts[48];
	long unsigned CheckP;
	char Answer;
	system("cls");
	cout << "Requesting Brand String...\n";

	_asm
	{
		mov EAX, 0x80000000
		CPUID
		mov CheckP, EAX
	}
	
	if(CheckP > 0x80000000)
	{
		cout << "Brand String is supported...\n";
	}
	else
	{
		cout << "Brand String NOT supported...\nProgram will terminate in 3 seconds." << CheckP;
		Sleep(3000);
		return 0;
	}

	_asm
	{
		//Get First Part of the Brand String
		mov EAX, 0x80000002
		CPUID
		//Get all byte stored in EAX
		mov Parts[0], al
		mov Parts[1], ah
		shr EAX, 16
		mov Parts[2], al
		mov Parts[3], ah
		//Get all byte stored in EBX
		mov Parts[4], bl
		mov Parts[5], bh
		shr EBX, 16
		mov Parts[6], bl
		mov Parts[7], bh
		//Get all byte stored in ECX
		mov Parts[8], cl
		mov Parts[9], ch
		shr ECX, 16
		mov Parts[10], cl
		mov Parts[11], ch
		//Get all byte stored in EDX
		mov Parts[12], dl
		mov Parts[13], dh
		shr EDX, 16
		mov Parts[14], dl
		mov Parts[15], dh

		//Get Second Part of the Brand String
		mov EAX, 0x80000003
		CPUID
		//Get all byte stored in EAX
		mov Parts[16], al
		mov Parts[17], ah
		shr EAX, 16
		mov Parts[18], al
		mov Parts[19], ah
		//Get all byte stored in EBX
		mov Parts[20], bl
		mov Parts[21], bh
		shr EBX, 16
		mov Parts[22], bl
		mov Parts[23], bh
		//Get all byte stored in ECX
		mov Parts[24], cl
		mov Parts[25], ch
		shr ECX, 16
		mov Parts[26], cl
		mov Parts[27], ch
		//Get all byte stored in EDX
		mov Parts[28], dl
		mov Parts[29], dh
		shr EDX, 16
		mov Parts[30], dl
		mov Parts[31], dh

		//Get Third Part of the Brand String
		mov EAX, 0x80000004
		CPUID
		//Get all byte stored in EAX
		mov Parts[32], al
		mov Parts[33], ah
		shr EAX, 16
		mov Parts[34], al
		mov Parts[35], ah
		//Get all byte stored in EBX
		mov Parts[36], bl
		mov Parts[37], bh
		shr EBX, 16
		mov Parts[38], bl
		mov Parts[39], bh
		//Get all byte stored in ECX
		mov Parts[40], cl
		mov Parts[41], ch
		shr ECX, 16
		mov Parts[42], cl
		mov Parts[43], ch
		//Get all byte stored in EDX
		mov Parts[44], dl
		mov Parts[45], dh
		shr EDX, 16
		mov Parts[46], dl
		mov Parts[47], dh
	}
	BrandString.append(Parts, 48);
	cout << "Output: " << BrandString << "\n";
	return 0;
}

How can I get ther processor's serial number o id?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.