1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <stdio.h>
struct cpuid_type {
unsigned int eax;
unsigned int ebx;
unsigned int ecx;
unsigned int edx;
};
typedef struct cpuid_type cpuid_t;
cpuid_t cpuid(unsigned int number)
{
cpuid_t result;
asm("movl %4, %%eax; cpuid; movl %%eax, %0; movl %%ebx, %1; movl %%ecx, %2; movl %%edx, %3;"
: "=m" (result.eax),
"=m" (result.ebx),
"=m" (result.ecx),
"=m" (result.edx) /* output */
: "r" (number) /* input */
: "eax", "ebx", "ecx", "edx" /* no changed registers except output registers */
);
return result;
}
int main (int argc, const char * argv[])
{
cpuid_t cpuid_registers;
unsigned int cpu_family, cpu_model, cpu_stepping;
cpuid_registers = cpuid(1);
cpu_family = 0xf & (cpuid_registers.eax>>8);
cpu_model = 0xf & (cpuid_registers.eax>>4);
cpu_stepping = 0xf & cpuid_registers.eax;
printf("CPUID (1): CPU is a %u86, Model %u, Stepping %u\n",
cpu_family, cpu_model, cpu_stepping);
return 0;
}
| |