logo
Kurashizu Blogwhere ideas flow
  • Home
  • News
  • Blog
  • About
  • Home
  • News
  • Blog
  • About

© 2026 Kurashizu. All rights reserved.

Admin·New Post·Service Status
Powered by Cloudflare·31.10.26
← Back to news

Why is the x86 undefined instruction called ud2? Why 2?

Original article
devblogs.microsoft.com|92 points|28 comments|by ibobev|Sep 13, 2026

The Mystery of the x86 ud2 Instruction: Why the "2"?

Microsoft

If you have ever spent time analyzing x86 assembly—perhaps while debugging a crash caused by an API detour or examining compiler output—you have likely encountered the instruction ud2.

What exactly is ud2?

In short, ud2 is an architecturally undefined instruction. Its sole purpose is to be invalid; it is guaranteed to trigger an "invalid opcode" exception the moment it is executed.

Compilers frequently utilize this instruction to mark sections of code as unreachable. This serves as a safety mechanism: if the program's execution flow somehow enters a region it should never reach, it is far better to crash immediately via ud2 than to continue executing random bytes as if they were valid instructions.

Example Scenario: Imagine a function explicitly marked with the [[noreturn]] attribute. If that function somehow returns anyway, the compiler will insert a ud2 immediately following the call.

// Conceptual example
void fatal_error() [[noreturn]] {
    // ... logic that should never return ...
}

void main() {
    fatal_error();
    // Compiler inserts `ud2` here to prevent falling through
}

The History: A Tale of Two Opcodes

Originally, the x86 architecture did not provide a dedicated, official "undefined" instruction. Developers who wanted to force an invalid opcode exception had to go hunting for byte sequences that happened to cause one.

Eventually, two primary "schools of thought" emerged based on discovered byte sequences:

  1. The 0F FF Camp: Some discovered that the sequence 0F FF reliably triggered the exception.
  2. The 0F B9 Camp: Others found that 0F B9 achieved the same result.

Interestingly, both of these sequences were internally decoded by the CPU as if they required parameters (specifically, a destination register and a source register/memory location). While these parameters were never actually used—because the exception happened first—the CPU still expected them to be there.


The Arrival of Hyrum's Law

For a while, these two methods coexisted peacefully. However, as Intel developed newer processors, things changed. Perhaps 0F FF was assigned to a new instruction, or perhaps it stopped triggering an exception and started performing a random operation.

When software relying on 0F FF was run on these new chips, it broke. Intel discovered that developers had built dependencies on a behavior that was never officially supported. This is a classic example of Hyrum's Law:

"With a sufficient number of users, all observable behaviors will be depended upon by somebody."

To resolve this, Intel decided to formalize the process. They created a permanently, officially supported invalid instruction and named it ud2.

Why the number 2?

The naming convention was retroactive. To keep the history straight, Intel labeled the previous "accidental" undefined instructions:

InstructionOpcodeStatusNote
ud00F FFRetroactiveRequired unused parameters
ud10F B9RetroactiveRequired unused parameters
ud20F 0BOfficialNo parameters required

The primary advantage of ud2 is that it is a clean, two-byte instruction. It doesn't require the CPU to decode any trailing source or destination operands.


The Technical Nuance: Page Boundaries

You might wonder: Why does it even matter if ud0 and ud1 had unused parameters?

It matters because of how the CPU fetches and decodes instructions. Even if the processor doesn't use the parameters, it still decodes them. This leads to a potential edge case involving memory pages:

  • The Scenario: Suppose the 0F FF (ud0) opcode is located at the very end of a memory page.
  • The Problem: The CPU attempts to decode the required parameters, but those bytes reside on the next page.
  • The Result: If that next page is not present in memory, the CPU will trigger an access violation (page fault) instead of the intended invalid opcode exception.

Some older processors avoided this by crashing as soon as they saw 0F FF, but in general, the lack of parameters makes ud2 significantly more robust.

mind blown

Summary Checklist: Why ud2 is Superior

  • Officially supported by Intel.
  • Guaranteed to raise an invalid opcode exception.
  • No parameters required →\rightarrow→ No risk of page-boundary access violations.
  • Clear intent for compiler-generated unreachable code.

Bonus: Community Discussion

The article sparked a conversation about the relevance of assembly language in the modern era.

John Musbach suggested that with the quality of modern compilers, assembly knowledge is becoming obsolete for most developers, questioning how many "Gen Z" programmers even know it.

Csaba Varga countered this, arguing that while you might not need to write it daily, understanding the basics is still essential for anyone writing performance-critical code, regardless of how "amazing" the compilers are.

Raymond Chen


About the Author: Raymond Chen has spent over three decades contributing to Windows. Since 2003, he has authored The Old New Thing, a blog and subsequent book that explores the quirks of Windows internals. He occasionally shares stories on Twitter that, in his own words, "convey no useful information."