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
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With