FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
8088/8086 MICROPROCESSOR PROGRAMMING
CONTROL FLOW INSTRUCTIONS AND PROGRAM STRUCTURES
Flag-Control Instructions Compare Instructions Subroutines and Subroutine-Handling Instructions Control Flow and Jump Instructions The Loop and the Loop-Handling Instructions String and String-Handling Instructions
5/3/2012
K.Rajalakshmi,JIIT,Noida
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Flag-Control Instructions The flag-control instructions, when executed, directly affect the state of the flags. These 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)
5/3/2012 K.Rajalakshmi,JIIT,Noida
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Flag-Control Instructions
SF = Sign flag ZF = Zero flag AF = Auxiliary PF = Parity flag CF = Carry flag
- = Undefined (do not use)
5/3/2012 K.Rajalakshmi,JIIT,Noida 3
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
EXAMPLE
Write an instruction sequence to save the current contents of the 8088s 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/3/2012
K.Rajalakshmi,JIIT,Noida
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
EXAMPLE
Of the three carry flag instructions CLC, STC, and CMC, only one is really independent instruction. That is, the operation that it provides cannot be performed by a series of the other two instructions. Determine which one of the carry instruction is the independent instruction. Solution:
CLC STC followed by a CMC
STC CLC followed by a CMC Therefore, only CMC is the independent instruction.
5/3/2012
K.Rajalakshmi,JIIT,Noida
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Compare Instruction
The compare operation enable us to determine the relationship between two numbers.
Allowed operands for compare instruction
5/3/2012
K.Rajalakshmi,JIIT,Noida
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
EXAMPLE Describe what happens to the status flags as the sequence of instructions that follows is executed. MOV AX, 1234H MOV BX, 0ABCDH CMP AX, BX Solution: (AX) = 123416 = 00010010001101002 (BX) = 123416 = 10101011110011012 (AX) (BX) = 00010010001101002 - 10101011110011012 = 01100110011001112 Therefore, ZF = 0, SF = 0, OF = 0, PF = 0 ,CF = 1, AF = 1
5/3/2012
K.Rajalakshmi,JIIT,Noida
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Subroutines and Subroutine- Handling Instructions
A subroutine is a special program that can be called for execution from any point in a program. A subroutine is also known as a procedure. A call instruction is used to call a subroutine A return instruction must be included at the end of the subroutine to initiate the return sequence to the main program environment. CALL and RET instructions PUSH and POP instructions
5/3/2012 K.Rajalakshmi,JIIT,Noida 8
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Subroutine concept
Main program
: : :
Subroutine A
First Instruction
: : :
Call subroutine A Next Instruction
: : :
Return
Call Subroutine A
Next Instruction
: : :
5/3/2012
K.Rajalakshmi,JIIT,Noida
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
10
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
11
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Example: Write a program to display OK on the screen using procedure disp
.model small disp proc near mov ah,2 int 21h ret disp endp
.code
mov dl,'o' call disp mov dl,'k' call disp
mov ah,4ch
int 21h
end
5/3/2012
K.Rajalakshmi,JIIT,Noida
12
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
13
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
14
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
EXAMPLE
Write a procedure named SQUARE that squares the contents of
BL and places the result in BX
Solution: ;Subroutine: SQUARE ;Description: (BX)=square of (BL) SQUARE PROC NEAR PUSH AX ; Save the register to be used
MOV AL, BL ; Place the number in AL
IMUL BL ; Multiply with itself MOV BL, AL ; Save the result
POP AX ; Restore the register used
RET SQUARE ENDP
5/3/2012 K.Rajalakshmi,JIIT,Noida 15
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
16
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Control Flow and Jump Instructions
Unconditional jump instruction Conditional jump instruction Branching structure IF-THEN Loop program structure REPEAT-UNTIL & WHILE-DO Applications using the loop and branch software structures
5/3/2012
K.Rajalakshmi,JIIT,Noida
17
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Unconditional and conditional jump
Unconditional jump program sequence
5/3/2012 K.Rajalakshmi,JIIT,Noida
18
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Unconditional and conditional jump
Conditional jump program sequence
5/3/2012 K.Rajalakshmi,JIIT,Noida 19
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
+127 to -128 bytes
+ or 32K + or 2G
5/3/2012
K.Rajalakshmi,JIIT,Noida
20
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Conditional jump instruction
5/3/2012
K.Rajalakshmi,JIIT,Noida
21
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
22
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
23
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Branch program structure IF-THEN
IF-THEN branch program structure using a flag-condition test
5/3/2012
K.Rajalakshmi,JIIT,Noida
24
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Branch program structure IF-THEN
IF-THEN branch program structure using register-bit test
5/3/2012
K.Rajalakshmi,JIIT,Noida
25
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Branch program structure IF-THEN
IF-THEN branch program structure using an alternative register-bit test
5/3/2012
K.Rajalakshmi,JIIT,Noida
26
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
27
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
28
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
29
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
30
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
31
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
32
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
EXAMPLE Implement an instruction sequence that calculates the absolute difference between the contents of AX and BX and places it in DX. Solution:
CMP AX, BX
JC DIFF2 DIFF1: MOV DX, AX SUB DX, BX JMP DONE DIFF2: MOV DX, BX SUB DX, AX DONE: NOP
5/3/2012 K.Rajalakshmi,JIIT,Noida 33
; (DX)=(AX)-(BX)
; (DX)=(BX)-(AX)
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
34
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
35
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
36
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
EXAMPLE
Given the following sequence of instructions, explain what happens as they are executed.
MOV DL, 05
MOV AX, 0A00H MOV DS, AX
MOV SI, 0
MOV CX, 0FH AGAIN: INC SI
CMP [SI], DL
LOOPNE AGAIN
5/3/2012 K.Rajalakshmi,JIIT,Noida
37
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
String and String-Handling Instructions
5/3/2012
K.Rajalakshmi,JIIT,Noida
38
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Move string MOVSB, MOVSW
Example The block-move program using the move-string instruction
5/3/2012
K.Rajalakshmi,JIIT,Noida
39
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Compare string and scan string
CMPSB/CMPSW, SCASB/SCASW Example Block scan operation using the SCASB instruction
5/3/2012
K.Rajalakshmi,JIIT,Noida
40
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Load and store string LODSB/LODSW, STOSB/STOSW
Example Initializing a block of memory with a store string instruction
5/3/2012
K.Rajalakshmi,JIIT,Noida
41
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
5/3/2012
K.Rajalakshmi,JIIT,Noida
42
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
REP string REP (repeat prefixes) Example Initializing a block of memory by repeating the STOSB instruction
5/3/2012
K.Rajalakshmi,JIIT,Noida
43
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
Auto indexing for string instruction CLD and STD instructions
5/3/2012
K.Rajalakshmi,JIIT,Noida
44
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
EXAMPLE
Describe what happen as the following sequence of instruction is executed. CLD MOV AX, DATA_SEGMENT MOV DS, AX MOV AX, EXTRA_SEGMENT MOV ES, AX MOV CX, 20H
MOV SI, OFFSET MASTER
MOV DI, OFFSET COPY REPZMOVSB
5/3/2012 K.Rajalakshmi,JIIT,Noida 45