121

In x86 assembly code, are JE and JNE exactly the same as JZ and JNZ?

Daniel Hanrahan
  • 4,801
  • 7
  • 31
  • 44
  • 76
    Long answer: yes. – Hans Passant Jan 10 '13 at 20:58
  • 14
    Short answer: no. (But they correspond to *exactly* the same machine code, so they do *exactly* the same thing. They just have different mnemonics for the same comparison.) –  Jan 10 '13 at 21:05
  • 4
    JE means jump if equal, it is equal if a prior compare has the z flag set, JZ means jump if the z flag is set. They are exactly the same, some folks want to think/write in terms of my compare was equal or not equal. Some folks think and write in terms of is the z flag set or z flag clear. – old_timer Jan 10 '13 at 22:41

3 Answers3

168

JE and JZ are just different names for exactly the same thing: a conditional jump when ZF (the "zero" flag) is equal to 1.

(Similarly, JNE and JNZ are just different names for a conditional jump when ZF is equal to 0.)

You could use them interchangeably, but you should use them depending on what you are doing:

  • JZ/JNZ are more appropriate when you are explicitly testing for something being equal to zero:

    dec  ecx
    jz   counter_is_now_zero
    
  • JE and JNE are more appropriate after a CMP instruction:

    cmp  edx, 42
    je   the_answer_is_42
    

    (A CMP instruction performs a subtraction, and throws the value of the result away, while keeping the flags; which is why you get ZF=1 when the operands are equal and ZF=0 when they're not.)

Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119
  • 4
    TL:DR: same machine operation, different *semantic* meaning. Just like `jb` / `jc` / `jnae` all testing CF=1. See https://www.felixcloutier.com/x86/jcc (or cmovcc or setcc) – Peter Cordes Jul 05 '19 at 07:01
49

From the Intel's manual - Instruction Set Reference, the JE and JZ have the same opcode (74 for rel8 / 0F 84 for rel 16/32) also JNE and JNZ (75 for rel8 / 0F 85 for rel 16/32) share opcodes.

JE and JZ they both check for the ZF (or zero flag), although the manual differs slightly in the descriptions of the first JE rel8 and JZ rel8 ZF usage, but basically they are the same.

Here is an extract from the manual's pages 464, 465 and 467.

 Op Code    | mnemonic  | Description
 -----------|-----------|-----------------------------------------------  
 74 cb      | JE rel8   | Jump short if equal (ZF=1).
 74 cb      | JZ rel8   | Jump short if zero (ZF ← 1).

 0F 84 cw   | JE rel16  | Jump near if equal (ZF=1). Not supported in 64-bit mode.
 0F 84 cw   | JZ rel16  | Jump near if 0 (ZF=1). Not supported in 64-bit mode.

 0F 84 cd   | JE rel32  | Jump near if equal (ZF=1).
 0F 84 cd   | JZ rel32  | Jump near if 0 (ZF=1).

 75 cb      | JNE rel8  | Jump short if not equal (ZF=0).
 75 cb      | JNZ rel8  | Jump short if not zero (ZF=0).

 0F 85 cd   | JNE rel32 | Jump near if not equal (ZF=0).
 0F 85 cd   | JNZ rel32 | Jump near if not zero (ZF=0).
Andrei Damian-Fekete
  • 1,820
  • 21
  • 27
higuaro
  • 15,730
  • 4
  • 36
  • 43
-11
  je : Jump if equal:

  399  3fb:   64 48 33 0c 25 28 00    xor    %fs:0x28,%rcx
  400  402:   00 00
  401  404:   74 05                   je     40b <sims_get_counter+0x51>
leesagacious
  • 182
  • 1
  • 8