Chapter 6
Chapter 6
Overview
Flow control instructions are used to make decisions and repeat
sections of code. The jump and loop instructions transfer control to
another part of the program. This transfer can be unconditional or
conditional (dependent on a particular combination of status flags
settings)
6.1 An Example of a Jump
TITLE PGM6_1: IBM CHARACTER DISPLAY
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH, 2
MOV CX, 256
MOV DL, 0
PRINT_LOOP:
INT 21H MOV AH, 4CH
INC DL INT 21H
DEC CX
MAIN ENDP
JNZ PRINT_LOOP
END MAIN
6.2 Conditional Jumps
JNZ is an example of a conditional jump instruction. The syntax is
Jxxx destination_label
Note: CMP is just like SUB, except that destination is not changed.
For example, suppose a program contains these lines:
CMP AX, BX
JG BELOW
where, AX=7FFFH, and BX=0001H. Table 6.1 shows that JG is satisfied,
because ZF = SF = OF = 0, so control transfers to label BELOW.
Table 6.1: Conditional Jumps
Signed Jumps
Symbol Description Condition for Jumps
JG/JNLE jump if greater than ZF = 0 and SF = OF
jump if not less than
or equal to
JGE/JNL jump if greater than SF = OF
or equal to
jump if not less than
JL/JNGE jump if less than SF <> OF
jump if not greater than
or equal to
JLE/JNG jump if less than or equal ZF = 1 or SF <> OF
Jump if not greater than
Unsigned Jumps
Symbol Description Condition for Jumps
JA/JNBE jump if above CF = 0 and ZF = 0
jump if not below
or equal to
JAE/JNB jump if above CF = 0
or equal to
jump if not below
JB/JNAE jump if below CF = 1
jump if not above or equal
JBE/JNA jump if below or equal CF = 1 or ZF = 1
jump if not above
Single-Flag Jumps
Symbol Description Condition for Jumps
JE/JZ jump if equal ZF = 1
jump if equal to zero
JNE/JNZ jump if not equal ZF = 0
jump if not zero
JC jump if carry CF = 1
JNC jump if not carry CF = 0
JO jump if overflow OF = 1
JNO jump if not overflow OF = 0
JS jump if sign negative SF = 1
JNS jump if nonnegative sign SF = 0
JP/JPE jump if parity even PF = 1
JNP/JPO jump if parity odd PF = 0
Interpreting the Conditional Jumps
In the following example:
CMP AX, BX
JG BELOW
if AX is greater than BX (in a signed sense), then JG transfers to
BELOW.
Another example is
DEC AX
JL THERE
IF-THEN
IF condition is true
THEN
Execute true-branch statements
END_IF
IF-THEN-ELSE
IF condition is true
THEN
Execute true-branch statements
ELSE
Execute false-branch statements
END_IF
Example 6.3: Suppose AL and BL contain extended ASCII characters.
Display the one that comes first in the character sequence.
Solution:
MOV AH, 2
CMP AL, BL
JNBE ELSE_
MOV DL, AL
JMP DISPLAY
ELSE_:
MOV DL, BL
DISPLAY:
INT 21H
END_IF:
CASE
CASE expression
values_1: statements_1
values_2: statements_2
:
:
values_n: statements_n
END_CASE
AND Conditions
Example 6.6: Read a character, and if it’s an uppercase letter, display it.
Solution:
MOV AH,1
INT 21H
CMP AL, ‘A’
JNGE END_IF
CMP AL, ‘Z’
JNLE END_IF
MOV DL,AL
MOV AH,2
INT 21H
END_IF:
OR Conditions
Example 6.7: Read a character. If it’s “y”’ or “Y”, display it;
otherwise, terminate the program.
Solution:
MOV AH,1
INT 21H
CMP AL, ‘y’
JE THEN
CMP AL, ‘Y’
JE THEN
JMP ELSE_
THEN: MOV AH, 2
MOV DL,AL
INT 21H
JMP END_IF
ELSE_: MOV AH, 4CH
INT 21H
END_IF:
FOR LOOP
The LOOP instruction can be used to implement a for loop. It has the
form:
LOOP destination_label
Solution:
MOV CX, 80
MOV AH,2
MOV DL, ‘*’
TOP:
INT 21H
LOOP TOP
JCXZ SKIP
TOP:
; body of the loop
LOOP TOP
SKIP:
WHILE LOOP
WHILE condition DO
Statements
END_WHILE
Example 6.9: Write some code to count the number of characters in
an input line.
MOV DX, 0
MOV AH, 1
INT 21H
WHILE_:
CMP AL, 0DH
JE END_WHILE
INC DX
INT 21H
JMP WHILE_
END_WHILE: