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


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.