x64 Assembly: How to Check for CPUID Support
This code was compiled and linked using the Microsoft Macro Assembler for Visual Studio 2022.
This example of checking for the CPUID instruction is done with
rflags — if the 22nd bit (bit 21) can be toggled then the cpu supports supports CPUID (all x64 processors support CPUID).
TITLE 'extern "C" long long isCPUID64();' _TEXT SEGMENT isCPUID64 PROC push rbx ; save rbx for the caller pushfq ; push rflags on the stack pop rax ; pop them into rax mov rbx, rax ; save to rbx for restoring afterwards xor rax, 200000h ; toggle bit 21 push rax ; push the toggled rflags popfq ; pop them back into rflags pushfq ; push rflags pop rax ; pop them back into rax cmp rax, rbx ; see if bit 21 was reset jz not_supported mov rax, 1 jmp exit not_supported: xor rax, rax; exit: pop rbx ret 0 isCPUID64 ENDP _TEXT ENDS END