CPUID - Accessing The Id From Other Languages

Accessing The Id From Other Languages

This information is easy to access from other languages as well. For instance, the C++ code for gcc below prints the first five values, returned by the cpuid:

#include int main { int a, b; for (a = 0; a < 5; a++) { __asm ( "mov %1, %%eax; " // a into eax "cpuid;" "mov %%eax, %0;" // eax into b :"=r"(b) // output :"r"(a) // input :"%eax","%ebx","%ecx","%edx" // clobbered register ); std::cout << "The code " << a << " gives " << b << std::endl; } return 0; }

In C, the code may be shortened to:

int main { int a, b; for (a = 0; a < 5; a++) { __asm( "cpuid" : "=a" (b) // EAX into b (output) : "a" (a) // a into EAX (input) :"%ebx","%ecx","%edx"); // cpuid always clobbers these printf("The code %i gives %i\n", a, b); } return 0; }

Or, a generally useful C implementation that works on 32 and 64 bit setups:

#include void cpuid(unsigned info, unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *edx) { *eax = info; __asm volatile ("mov %%ebx, %%edi;" /* 32bit PIC: don't clobber ebx */ "cpuid;" "mov %%ebx, %%esi;" "mov %%edi, %%ebx;" :"+a" (*eax), "=S" (*ebx), "=c" (*ecx), "=d" (*edx) : :"edi"); } int main { unsigned int eax, ebx, ecx, edx; int i; for (i = 0; i < 6; ++i) { cpuid(i, &eax, &ebx, &ecx, &edx); printf("eax=%i: %#010x %#010x %#010x %#010x\n", i, eax, ebx, ecx, edx); } return 0; }

Microsoft Visual C compiler has builtin function __cpuid so cpuid instruction may be embedded without using inline assembly. This is handy since x64 version of MSVC doesn't allow inline assembly at all. The same program for MSVC would be:

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

For Borland/Embarcadero C compilers (bcc32), native asm function calls are necessary, as there is no asm implementation. The pseudo code:

unsigned int a, b, c, d; unsigned int InfoType = 0; __asm xor EBX, EBX; __asm xor ECX, ECX; __asm xor EDX, EDX; __asm mov EAX, InfoType; __asm cpuid; __asm mov a, EAX; __asm mov b, EBX; __asm mov c, ECX; __asm mov d, EDX;

Read more about this topic:  CPUID

Famous quotes containing the word languages:

    I am always sorry when any language is lost, because languages are the pedigree of nations.
    Samuel Johnson (1709–1784)