Adam Sh
Adam Sh

Reputation: 8577

Assembly - JZ instruction after CMP

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

Upvotes: 19

Views: 99871

Answers (3)

paxdiablo
paxdiablo

Reputation: 882776

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.

Upvotes: 5

Martin James
Martin James

Reputation: 24907

'Jump Zero' - jump to label 'mub' if the zero flag is set. 'cmp' is a subtract that only sets flags & so, if al is 2, (2-1)<>0 so the zero flag is clear and the jump will not be performed.

Upvotes: 1

Mat
Mat

Reputation: 206929

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.

Upvotes: 30

Related Questions