Reputation: 157
I am learning assembly language at the moment and in the book assembly language by kip irvine he talks about conditional jump instructions and gives the following example
Example 2:
mov bx,1234h
sub bx,1234h
jne L5 ; jump not taken
je L1 ; jump is taken
Example 3:
mov cx,0FFFFh
inc cx
jcxz L2 ; jump is taken
Can someone please explain to me how " je L1" in example 2 works. Do you not need to use the cmp command somewhere. What specifically caused you to jump, as at that moment bx=0;
Also why was the jump taken in example 3. I thought that you are incrementing cx, however the jcxz will test if cx=0?
Any help would be greatly appreciated.
Upvotes: 2
Views: 4711
Reputation: 64904
No, you don't need to use the cmp
command anywhere. Many instructions affect the flags (or a subset of them).
sub
is particularly "good" for that purpose, because it affects the flags in exactly the same way a cmp
does (the only difference between sub
and cmp
is that sub
writes the result of the subtraction to the destination, whereas cmp
does not).
Remember that je
is really just a synonym of jz
, it doesn't really care about equality of any form, it just looks at the Z flag. 1234h
minus itself is obviously zero, so the Z flag should be set.
In the second example, jcxz
does branch if cx = 0
, which is the case. After incrementing cx
, it will be zero, because it was -1 before.
Upvotes: 3
Reputation: 71536
The "condition" is based on flags, typically a carry flag, zero flag, negative flag and overflow flag. but not all processor architectures do it exactly the same way, most use those four flags though.
Those flags come out of the "alu" and the instruction set defines what flag is or is not affected by each of the instructions. So you have to look at the documentation for each instruction, understand what flags are affected and how, and then understand if that helps you or not.
Subtract or compare (the difference usually is that sub modifies a result register or memory where cmp does everything the same as a subtract except it doesnt modify a destination) is quite useful because with a single subtract you can determine equal to, greater than or less than (and combinations less than or equal to, greater than or equal to).
For the case je or jump if equal. The subtract will result in a zero or not, if you subtract a from b and b and a are the same value then the result is zero. if the result is zero then the z flag is set else the z flag is not set. So if the compare resulted in things being equal that is the same as the z flag being set so jump if equal is also know as jump if zero, jump if the result was zero which is the same as jump if the zero flag is set. Jne is jump if not equal or jump if the zero flag is not set (sometimes you will see jnz depending on the instruction set and syntax). In your case you subtracted 1234 from itself the result was zero the z flag is set the jne does not affect the flags it says jump if the z bit is zero or keep going, so it kept going, then je says jump if the z flag is set which it was so it jumped...
Upvotes: 0
Reputation: 25873
Because the bit flag set from the comparison (in this case you only care about ZF
) has not been cleared.
On a side note you should use CMP
not SUB
.
Upvotes: 0