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