Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JE/JNE and JZ/JNZ

Tags:

x86

assembly

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

like image 499
Daniel Hanrahan Avatar asked Jan 10 '13 20:01

Daniel Hanrahan


2 Answers

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.)

like image 105
Matthew Slattery Avatar answered Sep 23 '22 01:09

Matthew Slattery


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). 
like image 25
higuaro Avatar answered Sep 26 '22 01:09

higuaro