CPUID: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
m Reverted edits by Markhobley (Talk); changed back to last version by Steve23
→‎How to use CPUID: simplify asm, and use ecx rather than ebx because cdecl requires us to preserve ebx
Line 4: Line 4:


Note that prior to use the CPUID instruction, you should also make sure the processor supports it by testing the 'ID' bit (0x200000) in eflags (modifiable only when the CPUID instruction is supported. For systems that don't support CPUID, writing a '1' at that place will have no effect).
Note that prior to use the CPUID instruction, you should also make sure the processor supports it by testing the 'ID' bit (0x200000) in eflags (modifiable only when the CPUID instruction is supported. For systems that don't support CPUID, writing a '1' at that place will have no effect).
An Assembly-routine that checks whether CPUID is supported looks like the following:
Here is an assembly routine that checks if CPUID is supported:


<source lang="asm">
<source lang="asm">
; returns zero if CPUID is not supported. returns 1 otherwise.
; returns 1 if CPUID is supported, 0 otherwise (ZF is also set accordingly)
pushfd
pushfd ; get
pop eax
pop eax
mov ebx, eax
mov ecx, eax ; save
xor eax, 0x200000
xor eax, 0x200000 ; flip
push eax ; set
and ebx, 0x200000
push eax
popfd
popfd
pushfd
pushfd ; and test
pop eax
pop eax
xor eax, ecx ; mask changed bits
and eax, 0x200000
shr eax, 21 ; move bit 21 to bit 0
xor eax, ebx
shr eax, 21
and eax, 1 ; and mask others
ret
ret
</source>
</source>