Flow Control Instructions
Flow Control Instructions
Sheeza Zaheer
Lecturer
University of Management and Technology
Lahore Campus
2
ORG 100h
.CODE
MOV AX, 2
MOV BX, 2
JMP LABEL_SUB
ADD AX, BX ;this instruction will never execute
LABEL_SUB:
SUB AX, BX
RET
4
Conditional Jump
5
● Syntax:
Jxxx destination_label
where xxx represents the condition
● If condition is true, the next instruction to be executed is the one at
destination_label.
● If condition is false, the instruction immediately following the jump is done
next.
● To implement a conditional jump, the CPU looks at the FLAG register (set
by last instruction executed by the processor).
● JUMP instructions themselves do not affect flags.
5
Categories of Conditional Jump
6
6
Contd..
7
7
Contd.
8
● Single-Flag Jumps: which operate on settings of individual
flags.
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 Equal to 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 Sign Non Negative SF = 0
JP/JPE Jump if parity even PF = 1
JNP/JPO Jump if parity not even/jump if parity PF = 0
8
odd
CMP Instruction
9
● CMP (compare) instruction performs an implied subtraction of a source
operand from destination operand. Neither operand is modified.
CMP destination, source
● FLAGS
CMP Result ZF CF
Destination = Source 1 0
9
CMP Instruction Examples
10
10
High Level Language Structures
11
● Branching Structure
■ IF-THEN
■ IF-THEN-ELSE
■ CASE
■ Branching with Compound Conditions
○ AND CONDITIONS
○ OR CONDITIONS
● Looping Structures
■ FOR LOOP
■ WHILE LOOP
■ REPEAT LOOP
11
IF-THEN
12
;Code in Assembly Language:
ORG 100h
False True
.CODE
AX < 0
MOV AX, FFFE
CMP AX, 0
JL IF1
JMP END_IF
IF1:
Replace AX NEG AX
by –AX END_IF:
MOV AH, 4CH
INT 21H
End
12
IF-THEN-ELSE
13
;Code in Assembly Language:
15
Contd..
16
ORG 100h
.CODE
MOV AH, 1
INT 21H
;character in AL
CMP AL, ‘A’ ;if AL >= ‘A’
JGE L1
JMP END_IF
L1:
CMP AL, ‘Z’ ;and AL <= ‘Z’
JLE L2
JMP END_IF
L2:
MOV AH, 2
MOV DL, AL ;then
INT 21H
END_IF:
MOV AH, 4Ch
INT 21H
16
OR Condition
17
17
Contd..
18
ORG 100h
.CODE
MOV AH, 1
INT 21H ;character in AL
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:
18
Looping Structures
19
ORG 100h
● FOR LOOP .CODE
;initialize CX MOV CX, 80
TOP: MOV AH, 2
;body of the loop MOV DL, ‘*’
TOP:
LOOP TOP
INT 21H
LOOP TOP
19
Contd..
20
● FOR LOOP
■ Executed at least once.
■ If CX contains 0, the loop instruction decrements CX (CX =
FFFFh) and the loop is then executed 65535 times!
■ To prevent this, use instruction JCXZ before the loop.
;initialize CX
JCXZ SKIP
TOP:
;body of the loop
LOOP TOP
SKIP:
20
Contd..
21
● WHILE LOOP
WHILE condition DO
;statements
END_WHILE
● WHILE LOOP checks the terminating condition at the top of
the loop, so don’t forget to initialize variables.
● Example:
Initialize count to 0
Read a character
WHILE character <> carriage return Do
count = count + 1
read a character
21 END_WHILE
Contd..
22
ORG 100h
.CODE
MOV DX, 0
MOV AH, 1 ;read first character Note: Requires 2 Jumps:
INT 21H o Conditional Jump at top
WHILE_:
CMP AL, 0Dh o JMP at the bottom
JE END_WHILE
INC DX Also, If terminating condition is false, loop
INT 21H is not executed.
JMP WHILE_
END_WHILE:
MOV AH, 4Ch
INT 21H
22
Contd..
23
● REPEAT LOOP
REPEAT
;statements
UNTIL condition
● First statements are executed, then the condition is checked.
● If true, the loop terminates; if false, control branches to the top
of the loop
● Example:
REPEAT
Read a character
UNTIL character is a blank
23
Contd..
24
ORG 100h Note: Requires only one Conditional
.CODE Jump at the end
MOV AH, 1 ;read first character
REPEAT_: Also, If terminating condition is false, still
INT 21H loop is executed at least once.
CMP AL, ‘ ‘
JNE REPEAT_
MOV AH, 4Ch
INT 21H
24
For Practice
25
● Example given in section 6.5
● Ch 6 Exercise: Q1, Q2, Q4
25