Lecture 12 Control, Subroutine, String Instructions)
Lecture 12 Control, Subroutine, String Instructions)
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-Control Instructions The flag-control instructions, when executed, directly affect the state of the flags. These instructions include:
Flag-Control Instructions
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:
5/3/2012
K.Rajalakshmi,JIIT,Noida
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:
5/3/2012
K.Rajalakshmi,JIIT,Noida
Compare Instruction
The compare operation enable us to determine the relationship between two numbers.
5/3/2012
K.Rajalakshmi,JIIT,Noida
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
Subroutine concept
Main program
: : :
Subroutine A
First Instruction
: : :
Return
Call Subroutine A
Next Instruction
: : :
5/3/2012
K.Rajalakshmi,JIIT,Noida
5/3/2012
K.Rajalakshmi,JIIT,Noida
10
5/3/2012
K.Rajalakshmi,JIIT,Noida
11
.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
5/3/2012
K.Rajalakshmi,JIIT,Noida
13
5/3/2012
K.Rajalakshmi,JIIT,Noida
14
EXAMPLE
Solution: ;Subroutine: SQUARE ;Description: (BX)=square of (BL) SQUARE PROC NEAR PUSH AX ; Save the register to be used
5/3/2012
K.Rajalakshmi,JIIT,Noida
16
5/3/2012
K.Rajalakshmi,JIIT,Noida
17
18
5/3/2012
K.Rajalakshmi,JIIT,Noida
20
5/3/2012
K.Rajalakshmi,JIIT,Noida
21
5/3/2012
K.Rajalakshmi,JIIT,Noida
22
5/3/2012
K.Rajalakshmi,JIIT,Noida
23
5/3/2012
K.Rajalakshmi,JIIT,Noida
24
5/3/2012
K.Rajalakshmi,JIIT,Noida
25
5/3/2012
K.Rajalakshmi,JIIT,Noida
26
5/3/2012
K.Rajalakshmi,JIIT,Noida
27
5/3/2012
K.Rajalakshmi,JIIT,Noida
28
5/3/2012
K.Rajalakshmi,JIIT,Noida
29
5/3/2012
K.Rajalakshmi,JIIT,Noida
30
5/3/2012
K.Rajalakshmi,JIIT,Noida
31
5/3/2012
K.Rajalakshmi,JIIT,Noida
32
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)
5/3/2012
K.Rajalakshmi,JIIT,Noida
34
5/3/2012
K.Rajalakshmi,JIIT,Noida
35
5/3/2012
K.Rajalakshmi,JIIT,Noida
36
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
5/3/2012
K.Rajalakshmi,JIIT,Noida
38
5/3/2012
K.Rajalakshmi,JIIT,Noida
39
5/3/2012
K.Rajalakshmi,JIIT,Noida
40
5/3/2012
K.Rajalakshmi,JIIT,Noida
41
5/3/2012
K.Rajalakshmi,JIIT,Noida
42
REP string REP (repeat prefixes) Example Initializing a block of memory by repeating the STOSB instruction
5/3/2012
K.Rajalakshmi,JIIT,Noida
43
5/3/2012
K.Rajalakshmi,JIIT,Noida
44
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