Skip to content

Instantly share code, notes, and snippets.

@wen-long
Created June 23, 2017 01:16
Show Gist options
  • Save wen-long/0e1664c614313575b44c42382e70121c to your computer and use it in GitHub Desktop.
Save wen-long/0e1664c614313575b44c42382e70121c to your computer and use it in GitHub Desktop.
static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
/* ecx is often an input as well as an output. */
asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx));
}
#include <stdio.h>
int main(int argc, char **argv)
{
unsigned eax, ebx = 0, ecx = 0, edx = 0;
// eax = 1; /* processor info and feature bits */
// native_cpuid(&eax, &ebx, &ecx, &edx);
// printf("stepping %d\n", eax & 0xF);
// printf("model %d\n", (eax >> 4) & 0xF);
// printf("family %d\n", (eax >> 8) & 0xF);
// printf("processor type %d\n", (eax >> 12) & 0x3);
// printf("extended model %d\n", (eax >> 16) & 0xF);
// printf("extended family %d\n", (eax >> 20) & 0xFF);
/* EDIT */
eax = 0x40000000; /* processor serial number */
native_cpuid(&eax, &ebx, &ecx, &edx);
/** see the CPUID Wikipedia article on which models return the serial
number in which registers. The example here is for
Pentium III */
char buf[13] = {0};
*(unsigned *)(&buf[0]) = ebx;
*(unsigned *)(&buf[4]) = edx;
*(unsigned *)(&buf[8]) = ecx;
//printf("serial number 0x%08x%08x\n", edx, ecx);
printf("serial number %s\n", buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment