要在Linux上通過程序獲取CPUID,您可以使用內聯匯編和C語言編寫一個簡單的程序
#include <stdio.h>
#include <stdint.h>
uint32_t get_cpuid(uint32_t eax) {
uint32_t ebx, ecx, edx;
__asm__ __volatile__(
"cpuid"
: "=b"(ebx), "=c"(ecx), "=d"(edx)
: "a"(eax)
: );
return ebx;
}
int main() {
uint32_t eax = 1; // 選擇CPUID功能,ECX = 0表示獲取供應商字符串
uint32_t ebx = get_cpuid(eax);
printf("Vendor ID: %x\n", ebx);
printf("Version: %x\n", eax & 0xFF);
printf("Signature: %x%x%x\n", (eax >> 24) & 0xFF, (eax >> 16) & 0xFF, eax & 0xFF);
return 0;
}
要編譯此程序,請使用以下命令:
gcc cpuid.c -o cpuid
然后運行生成的可執行文件:
./cpuinfo
這將輸出CPU的供應商ID、版本號和簽名。請注意,此示例僅適用于x86和x86_64架構。如果您需要在其他架構上執行CPUID操作,您可能需要使用不同的內聯匯編語法。