Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly - JZ instruction after CMP

Tags:

assembly

I have the next instruction:

cmp al, 1
jz mub

When al is 2 (10 in binary). What would do this instruction? As I know, I can use JE,JNE,JA etc., but what is meaning jz after cmp instruction?

Thanks

like image 821
Adam Sh Avatar asked Feb 18 '12 15:02

Adam Sh


People also ask

What does JZ do in assembly?

The JZ instruction transfers control to the specified address if the value in the accumulator is 0. Otherwise, the next instruction is executed. Neither the accumulator nor any flags are modified by this instruction.

What does cmp command do in assembly?

The CMP instruction compares two operands. It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not.

What does Jnz mean in assembly?

JNZ is short for "Jump if not zero (ZF = 0)", and NOT "Jump if the ZF is set". If it's any easier to remember, consider that JNZ and JNE (jump if not equal) are equivalent.

Which flags are affected by cmp instruction?

After operation between operands, result is always stored in first operand. CMP and TEST instructions affect flags only and do not store a result (these instruction are used to make decisions during program execution). These instructions affect these flags only: CF, ZF, SF, OF, PF, AF.


2 Answers

jz is "jump if zero". cmp subtracts its two operands, and sets flags accordingly. (See here for reference.)

If the two operands are equal, the subtraction will result in zero and the ZF flag will be set.

So in your sample, the jump will be taken if al was 1, not taken otherwise.

like image 79
Mat Avatar answered Sep 29 '22 06:09

Mat


jz means jump if zero. In this context, it will only jump if al was 1.

That's because cmp is usually equivalent to sub (subtract) but without actually changing the value.

cmp al, 1 will set the processor flags (including the zero flag) based on what would have happened if you'd subtracted 1 from al.

If al is 2, the jump will not be taken (because the zero flag has not been set) and code will continue to execute at the instruction following the jz.

As an aside, jz is often the same opcode as je since they effectively mean the same thing. See for example the Wikipedia page on x86 control flow:

Jump on Zero
jz loc
Loads EIP with the specified address, if the zero bit is set from a previous arithmetic expression. jz is identical to je.

like image 45
paxdiablo Avatar answered Sep 29 '22 04:09

paxdiablo