COAL Lec 7 Flow Control Instructions - Chap6
COAL Lec 7 Flow Control Instructions - Chap6
COAL Lec 7 Flow Control Instructions - Chap6
3
Categories of Conditional Jump
Signed Jumps: are used when a signed interpretation is
being given to result.
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 or equal to SF = OF
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 to ZF = 1 and SF <> OF
Jump if not greater than
4
Contd..
Unsigned Jumps: are used for an unsigned interpretation of
result
Symbol Description Condition for Jumps
JA/JNBE Jump if above than ZF = 0 and CF = 0
Jump if not below than or equal to
JAE/JNB Jump if above than or equal to CF = 0
Jump if not below than
JB/JNAE Jump if below than CF = 1
Jump if not above than or equal to
JBE/JNA Jump if below than or equal to ZF = 1 and CF = 1
Jump if not above than
5
Contd.
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
6
odd
CMP Instruction
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 < 0 1
Source
Destination > 0 0
Source
Destination = 1 0
Source
7
CMP Instruction Examples
Destination < Source:
mov ax, 5
cmp ax, 10 ;CF = 1, ZF = 0
Destination = Source
mov ax, 1000
mov cx, 1000
cmp cx, ax ; ZF = 1, CF = 0
Destination > Source
mov ax, 105
cmp ax, 0 ; ZF = 0 and CF = 0
8
High Level Language Structures
Branching Structure
IF-THEN
IF-THEN-ELSE
CASE
Branching with Compound Conditions
AND CONDITIONS
OR CONDITIONS
Looping Structures
FOR LOOP
WHILE LOOP
REPEAT LOOP
9
IF-THEN
;Code in Assembly Language:
End
10
IF-THEN-ELSE
;Code in Assembly Language:
13
Contd..
ORG 100h
.CODE
MOV AH, 1
INT 21H
;character in AL
CMP AL, ‘A’ ;if AL >= ‘A’
JNAE END_IF
CMP AL, ‘Z’ ;and AL <= ‘Z’
JNBE END_IF
MOV AH, 2
MOV DL, AL ;then
INT 21H
END_IF:
MOV AH, 4Ch
INT 21H
14
OR Condition
An OR condition is true if at least one of the conditions is
true.
Consider following Pseudo code:
Read a character (into AL)
IF (character = ‘y’) OR (character = ‘Y’)
THEN
display it
ELSE
terminate the program
END_IF
15
Contd..
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
ELSE_:
MOV AH, 4Ch
INT 21H
16
Looping Structures
FOR LOOP ORG 100h
.CODE
;initialize CX
MOV CX, 80
TOP: MOV AH, 2
;body of the loop MOV DL, ‘*’
LOOP TOP TOP:
INT 21H
LOOP TOP
17
Contd..
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:
18
Contd..
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
END_WHILE
19
Contd..
ORG 100h
.CODE
MOV DX, 0
MOV AH, 1 ;read first character
INT 21H
Note: Requires 2 Jumps:
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
20
Contd..
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
21
Contd..
ORG 100h
.CODE Note: Requires only one Conditional
MOV AH, 1 ;read first character Jump at the end
REPEAT_:
INT 21H Also, If terminating condition is false, still
CMP AL, ‘ ‘ loop is executed at least once.
JNE REPEAT_
MOV AH, 4Ch
INT 21H
22
For Practice(Book Ytha Yu)
Example given in section 6.5
Ch 6 Exercise: Q1, Q2, Q4
23