Lecture 12 (Flag,Control, Subroutine,String Instructions)
-
Upload
ayush-purohit -
Category
Documents
-
view
221 -
download
0
description
Transcript of Lecture 12 (Flag,Control, Subroutine,String Instructions)
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 1
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
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 2
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)
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 3
Flag-Control Instructions
SF = Sign flag
ZF = Zero flag
AF = Auxiliary
PF = Parity flag
CF = Carry flag
- = Undefined (do not use)
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 4
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
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 5
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.
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 6
Compare Instruction
The compare operation enable us to determine the relationship between two numbers.
Allowed operands for compare instruction
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 7
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) = ABCD16 = 10101011110011012
(AX) – (BX) = 00010010001101002 - 10101011110011012
= 01100110011001112
Therefore, ZF = 0, SF = 0, OF = 0, PF = 0 ,CF = 1, AF = 1
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 8
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
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 9
:
:
:
Call subroutine A
Next Instruction:
:
:
Call Subroutine A
Next Instruction:
:
:
First Instruction:
:
:
Return
Subroutine A
Main programSubroutine concept
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 12
.model small
.code
mov dl,'o'
call disp
mov dl,'k'
call disp
mov ah,4ch
int 21h
disp proc near
mov ah,2
int 21h
ret
disp endp
end
Example: Write a program to display OK on the screen using procedure ‘disp’
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 15
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 BX, AX ; Save the result
POP AX ; Restore the register used
RET
SQUARE ENDP
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 17
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
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 18
Unconditional and conditional jump
Unconditional jump program sequence
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 19
Conditional jump program sequence
Unconditional and conditional jump
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 20
+127 to -128 bytes
+ or – 32K
+ or – 2G
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 21
Conditional jump instruction
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 24
Branch program structure – IF-THEN
IF-THEN branch program structure using a flag-condition test
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 25
Branch program structure – IF-THEN
IF-THEN branch program structure using register-bit test
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 26
IF-THEN branch program structure using an alternative register-bit test
Branch program structure – IF-THEN
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 33
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 ; (DX)=(AX)-(BX)
JMP DONE
DIFF2: MOV DX, BX
SUB DX, AX ; (DX)=(BX)-(AX)
DONE: NOP
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 37
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
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 38
String and String-Handling Instructions
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 39
Move string – MOVSB, MOVSW
Example – The block-move program using the move-string
instruction
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 40
Compare string and scan string –
CMPSB/CMPSW, SCASB/SCASW
Example – Block scan operation using the SCASB instruction
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 41
Load and store string – LODSB/LODSW, STOSB/STOSW
Example – Initializing a block of memory with a store string instruction
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 43
REP string – REP (repeat prefixes)
Example – Initializing a block of memory by repeating the STOSB instruction
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 44
Auto indexing for string instruction – CLD and STD instructions
FLAG, COMPARE, SUBROUNTINE,CONTROL FLOW,LOOP,STRING HANDLING
04/19/23 K.Rajalakshmi,JIIT,Noida 45
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
REPMOVSB