Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPUID in x86 Architecture

I am trying to access the Information using CPUID in C++. I have produced this code so far and could not go any longer. I found some useful articles here and on the web but they did not seem to help me.

I am supposed to use instructions and registers from x88 only. This is the code.

int b[5] = {0} ;

for (int a = 0; a < 5 ; a++)
{
    __cpuid (b,a) ;
    std::cout << "The code " << a << " gives " << b[0] << std::endl;
}

I am unable to go any further as I cannot understand how to fetch the information from this array bitwise. I have this wiki and msdn article which explains the scheme. My question is not very good but I would appreciate any help or direction in this regard.

like image 877
Sasha Avatar asked Dec 04 '25 16:12

Sasha


1 Answers

I suppose your problem is not about accessing the array data through an index, which is as you already done b[0], b[1] and so on. You need a little bit of bit (!) manipulation. You have to mask out the bits you are not interested in and interpret what is left; e.g.

    (b[0] & 0xF0) >> 4

will give you 4 bits (4-7) that are the model according to MSDN. And so on. Consider the following

    3            2            1
    1098 7654 3210 9876 5432 1098 7654 3210
    0000 0000 0000 0000 0000 0000 0000 0000 
    \R_/ \ext_Fam/ \eM/ RRpp \fm/ \md/ \sd/

    R = reserved
    ext_Fam = extended family
    eM = extended model
    pp = processor type
    fm = family
    md = model
    sd = stepping id

(int supposed 32 bit int — or wider)

If you want e.g. the processor type, you need:

    0000 0000 0000 0000 0011 0000 0000 0000 
    \R_/ \ext_Fam/ \eM/ RRpp \fm/ \md/ \sd/

and then shift (logical shift) right of 3 "nibbles" (12). So

   (b[0] >> 12) & 3

will give you a number representing processor type. (The number 3 is 11 in base 2, so it's the right mask to select only the rightmost two bits).

like image 72
ShinTakezou Avatar answered Dec 07 '25 15:12

ShinTakezou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!