| Conditional jump instructions are the basic tools for creating selective structures
like the IF..ENDIF statement and repetitive structures like loops.
|
| A conditional jump tests one or more flags in the flags register
|
| If the flag settings match the instruction, control transfers to the target location
|
| If the match fails, the CPU ignores the conditional jump and execution
continues with the next instruction.
|
| Most of the time, a conditional jump is executed after a CMP instruction.
|
| The CMP instruction sets the flags so that test can be carried out for less
than, greater than, equality, etc.
|
Conditional jump instructions take the following form:
Jcc label;
The “cc” in Jcc indicates that some character sequence, specifying the type of
condition to be tested, must be substituted.
For example, JS stands for jump if the sign flag is set and JC
stands for jump if the carry flag is set.
Conditional jumps test the sign (S), zero (Z), carry (C), parity (P),
and overflow (O) flags.
When comparing two numbers it is necessary to know whether these numbers are
representing signed or unsigned numbers in order to establish a relationship between them.
For example, suppose that AL=FF and BL=01. If we execute the instruction CMP AL, BL, the
result of the comparison will be different depending on whether the registers represent signed or
unsigned numbers.
If unsigned numbers are represented, then AL=255 and BL=1 and hence
AL is greater than BL. However, if signed numbers are represented, then AL=-1 and BL=1 and
hence BL is greater than AL. Thus, we need conditional jump instructions for unsigned number
comparison and conditional jump instructions for signed number
comparison.
Conditional jump instructions are divided into three main types:
| Single Flag Based Jump Instructions
|
| Unsigned Conditional Jump Instructions
|
| Signed Conditional Jump Instructions
|
Addressing Modes
| Unlike the unconditional JMP instruction, the conditional jump instructions do not
provide an indirect form.
|
| The only form they allow is a branch to a statement label in your program.
|
| Conditional jump instructions are only of type SHORT.
|
Since unconditional jump instructions can be only of type SHORT, if the target
address is not within a range of -128 to +127 from the IP, then a remedy is needed.
The following example shows two implementations of a program one when the target
address is within a SHORT range and the other when it is in a Near range.
Example: Target address is SHORT. |
CMP AX, BX ; compare ax to bx to set flags
JE THERE ; if equal, then skip next statement
ADD AX, 0002H ; correction statement
THERE: MOV CL, 07H ; load count
....
|
Example: Target address is Near. |
CMP AX, BX ; compare AX to BX to set flags
JNE FIX ; if not equal, then goto label FIX
JMP THERE ; otherwise, unconditional jump to there
FIX: ADD AX, 0002H ; correction statement
THERE: MOV CL, 07H ; load count
....
|