Skip to content

Instantly share code, notes, and snippets.

@wen-long
Created June 23, 2017 02:17
Show Gist options
  • Save wen-long/8f33f42d64d8e53b9c7bc474eaf097f4 to your computer and use it in GitHub Desktop.
Save wen-long/8f33f42d64d8e53b9c7bc474eaf097f4 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 = 0x40000000; /* processor serial number */
native_cpuid(&eax, &ebx, &ecx, &edx);
char buf[13] = {0};
*(unsigned *)(&buf[0]) = ebx;
*(unsigned *)(&buf[4]) = ecx;
*(unsigned *)(&buf[8]) = edx;
printf("serial number %s\n", buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment