#include <iostream>
using namespace std;
#define BYTE unsigned char
#define UINT unsigned int
int main(int argc, char* argv[])
{
BYTE szCPU[16] = { 0 }; //定义存放CPU类型的数组
UINT uCPUID = 0U; //定义存放CPU ID的数组
_asm //开始执行汇编
{
mov eax, 0 //获取CPU型号
cpuid
mov dword ptr szCPU[0], ebx
mov dword ptr szCPU[4], edx
mov dword ptr szCPU[8], ecx
mov eax, 1
cpuid
mov uCPUID, edx
}
cout << szCPU << endl;
cout << uCPUID << endl;
getchar();
return 0;
}
执行结果:
GenuineIntel
3219913727
CPU检测程序
#include <stdio.h>
void main()
{
unsigned long DBaseIndex, DFeInfo, DFeInfo2, DCPUBaseInfo;
unsigned long DFeIndex, DCPUExInfo, i;
unsigned long DOther[4], DTLB[4], DProceSN[2];
char cCom[13];
char cProStr[49];
unsigned int j;
_asm
{
xor eax, eax
cpuid
mov DBaseIndex, eax
mov dword ptr cCom, ebx
mov dword ptr cCom + 4, ecx //AMD CPU要把ecx改为edx
mov dword ptr cCom + 8, edx //AMD CPU要把edx改为ecx
mov eax, 1
cpuid
mov DCPUBaseInfo, eax
mov DFeInfo, ebx
mov DFeInfo2, edx
mov eax, 0x80000000
cpuid
mov DFeIndex, eax
mov eax, 0x80000001
cpuid
mov DCPUExInfo, eax
mov eax, 0x80000002
cpuid
mov dword ptr cProStr, eax
mov dword ptr cProStr + 4, ebx
mov dword ptr cProStr + 8, ecx
mov dword ptr cProStr + 12, edx
mov eax, 0x80000003
cpuid
mov dword ptr cProStr + 16, eax
mov dword ptr cProStr + 20, ebx
mov dword ptr cProStr + 24, ecx
mov dword ptr cProStr + 28, edx
mov eax, 0x80000004
cpuid
mov dword ptr cProStr + 32, eax
mov dword ptr cProStr + 36, ebx
mov dword ptr cProStr + 40, ecx
mov dword ptr cProStr + 44, edx
}
if (DBaseIndex >= 2)
{
_asm
{
mov eax, 2
cpuid
mov DTLB[0], eax
mov DTLB[2], ebx
mov DTLB[3], ecx
mov DTLB[4], edx
}
}
if (DBaseIndex == 3)
{
_asm
{
mov eax, 3
cpuid
mov DProceSN[0], ecx
mov DProceSN[1], edx
}
}
cCom[12] = '/0'; //加一个结尾符
printf("CPU厂商: %s\n", cCom);
printf("CPU字串: %s\n", cProStr);
printf("CPU基本参数: Family:%X Model:%X Stepping ID:%X\n", (DCPUBaseInfo & 0x0F00) >> 8,
(DCPUBaseInfo & 0xF0) >> 4, DCPUBaseInfo & 0xF);
printf("CPU扩展参数: Family:%X Model:%X Stepping ID:%X\n", (DCPUExInfo & 0x0F00) >> 8,
(DCPUExInfo & 0xF0) >> 4, DCPUExInfo & 0xF);
printf("CPU字串索引: 0x%X\n", DFeInfo & 0xFF);
printf("CLFLUSH线大小: 0x%X\n", (DFeInfo & 0xFF00) >> 8);
printf("处理器APIC物理标号:0x%X\n", (DFeInfo & 0xF000) >> 24);
if (DBaseIndex >= 2)
{
printf("CPU Cache & TLB Information: ");
for (j = 0; j < 4; j++)
{
if (!(DTLB[j] & 0xFF)) printf("%.2X ", DTLB[j] & 0xFF);
if (!((DTLB[j] & 0xFF) >> 8)) printf("%.2X ", (DTLB[j] & 0xFF00) >> 8);
if (!((DTLB[j] & 0xFF0000) >> 16)) printf("%.2X ", (DTLB[j] & 0xFF0000) >> 16);
if (!((DTLB[j] & 0xFF000000) >> 24)) printf("%.2X ", (DTLB[j] & 0xFF000000) >> 24);
}
printf("\n");
}
if (DBaseIndex == 3)
{
printf("CPU序列号是:%X%X\n", DProceSN[0], DProceSN[1]);
}
printf("FPU: %d\t\t", DFeInfo2 & 0x00000001); //下面是调用某BLOG上面的代码,懒得写了 ^^
printf("VME: %d\t\t", (DFeInfo2 & 0x00000002) >> 1);
printf("DE: %d\n", (DFeInfo2 & 0x00000004) >> 2);
printf("PSE: %d\t\t", (DFeInfo2 & 0x00000008) >> 3);
printf("TSC: %d\t\t", (DFeInfo2 & 0x00000010) >> 4);
printf("MSR: %d\n", (DFeInfo2 & 0x00000020) >> 5);
printf("PAE: %d\t\t", (DFeInfo2 & 0x00000040) >> 6);
printf("MCE: %d\t\t", (DFeInfo2 & 0x00000080) >> 7);
printf("CX8: %d\n", (DFeInfo2 & 0x00000100) >> 8);
printf("APIC: %d\t", (DFeInfo2 & 0x00000200) >> 9);
printf("SEP: %d\t\t", (DFeInfo2 & 0x00000800) >> 11);
printf("MTRR: %d\n", (DFeInfo2 & 0x00001000) >> 12);
printf("PGE: %d\t\t", (DFeInfo2 & 0x00002000) >> 13);
printf("MCA: %d\t\t", (DFeInfo2 & 0x00004000) >> 14);
printf("CMOV: %d\n", (DFeInfo2 & 0x00008000) >> 15);
printf("PAT: %d\t\t", (DFeInfo2 & 0x00010000) >> 16);
printf("PSE-36: %d\t", (DFeInfo2 & 0x00020000) >> 17);
printf("PSN: %d\n", (DFeInfo2 & 0x00040000) >> 18);
printf("CLFSN: %d\t", (DFeInfo2 & 0x00080000) >> 19);
printf("DS: %d\t\t", (DFeInfo2 & 0x00200000) >> 21);
printf("ACPI: %d\n", (DFeInfo2 & 0x00400000) >> 22);
printf("MMX: %d\t\t", (DFeInfo2 & 0x00800000) >> 23);
printf("FXSR: %d\t", (DFeInfo2 & 0x01000000) >> 24);
printf("SSE: %d\n", (DFeInfo2 & 0x02000000) >> 25);
printf("SSE2: %d\t", (DFeInfo2 & 0x04000000) >> 26);
printf("SS: %d\t\t", (DFeInfo2 & 0x08000000) >> 27);
printf("TM: %d\n", (DFeInfo2 & 0x20000000) >> 29);
printf("\n其它信息:\n");
printf("----------------------------------------\n");
printf("In \t\tEAX \t\tEBX \t\tECX \t\tEDX");
for (i = 0x80000004; i <= DFeIndex; ++i)
{
DOther[0] = DOther[1] = DOther[2] = DOther[3] = 0;
_asm
{
mov eax, i
cpuid
mov DOther[0], eax
mov DOther[1], ebx
mov DOther[2], ecx
mov DOther[3], edx
}
printf("\n0x%.8X\t0x%.8X\t0x%.8X\t0x%.8X\t0x%.8X", i, DOther[0], DOther[1], DOther[2], DOther[3]);
}
printf("\n");
system("pause");
}
输出信息:
CPU厂商: GenuntelineI0烫烫烫烫烫烫烫烫烫烫烫烫烫?c
CPU字串: Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
CPU基本参数: Family:6 Model:5 Stepping ID:1
CPU扩展参数: Family:0 Model:0 Stepping ID:0
CPU字串索引: 0x0
CLFLUSH线大小: 0x8
处理器APIC物理标号:0x0
CPU Cache & TLB Information: 63 00 00 00 00 CC CC
FPU: 1 VME: 1 DE: 1
PSE: 1 TSC: 1 MSR: 1
PAE: 1 MCE: 1 CX8: 1
APIC: 1 SEP: 1 MTRR: 1
PGE: 1 MCA: 1 CMOV: 1
PAT: 1 PSE-36: 1 PSN: 0
CLFSN: 1 DS: 1 ACPI: 1
MMX: 1 FXSR: 1 SSE: 1
SSE2: 1 SS: 1 TM: 1
其它信息:
----------------------------------------
In EAX EBX ECX EDX
0x80000004 0x00003020 0x00000000 0x00000000 0x00000000
0x80000005 0x00000000 0x00000000 0x00000000 0x00000000
0x80000006 0x00400000 0x00000000 0x00000000 0x00000000
0x80000007 0x00000000 0x00000001 0x00000000 0x00000000
0x80000008 0x00000027 0x00000000 0x00000000 0x00000000
参考:汇编实现获取CPU信息