How can I call "cpuid" in Linux?

While writing new code for Windows, I came across _cpuinfo() from the Windows API. Since I mainly deal with the Linux environment (GCC), I want to have access to CPUInfo.

I tried the following:

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

This is the use of assembly, but I do not want to reinvent the wheel. Is there any other way to implement CPUInfo without assembly?

Compiler Errors:

 lewis@lewis-desktop:~/Desktop/prog$ g++ -Wall CPUInfo.cpp CPUInfo.cpp: In function 'int main()': CPUInfo.cpp:10:22: error: expected ')' before ';' token CPUInfo.cpp:10:23: error: expected primary-expression before ')' token CPUInfo.cpp:10:23: error: expected ';' before ')' token CPUInfo.cpp:8:8: warning: unused variable 'b' [-Wunused-variable] CPUInfo.cpp:12:8: error: expected '}' at end of input 
+5
c ++ assembly cpu
Jan 10 '13 at
source share
2 answers

Since you are compiling GCC, you can include cpuid.h , which declares these functions:

 /* Return highest supported input value for cpuid instruction. ext can be either 0x0 or 0x8000000 to return highest supported value for basic or extended cpuid information. Function returns 0 if cpuid is not supported or whatever cpuid returns in eax register. If sig pointer is non-null, then first four bytes of the signature (as found in ebx register) are returned in location pointed by sig. */ unsigned int __get_cpuid_max (unsigned int __ext, unsigned int *__sig) /* Return cpuid data for requested cpuid level, as found in returned eax, ebx, ecx and edx registers. The function checks if cpuid is supported and returns 1 for valid cpuid information or 0 for unsupported cpuid level. All pointers are required to be non-null. */ int __get_cpuid (unsigned int __level, unsigned int *__eax, unsigned int *__ebx, unsigned int *__ecx, unsigned int *__edx) 

You do not need and should not re-implement this functionality.

+31
Jan 10 '13 at 20:44
source share
 for (a =0; a < 5; ++a;) 

There should only be two semicolons. You have three.

This is the basic syntax of C / C ++; CPUID is a red herring.

+7
Jan 10 '13 at 20:45
source share



All Articles