Page 1 of 1

Wiki: CPUID

Posted: Wed Jan 14, 2009 10:51 pm
by Hangin10
I've added a routine to the article that checks that the CPUID instruction is supported.

I am fairly confident that it is correct, but if someone could take a look and double check, I'd
appreciate it.

Re: Wiki: CPUID

Posted: Sat Jan 17, 2015 8:21 pm
by jackman
Hangin10 wrote:I've added a routine to the article that checks that the CPUID instruction is supported.

I am fairly confident that it is correct, but if someone could take a look and double check, I'd
appreciate it.
I believe this is the code in question:

Code: Select all

; returns 1 if CPUID is supported, 0 otherwise (ZF is also set accordingly)
pushfd ; get
pop eax
mov ecx, eax ; save 
xor eax, 0x200000 ; flip
push eax ; set
popfd
pushfd ; and test
pop eax
xor eax, ecx ; mask changed bits
shr eax, 21 ; move bit 21 to bit 0
and eax, 1 ; and mask others
push ecx
popfd ; restore original flags
ret
Forgive my ignorance. How is this called from C?

I don't see much reference to it in the forums or general googling, so I'm guessing that people just assume their CPU is CPUID capable.

Thank you.

Re: Wiki: CPUID

Posted: Sun Jan 18, 2015 2:28 am
by Combuster
How is this called from C?
This code follows the i386 system V ABI. That means you just give it a name and declare it as (extern "C") int isCPUIDsupported(); like you would for any function call in a different object file.
people just assume their CPU is CPUID capable.
More likely, they're not using it in the first place.
ZF is also set accordingly
Considering the last two instructions are POPFD; RET; I call all flags preserved instead. :wink:

Re: Wiki: CPUID

Posted: Sun Jan 18, 2015 4:55 am
by iansjack
Combuster wrote:
ZF is also set accordingly
Considering the last two instructions are POPFD; RET; I call all flags preserved instead. :wink:
But isn't the POPFD restoring the flags as pushed earlier in the routine (by PUSH ECX), not as originally set?

Re: Wiki: CPUID

Posted: Sun Jan 18, 2015 4:03 pm
by Combuster
Find the only assignment to ECX :wink:

Meanwhile, Brendan demonstrated a much shorter version.