Skip to content

Instantly share code, notes, and snippets.

@tmiz
Created December 9, 2010 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmiz/734651 to your computer and use it in GitHub Desktop.
Save tmiz/734651 to your computer and use it in GitHub Desktop.
Get x86 CPU Vender and Brand Name
//!(C)tmiz.net
#include <stdio.h>
#define X86_VENDERNAME_ARRAY_LENGTH 13
#define X86_BRANDNAME_ARRAY_LENGTH 49
extern void GetX86CPUVenderName(char*outVendername);
extern void GetX86CPUBrandName(char*brandname);
int main() {
char vender[X86_VENDERNAME_ARRAY_LENGTH];
GetX86CPUVenderName(vender);
printf("%s\n", vender);
char brand[X86_BRANDNAME_ARRAY_LENGTH];
GetX86CPUBrandName(brand);
printf("%s\n", brand);
return 0;
}
union CpuRegs {
struct cpu_regs {
unsigned int eax, ebx, ecx, edx;
} e;
struct cpu_regs_n {
unsigned int val[4];
} n;
};
void x86CPUIDOperate(unsigned int op, CpuRegs *outCpuRegs)
{
__asm__ volatile(
"xchgl %%ebx, %1 \n\t"
"cpuid \n\t"
"xchgl %%ebx, %1 \n\t"
: "=a"(outCpuRegs->e.eax),
"=b"(outCpuRegs->e.ebx),
"=c"(outCpuRegs->e.ecx),
"=d"(outCpuRegs->e.edx)
: "a"(op));
}
void int2Ascii(int in, char* out){
if(!out) return;
//x86なのでLittle Endian
for(int i= 0; i< 4; i++) {
out[i] = (in >> (i*8)) & 0x000000ff;
}
}
void GetX86CPUVenderName(char* vendername)
{
//vendername must be 13 chars
CpuRegs inCpuRegs;
x86CPUIDOperate(0, &inCpuRegs);
//building strings by order of ebx,edx,ecx.
int2Ascii(inCpuRegs.e.ebx, &vendername[0]);
int2Ascii(inCpuRegs.e.edx, &vendername[4]);
int2Ascii(inCpuRegs.e.ecx, &vendername[8]);
vendername[13] = 0;//insert NULL char as a last word.
}
void GetX86CPUBrandName(char*brandname)
{
//brandname must be 49 chars
int i;
CpuRegs result_regs[3];
for(i= 0; i< 3; i++) {
x86CPUIDOperate(0x80000002+ i, &(result_regs[i]));
}
for(i= 0; i< 12; i++) {
int2Ascii(result_regs[i/4].n.val[i%4], &brandname[i*4]);
}
brandname[49] = 0;//insert NULL char as a last word.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment