Intel x86 Assembly Language & Microarchitecture Optimization Test a register for 0

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Background

To find out if a register holds a zero, the naïve technique is to do this:

    cmp   eax, 0

But if you look at the opcode for this, you get this:

83 F8 00      cmp   eax, 0

Use test

    test   eax, eax      ; Equal to zero?

Examine the opcode you get:

85 c0         test   eax, eax

Pros

  • Only two bytes!

Cons

  • Opaque to a reader unfamiliar with the technique

You can also have a look into the Q&A Question on this technique.



Got any Intel x86 Assembly Language & Microarchitecture Question?