Lecture14 PDF
Lecture14 PDF
8088/8086 Programming:
Control Flow Instructions and Program
Structures
1
Control Flow Instructions and Program
Structures
Flag-Control Instructions
Compare Instruction
Control Flow and Jump Instructions
Subroutines and Subroutine-Handling Instructions
The Loop and the Loop-Handling Instructions
String and String-Handling Instructions
Input/Output Instructions
2
Flag-Control Instructions
8088 microprocessor has a set of flags that monitors the
state of executing instructions or controls options
Flag-control instructions directly affect the state of the
flags
Flag-control instructions include
◦ LAHF (Load AH from flags)
◦ SAHF (Store AH into flags)
◦ CLC (Clear carry)
◦ STC (Set carry)
◦ CMC (Complement carry)
◦ CLI (Clear interrupt)
◦ STI (Set interrupt)
3
Flag-Control Instructions
Flag-Control Instructions & Format of the flags in AH
register for LAHF and SAHF
4
Flag-Control Instructions
EXAMPLE:
◦ Write an instruction sequence to save the current
contents of the 8088’s flags in the memory location
at offset MEM1 of the current data segment and then
reload the flags with the contents of the storage
location at offset MEM2
Solution:
LAHF ; Load AH from flags
MOV [MEM1], AH ; Move content of AH to MEM1
MOV AH, [MEM2] ; Load AH from MEM2
SAHF ; Store content of AH into flags
5
Compare Instruction
The compare operation enable us to determine the
relationship between two numbers (8 bits or 16 bits)
6
Compare Instruction
The process of comparison performed by the CMP
instruction is a subtraction operation
But the result of this subtraction is not saved
6 status register can be changed
◦ ZF
◦ SF
◦ CF
◦ AF
◦ OF
◦ PF
7
Compare Instruction
Example: destination equal source
MOV AL,05
CMP AL,05 ;Zero flag set
Example: destination less than source
MOV AL,04
CMP AL,05 ;Carry flag set
Example: destination greater than source
MOV AL,06
CMP AL,05 ; ZF=0 and CF=0
8
Compare Instruction
EXAMPLE:
◦ Describe what happens to the status flags as the
sequence of instructions that follows is executed
MOV AX, 1234
MOV BX, ABCD
CMP AX, BX
Solution:
(AX) = 123416 = 00010010001101002
(BX) = ABCD16 = 10101011110011012
(AX) – (BX) = 00010010001101002 – 10101011110011012 =
01100110011001112
Therefore, ZF = 0, SF = 0, OF = 0, PF = 0, CF = 1, AF = 1
9
Test Instruction
The TEST instruction performs a bit by bit logical AND
operation on the two operands.
Similar to compare instruction, it does not change the
operands but it only affects the condition of the flag
register.
TEST AX, BX
TEST AX, 88
TEST [700], CX
10
TEST Instruction
Example:
TEST AL,1 ;Test LSB of AL
TEST AL,80 ;Test MSB of AL
Example: jump to a label if either bit 0 or bit 1 in AL is set.
TEST AL,03
JNZ label
11
Control Flow and Jump Instructions
Unconditional jump instruction
Conditional jump instruction
Branching structure – IF-THEN
Loop program structure – REPEAT-UNTIL and WHILE-
DO
Applications using the loop and branch software
structures
12
Control Flow and Jump Instructions
Unconditional jump instruction
13
Control Flow and Jump Instructions
Unconditional jump instruction
14
Control Flow and Jump Instructions
Short-label
+ve 00-7F +127
-ve 80-FF -128
Near-label
+ve 0000-7FFF +32,767
-ve 8000-FFFFF -32,768
Memptr16
JMP [BX] where BX=1234
Regptr16
JMP BX where BX=1234
15
Control Flow and Jump Instructions
Far-label
JMP 1234ABCD
Memptr32
JMP Dword ptr [DI]
16
Control Flow and Jump Instructions
Unconditional jump instruction
◦ Two basic jumps
1. Intrasegment jump
- It is limited to address within the current code segment
2. Intersegment jump
- It permits jumps from one code segment to another
17
Control Flow and Jump Instructions
EXAMPLE:
1. JMP BX, with the contents of BX be 001016
2. JMP [BX] , with the contents of BX be 100016
Solution:
1. IP is changed into 001016
2. IP is changed into DS:100016 (20016)
18
Control Flow and Jump Instructions
Conditional jump instruction
19
Control Flow and Jump Instructions
Conditional jump instruction
20
Control Flow and Jump Instructions
Conditional jump instruction
21
Control Flow and Jump Instructions
Conditional jump instruction
The terms below and above refer to unsigned numbers. Above means larger in magntude.
The terms greater and less refer to sign numbers. Greater than means more positive.
22
Control Flow and Jump Instructions
Branch program structure – IF-THEN
◦ IF-THEN branch program structure using a flag-condition test
23
Control Flow and Jump Instructions
Branch program structure – IF-THEN
◦ IF-THEN branch program structure using register-bit test
24
Control Flow and Jump Instructions
Branch program structure – IF-THEN
◦ IF-THEN branch program structure using an alternative register-bit
test
25
Control Flow and Jump Instructions
Loop program structures – REPEAT-UNTIL
◦ Typical REPEAT-UNTIL instruction sequence
26
Control Flow and Jump Instructions
Loop program structures – WHILE-DO
◦ Typical WHILE-DO instruction sequence
27