2010 09 01 18 36 [程式] 檢查 CPU 是否支援 cpuid

 

參考資料:

Intel® Processor Identification and the CPUID Instruction

檢查 CPU 能否執行 CPUID?

 

#include <stdio.h>
#include <stdlib.h>

__declspec(naked) int IsCPUIDSupported()
{
__asm {
pushfd ;使延伸旗標推入堆疊
pop eax ;把堆疊中的延伸旗標彈出至 EAX
mov ecx,eax ;保存舊的延伸旗標
xor eax,200000h ;改變強制延伸旗標第 21 位元
push eax ;把改變後新的延伸旗標推入堆疊
popfd ;存入延伸旗標
pushfd ;再取出延伸旗標
pop eax
xor eax,ecx ;如果延伸旗標的第21位元為1,那麼
ret
}
}

int main(int argc, char *argv[])
{
if (IsCPUIDSupported() == 0) {
printf("cpuid is not supported\n");
} else {
printf("cpuid is supported\n");
}
return 0;
}