CPUID Instruction x86

The CPUID instruction is a processor supplementary instruction for x86 architecture CPUs, providing detailed information about the processor. This includes details about the CPU's capabilities, features, and manufacturer, which is essential for system-level programming and optimizing applications.

1 What is CPUID?

CPUID is a privileged x86 instruction that stands for "CPU Identification." Its primary purpose is to provide detailed information about the CPU it's executed on. Introduced with the Intel 486 series and later adopted by other x86 processors, CPUID has become indispensable for system-level programming, software optimization, and CPU feature detection.

2 Using CPUID Instruction

The CPUID instruction is accessed using the opcode 0x0F 0xA2. It takes an input value in the EAX register and optionally in the ECX register to specify which piece of information to retrieve, and it outputs the results in EAX, EBX, ECX, and EDX.

Basic Syntax:

The syntax of the CPUID instruction in assembly language is straightforward:

mov eax, function_id
cpuid

Here, EAX is loaded with a function ID to specify the information required. Upon execution, the CPU stores the requested information in the EAX, EBX, ECX, and EDX registers.

3 Function IDs and Their Outputs

CPUID supports various function IDs, each providing specific information about the processor. Some common function IDs and their outputs include:

3.1 Function 0 (EAX=0): Get highest function parameter and manufacturer ID.

  • EAX: Highest function supported.
  • EBX, EDX, ECX: CPU vendor string (12 ASCII characters).

3.2 Function 1 (EAX=1): Get processor info and feature bits.

  • EAX: Processor version information (type, family, model, stepping).
  • EBX: Additional information (e.g., brand index, CLFLUSH cache line size).
  • ECX: Feature flags (SSE3, PCLMULQDQ, DTES64, etc.).
  • EDX: Feature flags (FPU, VME, DE, PSE, TSC, etc.).

3.3 Function 0x80000000 (EAX=0x80000000): Get highest extended function supported.

  • EAX: Highest extended function supported.

3.4 Function 0x80000001 (EAX=0x80000001): Get extended processor info and feature bits, including 64-bit mode support.

  • EAX, EBX, ECX, EDX: Extended feature flags and processor capabilities.

4 Example Usage

Let's consider an example where we check for 64-bit mode support:

mov     eax, 0x80000001      ; Check 64-bit mode support
cpuid
test    edx, (1 << 29)       ; Check if bit 29 of EDX is set

Here, we use function ID 0x80000001 to retrieve extended feature flags, and then we test bit 29 of EDX to determine 64-bit mode support.

5 Importance in System Programming

  • Feature Detection:CPUID facilitates runtime feature detection, allowing software to adapt its behavior based on the underlying hardware capabilities.
  • System Information: It provides critical information about the processor, which is essential for system-level programming tasks, such as optimizing code paths for specific CPU architectures.