Flow Control Instructions: Chapter-6
Flow Control Instructions: Chapter-6
Instructions
Chapter-6
Prepared by~
Mohammad Samawat Ullah
Assistant Professor,
Department of CS
AIUB
Email: [email protected]
Overview
At the end of this chapter, we will know
about:
INT 21H
.STACK 100H
INC DL
.CODE DEC CX
JNZ PRINT_LOOP ;
MAIN PROC
; return to DOS
MOV AH,2 MOV AH,4Ch ;DOS exit
INT 21H
MOV CX,256
MAIN ENDP
MOV DL,0 ; ASCII of null
END MAIN
JNZ (Jump if Not
Zero)
• JNZ is the instruction that controls loop.
JXXX destination_label
It computes by…
JB or • Jump if Below
CF = 1
JNAE • Jump if not Above or Equal
CMP AX,BX
JA BELOW
JMP destination
DEC CX
TOP:
JNZ BOTTOM ; keep looping till
CX>0
DEC CX
JMP EXIT
JNZ TOP BOTTOM:
JMP TOP
MOV AX,BX
EXIT:
MOV AX,BX
High-Level Language
Structures
Jump can be used to implement branches and
loops
1. IF-THEN
2. IF-THEN-ELSE
3. CASE
IF-THEN
IF condition is true.
THEN
execute true-
branch statements
END_IF
A Pseudo Code , Algorithm and
Code for IF-THEN
• The co11dition is an expression that is either true
or false.
THEN
execute true-branch
statements
ELSE
execute false-branch
statements
END_IF
A Pseudo Code and Algorithm and
Code for
IF-THEN-ELSE
• The condition is an expression that is either true or false.
CASE Expression
Values_1: Statement_1
Values_2: Statement_2
Values_n: Statement_n
END_CASE
CASE
• Example: If AX contains a negative number, put
-1 in BX; if AX contains 0, put 0 in BX; and if AX
contains a positive number, put 1 in BX.
CMP AX,O
JL NEGATIVE
JE ZERO
JG POSITIVE
CASE AX NEGATIVE:
<0 : put -1 in BX MOV BX,-1
=0 : put 0 in BX JMP END_CASE
ZERO:
>0 : put +l in BX
MOV BX,0
END_CASE JMP END_CASE
POSITIVE:
MOV BX, l
END_CASE:
Solve the Following
• If AL contains 1 or 3, display "o"; if AL
contains 2 or 4, display "e".
CASE AL
END_CASE
Branches with Compound
Conditions
Sometimes the branching condition in an IF or CASE takes the
form
or
condition_1 OR condition_2
THEN
display character
END IF
Converting to Assembly
;read a character CMP AL, 'Z'
THEN
display it
ELSE
END IF
ASSEMBLY CONVERSION
MOV AH,1 THEN:
FOR LOOP
WHILE LOOP
REPEAT LOOP
FOR LOOP
• FOR LOOP is a loop structure in which the loop statements are repeated
a known number of times (a count-controlled loop). In pseudo
code,
Statements
END_FOR
LOOP destination_label
TOP:
;initialize CX to loop_count
LOOP TOP
Example:
• Write a count- MOV CX,0
controlled loop to
display a row of 80 MOV AH,2
stars: MOV DL, '*'
JCXZ destination_label
Use of JCXZ
• If CX contains 0, control transferred to the
destination label. So a loop implemented as follows
is bypassed if CX is 0:
JCXZ SKIP
TOP:
LOOP TOP
SKIP:
WHILE LOOP
• WHILE condition DO
statements
END_WHILE
WHILE LOOP
The condition is checked at the top of the loop.
WHILE_:
• WHILE character <>
carriage_return DO CMP AL,0DH ; CR ?
END_WHILE:
WHILE LOOP Insights
A WHILE loop checks the terminating condition
at the top of the loop,
REPEAT
statements
UNTIL condition
MOV AH,1
REPEAT
REPEAT:
read a character
INT 21H
MOV AH,9
LEA DX,PROMPT
INT 21H
Step 2. Read and process a line of
text.
Read a character
THEN
THEN
END_IF
THEN
END_IF
END IF
Variables FIRST and LAST must have values before the
WHILE loop is executed the first time. They can be
initialized in the data segment
MOV AH,1
INT 21H
MOV FIRST,AL ;FIRST - char
CHECK_LAST:
WHILE_:
CMP AL,LAST ;char > LAST?
CMP AL,0DH ;CR?
JNG END_IF ;no, <=
JE END_WHILE ;yes, exit
MOV LAST I AL ; LAST - char
CMP AL, 'A' ;char >= 'A'?