articles » current » assembly » x64 » check-for-cpuid-support

x64 Assembly: How to Check for CPUID Support

An example of how to check if a processor supports the CPUID instruction using x86-64 (64-bit) assembly

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 rax, rbx and 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

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE