Lecture 5 and Lecture 6 Chapter 7 - Program Logic and Control
Lecture 5 and Lecture 6 Chapter 7 - Program Logic and Control
Chapter Outline
Short, near and far address
JMP Instruction
The CMP Instruction
Conditional Jump instruction
The Loop instruction
While Loop
REPEAT Loop
NEAR
FAR
JMP
YES
YES
YES
JXXX(conditional jump)
YES
YES
NO
LOOP
YES
NO
NO
CALL
NlA
YES
YES
SHORT/NEAR/FAR address
Example
JMP
..
L10:
L10
INC CX
Conditional Jumps
Syntax
Jxxx
destination_label
Example
JNZ
PRINT_LOOP
Conditional Jumps
Signed Jumps: used for signed interpretations.
Symbol
JG/JNLE
JGE/JNL
JL/JNGE
JLE/JNG
Description
jump if grater than
jump if not less than or equal
jump if grater than or equal
jump if not less than
jump if less than
jump if not greater than or equal
jump if less than or equal
jump if not grater than
ZF = 0 & SF = OF
SF = OF
SF <> OF
ZF = 1 or SF <> OF
Conditional Jumps
Unsigned Jumps: used for unsigned interpretations.
Symbol
JA/JNBE
JAE/JNB
JB/JNAE
JBE/JNA
Description
jump if above
jump if not below or equal
jump if above or equal
jump if not below
jump if below
jump if not above or equal
jump if below or equal
jump if not above
CF = 0 & ZF = 0
CF = 0
CF = 1
CF = 1 or ZF = 1
Conditional Jumps
Single Flag Jumps: operates on settings of individual flags.
Symbol
JE/JZ
JNE/JNZ
JC
JNC
JO
JNO
JS
JNS
JP/JPE
JNP/JPO
Description
jump if equal/ jump if equal to 0
jump if not equal/ jump if not 0
jump if carry
jump if no carry
jump if overflow
jump if no overflow
jump if sign negative
jump if nonnegative sign
jump if parity even
jump if parity odd
ZF = 1
ZF = 0
CF = 1
CF = 0
OF = 1
OF = 0
SF = 1
SF = 0
PF = 1
PF = 0
IF-THEN-ELSE
Example: Suppose AL and BL contain extended ASCII characters.
Display the one that comes first in the character sequence.
Solution:
Pseudocode:
IF AL <= BL
THEN
display the character in AL
ELSE
display the character in BL
END_IF
continue
IF-THEN-ELSE
It can be coded as follows:
; if AL <= BL
CMP AL, BL
JNBE ELSE_
; then
MOV DL, AL
JMP DISPLAY
ELSE_:
MOV DL, BL
DISPLAY:
MOV AH, 2
INT 21h
; AL <= BL?
; no, display char in BL
; AL <= BL
; move char to be displayed
; go to display
; BL < AL
; prepare to display
; display it
AND condition
OR condition
AND Condition
An AND condition is true if and only if all conditions are true.
Example: Read a character, and if its an uppercase letter, display it.
Solution:
Pseudocode:
Read a character (into AL)
IF ('A' <= character) and (character <= 'Z')
THEN
display character
END_IF
continue
AND Condition
It can be coded as follows:
; read a character
MOV AH,1
; prepare to read
INT 21h
; char in AL
; if ('A' <= char) and (char <='Z')
CMP AL, 'A'
; char >= 'A'?
JNGE END_IF
; no, exit
CMP AL, 'Z'
; char <= 'Z'?
JNLE END_IF
; no, exit
; then display char
MOV DL, AL
; get char
MOV AH, 2
; prepare to display
INT 21h
; display char
END_IF:
OR Condition
An OR condition is true if at least one of the conditions is true.
Example: Read a character. If its 'y' or 'Y', display it; otherwise,
terminate the program.
Solution:
Pseudocode:
Read a character (into AL)
IF (character = 'y') or (character = 'Y')
THEN
display character
ELSE
terminate the program
END_IF
continue
OR Condition
It can be coded as follows:
; read a character
MOV AH,1
INT
21h
; prepare to read
; char in AL
CMP
JE
CMP
JE
JMP
AL, 'y'
THEN
AL, 'Y'
THEN
ELSE_
; char = 'y'?
; yes, go to display it
; char = 'Y'?
; yes, go to display it
; no, terminate
MOV
MOV
INT
JMP
DL, AL
AH, 2
21h
END_IF
; get char
; prepare to display
; display char
; and exit
THEN:
ELSE_:
; DOS exit
Loop Instruction
The LOOP instruction can be used to implement a for loop.
Syntax:
LOOP
SHORT address
The counter for the loop is the register CX, which is initialized to
loop_count.
Execution of the LOOP instruction causes CX to be decremented
automatically.
If (CX < > 0) control transfers to destination_label
else the next instruction after LOOP is done.
Loop Instruction
Using the instruction LOOP, a FOR loop can be implemented as
follows:
TOP:
; initialize CX to loop_count
; body of the loop
LOOP TOP
FOR Loop
Example: Write some code to display a row of 80 stars.
Solution:
Pseudocode:
; what if CX =0?
FOR 80 times DO
display '*'
MOV CX, 80
END_IF
MOV AH, 2
It can be coded as follows:
MOV DL, '*'
MOV CX, 80
JCXZ SKIP ;jump if CX=0
MOV AH, 2
TOP:
MOV DL, '*'
INT 21h
TOP:
LOOP TOP
INT 21h
SKIP:
LOOP TOP
WHILE Loop
This loop depends on a condition.
Pseudocode:
WHILE condition DO
statements
END_WHILE
WHILE Loop
Example: Write some code to count the number of characters in an
input line.
Solution:
Pseudocode:
initialize count to 0
read a character
WHILE character <> carriage_return DO
count = count + 1
read character
END_WHILE
continue
WHILE Loop
It can be coded as follows:
MOV
MOV
INT
WHILE_:
CMP
JE
INC
INT
JMP
DX, 0
AH, 1
21h
; DX counts characters
; prepare to read
; character in AL
AL, 0Dh
END_WHILE
DX
21h
WHILE_
; CR?
; yes, exit
; not CR, increment count
; read a character
; loop back
END_WHILE:
REPEAT Loop
This loop depends on a condition.
Pseudocode:
REPEAT
Statements
UNTIL conditions
REPEAT Loop
Example: write code to read characters until a blank is read
Pseudocode:
REPEAT
Read character
UNTIL character is blank The code is:
REAPEAT:
MOV AH,1
INT 21H
CMP AL,
JNE REAPEAT