Uploaded by Vasu Parbhakar

toaz.info-354-39-solutions-instructor-manual-15-8086-interrupts-chapter-14-pr b659c05af277f128feef5ab94bec161c

advertisement
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
Chapter – 14
Addressing modes, instruction set, and Programming of 8086
Think and Answer type questions
1. Let the content of different registers in 8086 be as follows: DS=1000H,
SS=2000H, ES=3000H, BX=4000H, SI=5000H, DI= 6000H and BP=7000H.
Find the memory address/addresses from where the 8086 accesses the data while
executing the following instructions:
a. MOV AX,[BX] - 14000H and 14001H
b. MOV BX,[SI]- 15000H and 15001H
c. MOV CX,[BP]- 27000H and 27001H
d. MOV AL,[DI]- 16000H
e. MOV BH,SS:[SI]- 25000H
f. MOV CX, ES:[DI]- 36000H and 36001H
g. MOV AX,[BX+DI]- 1A000H and 1A001H
h. MOV BX,[BP+DI+5]- 2D005H and 2D006H
i. MOV AH,[BX+10H]- 14010H
j. MOV CX, DS:[BP+4]- 17004H and 17005H
k. MOV BX,[SI-5]- 14FFBH
l. MOV AX,[BX+10]- 1400AH
2. Which registers of 8086 are modified while executing intersegment and
intrasegment jump instructions?
While executing intersegment jump instruction, the content of CS and IP are
modified. While executing intrasegment jump instruction, the content of IP alone is
modified.
3. Is it possible to exchange the content of two memory locations or content of two
segment registers using the XCHG instruction? Why?
No. It is not possible since the exchange of data can be done only between a
register and a memory location.
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
4. If the content of BP=1000H and SI=2000H, what is the value present in CX after
8086 executes the instruction LEA CX,[BP+SI] and LEA CX,[SI].
i)
3000H
ii)
2000H
5. Is it possible to use two memory operands in ADD and SUB instructions?
No. It is not possible since it is not allowed.
6. Is carry flag affected by the execution of INC and DEC instruction in 8086?
No. Carry flag is not affected by the execution of INC and DEC instruction in
8086.
7. What is the difference between SUB and CMP instruction?
Both instructions perform subtraction of the content of the source from the
content of the destination and affect the AF, OF, SF, ZF, PF and CF flags. The
content of destination is not affected by CMP instruction but it is affected by the
SUB instruction as the result of subtraction is stored in the destination.
8. What is the difference between TEST and AND instructions?
Both instructions perform ANDing the content of the source and the content of
the destination and affect the AF, OF, SF, ZF, PF and CF flags. The content of
destination is not affected by TEST instruction but it is affected by the AND
instruction as the result of AND is stored in the destination..
9. Which instructions of 8086 are used to handle procedure or subroutine?
CALL and RET instructions 8086 are used to handle procedure or subroutine.
10. What is the difference between arithmetic and logical right shift?
In arithmetic right shift, the sign bit is again inserted at the left end of operand for
each bit shift. In logical right shift, bit ‘0’ is inserted at the left end of operand for
each bit shift.
11. What are the common applications of left shift and right shift operations?
Left shifting a number by 1 bit multiplies a number by 2 and right shifting a
number by 1 bit divides a number by 2.
Left shift is also used while multiplying two binary numbers.
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
Left/right shift is also used in stepper motor control.
12. When is the CL register used with shift or rotate instructions?
When the number of bits to be shifted is more than one bit, CL register is used with
shift or rotate instructions
13. Consider the following pair of partial programs:
i) MOV AX, 4000H
ii)
MOV AX, 4000H
ADD AX, AX
ADD AX, AX
ADC AX, AX
RCL AX, 1
JZ DOWN
JZ DOWN
For each case, what is the data in AX after execution of third instruction and from
where does the processor fetch next instruction after execution of 4th instruction?
i) AX=C000H and the processor fetches the next instruction present after JZ DOWN
in the program as the ZF flag is 0 after execution of third instruction.
ii) AX= 0000H and the processor fetches the next instruction present after JZ DOWN
in the program as the ZF flag is not affected after execution of third instruction and
ZF flag is 0 after execution of second (i.e. ADD) instruction in the program.
14. How is the WAIT instruction used to coordinate the operation between 8086 and
8087?
When the 8086 executes the WAIT instruction, it checks the TEST pin which is
connected to the BUSY output of 8087. If the TEST pin is high, the 8086 keeps
waiting until TEST pin becomes low. As long as the 8087 executes the floating point
instruction, it keeps the BUSY pin high and hence the 8086 also waits. When the
floating point instruction is completely executed by 8087, it makes its BUSY pin low
and WAIT instruction in 8086 will be treated as NOP instruction.
Programming Exercises:
1. Write 8086 assembly language program to find the sum of 100 words present in
an array stored from the address 3000H:1000H in data segment and store the
result from the address 3000H:2000H.
Solution: If all the words in the array is equal to FFFFH (max. value of a word), the
result will be 63FF9CH. Hence the result will have maximum of 3 bytes.
MOV CX, 100
MOV AX, 3000H
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
MOV DS, AX
MOV BL,00H ; BL is used to accumulate the carry generated in the addition
MOV AX,0000H
MOV SI,1000H
L1: ADD AX, [SI]
JNC NO_CARRY
INC BL
NO_CARRY: ADD SI,02H
LOOP L1
MOV DI,2000H
MOV [DI],AX ; Storing lower word of the result in AX in memory
MOV [DI+2], BL
HLT
2. Write 8086 assembly language program to find the prime numbers among 100
bytes of data in an array stored from the address 4000H:1000H in the data
segment and store the result from the address 4000H:3000H.
Algorithm:
Each byte in the array (say N) is first compared with 1 and 2. If the byte ‘N’ is equal
to 1 or 2, it is stored as a prime number. If the byte ‘N’ in the array is above 2, then
the byte is divided from the value 2 to N-1 one by one and if the byte ‘N’ gets divided
by any one number during this operation, then it is not a prime number.
For example, if the byte is equal to 05H , then 05H is divided by 2, 3 and 4 one by
one. If the remainder is 0 in any one division then it is not a prime number.
MOV AX, 4000H
MOV DS,AX
MOV SI,1000H
MOV CX,100 ; CX= NO. OF BYTES IN THE ARRAY
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
MOV BX,3000H ; BX= OFFSET ADDRESS OF RESULT
CHECK: MOV AH,00
MOV AL,[SI] ; GET ONE DATA (=N) FROM ARRAY
CMP AL,01H
JZ PRIME; IF NUMBER IS EQUAL TO 1, GOTO PRIME
CMP AL,02H
JZ PRIME
DEC AL; FIND N-1
MOV DH, AL; STORE N-1 IN DH
MOV DL,02H ; START DIVIDING THE BYTE N FROM 2 TO N-1 ONE BY ONE
DIVIDE: MOV AH,00H
MOV AL,[SI] ; Get the byte N again in AL
DIV DL
CMP AH,00H; CHECK REMAINDER IS ZERO
JZ NEXT; IF ZERO THEN NOT A PRIME NUMBER, GOTO NEXT
INC DL
CMP DL,DH
JBE DIVIDE
PRIME: MOV AL,[SI] ; Get the byte N again in AL for storing
MOV [BX],AL
INC BX
NEXT: INC SI
LOOP CHECK
HLT
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
3. Write 8086 assembly language program to find the number of occurrences of the
character ‘A’ among 50 characters of a string type data stored from the address
5000H:1000H in data segment and store the result in the address 2000H:5000H.
MOV CX,50
MOV AX, 5000H
MOV ES,AX
MOV DI, 1000H
CLD
MOV BL,00H; BL is used to store number of occurrence of ‘A’
MOV AL, ‘A’
L1: SCASB
JNZ NO_MATCH
INC BL
NO_MATCH: LOOP L1
MOV AX,2000H
MOV DS,AX
MOV [5000H], BL
HLT
4. Write 8086 assembly language program to check whether the two strings, one
stored from the address 2000H:1000H in data segment and the other stored from
the address 2000H:3000H are equal or not . If they are equal, store the value 00H
in AL otherwise store the value 01H in AL.
Let us assume that both strings are having 5 characters each.
MOV AX, 2000H
MOV DS,AX
MOV ES,AX
MOV SI, 1000H
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
MOV DI,3000H
CLD
MOV CX,5
REPE CMPSB
JCXZ L1
L3: MOV AL,01H
JMP L2
L1: JNZ L3
MOV AL,00H
L2: HLT
5. Write 8086 assembly language program to find the number of bytes that have the
hexadecimal digit ‘F’ in its upper nibble among 100 bytes of data in an array
stored from the address 8000H:1000H in data segment and store the result in the
address 8000H:3000H.
MOV AX, 8000H
MOV DS,AX
MOV CX,100
MOV SI,1000H
MOV BX,3000H
AGAIN: AND [SI],F0H
CMP [SI], F0H
JNZ NEXT
INC BYTE PTR [BX]
NEXT: INC SI
LOOP AGAIN
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
HLT
6. Write 8086 assembly language program to complement the lower nibble of each
byte in 100 bytes of data in an array stored from the address 8000H:1000H in the
data segment and store the result from the address 8000H:3000H.
MOV AX, 8000H
MOV DS,AX
MOV CX,100
MOV SI,1000H
MOV BX,3000H
AGAIN: MOV AL,[SI]
XOR AL, 0FH
MOV [BX], AL
INC SI
INC BX
LOOP AGAIN
HLT
7. Write 8086 assembly language program to add two matrices having word type
data in each element of the matrix. Assume that each element of the result after
addition of the corresponding elements of the matrix is also word type data. The
data for one matrix is present in an array stored from the address 8000H:1000H in
the data segment and the corresponding data for another matrix is present in an
array stored from the address 8000H:2000H in data segment. The result is to be
stored from the address 7000H:1000H.
Let the matrices to be added are 2x2 matrices and the elements of first matrix
namely (1,1),(1,2), (2,1) and (2,2) are stored from the address 8000H:1000H
successively. Let the elements of second matrix namely (1,1),(1,2), (2,1) and (2,2)
are stored from the address 8000H:2000H successively. Let the elements of resultant
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
matrix namely (1,1),(1,2), (2,1) and (2,2) are stored from the address 7000H:1000H
successively.
MOV AX, 8000H
MOV DS,AX
MOV CX,4
MOV SI, 1000H
MOV BX, 2000H
AGAIN: MOV AX, [SI]
ADD AX, [BX]
MOV DX, 7000H
MOV DS, DX
MOV [SI], AX
MOV AX, 8000H
MOV DS,AX
ADD SI,02H
ADD BX,02H
LOOP AGAIN
HLT
8. Write 8086 assembly language program to multiply two square matrices having
byte type data for each element of the matrix. Assume that each element of the
resultant matrix is of word type. The data for one matrix is present in an array
stored from the address 8000H:1000H in the data segment and the corresponding
data for other matrix is present in an array stored from the address 8000H:2000H
in the data segment. The result is to be stored from the address 7000H:1000H.
Let the matrices to be multiplied are 2x2 square matrices namely A and B and the
resultant matrix is C. The elements of A and B matrices are stored in the
following addresses:
Element
A
B
© Oxford University Press 2011
C
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
(1,1)
8000H:1000H
8000H:2000H
7000H:1000H
(1,2)
8000H:1001H
8000H:2001H
7000H:1002H
(2,1)
8000H:1002H
8000H:2002H
7000H:1004H
(2,2)
8000H:1003H
8000H:2003H
7000H:1006H
MOV AX,8000H
MOV DS,AX
MOV SI, 1000H
MOV BX,2000H
MOV AL, [SI] ; To find element (1,1) of C
MUL [BX]
MOV CX, AX
MOV AL, [SI+1]
MUL [BX+2]
ADD AX,CX
PUSH DS
MOV DX, 7000H
MOV DS, DX
MOV [SI], AX
POP DS
MOV AL, [SI] ; To find element (1,2) of C
MUL [BX+1]
MOV CX, AX
MOV AL, [SI+1]
MUL [BX+3]
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
ADD AX,CX
PUSH DS
MOV DX, 7000H
MOV DS, DX
MOV [SI+2], AX
POP DS
MOV AL, [SI+2] ; To find element (2,1) of C
MUL [BX]
MOV CX, AX
MOV AL, [SI+3]
MUL [BX+2]
ADD AX,CX
PUSH DS
MOV DX, 7000H
MOV DS, DX
MOV [SI+4], AX
POP DS
MOV AL, [SI+2] ; To find element (2,2) of C
MUL [BX+1]
MOV CX, AX
MOV AL, [SI+3]
MUL [BX+3]
ADD AX,CX
MOV DX, 7000H
MOV DS, DX
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
MOV [SI+6], AX
HLT
9. Write 8086 assembly language program to find the factorial of the given byte of
data using recursive algorithm. The result is to be stored at the address
7000H:1000H.
The factorial of 0 or 1 is equal to 1. In general, the factorial of a number ‘n’ is
obtained by multiplying ‘n’ with the factorial of (n-1). The factorial of the number
(n-1) is obtained by multiplying (n-1) with the factorial of (n-2). This is repeated
until the factorial of one has to be found which is equal to 1. For example to find
factorial of 3 which is denoted by 3!, the following steps are done:
3! = 3 x 2! = 3 x (2 x 1!) = 3 x 2 x 1 = 6
The following program finds the factorial of the number ‘n’ which is any one
number between 0 to 8 (since its result is 16 bits) using the above technique. It
uses stack and BP register to store and retrieve the temporary results obtained
while executing the same factorial subroutine again and again which is known as
recursive procedure.
MOV AX, n ; Load AX with the number ‘n’ whose factorial has to be found
SUB SP,02; make space for one word output (factorial value)
PUSH AX ; push input parameter ‘n’ into stack
CALL FACT ; call the FACT recursive subroutine
ADD SP,02; clear the stack of the input (i.e. to undo the PUSH AX above)
POP AX; get the result (i.e. factorial value) in AX
HLT ; Halt
;recursive procedure FACT starts here
FACT: PUSH BP ;BP is the frame pointer (defaulting to stack segment), so save its old
value belonging to the calling process and make space to have its new
value
MOV BP,SP; BP is now the (new) frame pointer for the subroutine
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
PUSH AX ; stack space of subroutine is used to save registers AX,BX,DX
PUSH BX
PUSH DX
MOV BX, [BP+4]; get the value of ‘n’ which is at [BP+4] in BX
MOV AX,01; prepare for checking termination condition
CMP AX,BX; check for termination
JAE TERMI; If AX is above or equal to BX, (i.e. if n= 0 or 1) go to label TERMI
; the result is already in register AX.
DEC BX ; else prepare to recursively call FACT (n-1)
SUB SP,2; make space for output of the procedure for FACT (n-1)
PUSH BX; input to the procedure to find FACT (n-1)
CALL FACT ; recursive call
ADD SP,2; discard the input variable of the called procedure
POP AX; get FACT (n-1) in AX, output of the called procedure
MOV BX,[BP+4]; get the input variable to this procedure, n, in BX.
MUL BX; The product n* FACT (n-1) goes to DX:AX, but DX will be 0 since
the result is 16 bits only.
TERMI: MOV [BP+6], AX; store the result in AX in the output space provided in the
stack frame. In case n is 1 or 0, we directly come here with
the result 1 in AX.
POP DX ; retrieve the saved registers from stack and clean the procedure stack
POP BX
POP AX
MOV SP,BP; ensure that stack is clean
POP BP
; get back the original BP
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
RET; procedure over, go back to the calling program
10. Write a non-recursive assembly language subroutine of 8086, to evaluate the
number
Fn = Fn-1 + Fn-2 for any given n > 1
given that F0 = 0 and F1 = 1. Consider the number n to be such that Fn is not more
than a 16 bit number.
The result will be within 16 bits only when the value of n is 24. Hence the
maximum value of n is 24 in this program. The logic used in the program is as
follows:
First according to the value of ‘n’, the function value from F0, F1, F2,….. up to Fn-1
are calculated and successively stored in memory from the address 1000H:1000H
in the form of words. Then the last two words in the memory (i.e. Fn-1 and Fn-2)
are added to get the result.
MOV CX, n ; load CX with the value of n
MOV BX,1000H ; initialize segment and offset address registers
MOV DS,BX
MOV WORD PTR [BX], 00H ; store the value of F0
ADD BX,02
MOV WORD PTR [BX], 01H ; store the value of F1
SUB CX, 02 ; check whether n is equal to 2
JZ ZERO ; if cx=n=2 then go to label ZERO to perform addition of F0 & F1
;else find the remaining values of F using following loop L1
L1:
MOV AX, [BX] ; get last calculated value of F
ADD AX, [BX-2] ; add it with the previously calculated value of F
ADD BX,2 ; Increment pointer BX to store next value of F in memory
MOV [BX],AX ; store the newly calculated function value
DEC CX ; decrement CX
JNZ L1; if CX is not zero, find the next value of F by jumping to L1
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
ZERO: MOV BX,1000H ; initialize BX to point the first location
MOV CX, n ; load CX with the value of n
DEC CX ; decrement CX by 1
ADD CX,CX ; multiply CX by 2 since each function is stored in the form of
;word
ADD BX, CX ; add BX with CX to get the location of Fn-1
MOV AX, [BX] ; get the value of Fn-1 in AX
ADD AX, [BX-2] ; add Fn-1 and Fn-2 to get Fn which is in AX
HLT ; Stop
11. Solve problem 1 assuming that the program is to be assembled by an assembler.
The problem is to write 8086 assembly language program to find the sum of 100
words present in an array stored in data segment and store the result in data
segment assuming that the program is to be assembled by an assembler.
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
ARRAY DW 2000H, 3000H, 5000H,….. ; 100 WORDS ARE STORED HERE
RESULT_LSW DW 0000H ; RESERVE SPACE FOR STORING RESULT
RESULT_MSB DB 00H
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS,AX
MOV CX, 100
MOV BL,00H ; BL is used to accumulate the carry generated in the addition
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
MOV AX,0000H
MOV SI, OFFSET ARRAY
L1: ADD AX, [SI]
JNC NO_CARRY
INC BL
NO_CARRY: ADD SI,02H
LOOP L1
MOV RESULT_LSW, AX ; Storing lower word of the result in AX in memory
MOV RESULT_MSB, BL ; Storing the upper byte of the result in BL in memory
MOV AH,4CH
INT 21H
CODE ENDS
END START
12. Solve problem 7 assuming that the program is to be assembled by an assembler.
Let the matrices to be added are 2x2 matrices and the elements of first matrix
namely (1,1),(1,2), (2,1) and (2,2) are first stored successively in the data segment.
Let the elements of second matrix namely (1,1),(1,2), (2,1) and (2,2) are stored next
successively in the data segment. This is followed by memory space for storing the
elements of resultant matrix namely (1,1),(1,2), (2,1) and (2,2).
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
MATRIX_A DW 2000H, 3000H, … ; Elements of Matrix A are stored here
MATRIX_B DW 1000H, 3000H… ; Elements of Matrix B are stored here
RESULT_MATRIX DW 4 DUP (0)
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
MOV DS,AX
MOV CX,4
MOV SI, OFFSET MATRIX_A
MOV BX, OFFSET MATRIX_B
MOV DI, OFFSET RESULT_MATRIX
AGAIN: MOV AX, [SI]
ADD AX, [BX]
MOV [DI], AX
ADD SI,02H
ADD BX,02H
ADD DI,02H
LOOP AGAIN
MOV AH,4CH
INT 21H
CODE ENDS
END START
13. Solve problem 10 assuming that the program is to be assembled by an assembler.
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
ARRAY DW 25 DUP (0); reserve 25 words to store different function values
RESULT DW 1 DUP (0); reserve one word for storing result
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
MOV DS,AX
MOV CX, n ; load CX with the value of n
MOV BX, OFFSET ARRAY ; initialize segment and offset address registers
MOV WORD PTR [BX], 00H ; store the value of F0
ADD BX,02
MOV WORD PTR [BX], 01H ; store the value of F1
SUB CX, 02 ; check whether n is equal to 2
JZ ZERO ; if cx=n=2 then go to label ZERO to perform addition of F0 & F1
;else find the remaining values of F using following loop L1
L1:
MOV AX, [BX] ; get last calculated value of F in AX
ADD AX, [BX-2] ; add it with the previously calculated value of F
ADD BX,2 ; Increment pointer BX to store next value of F in memory
MOV [BX],AX ; store the newly calculated function value
DEC CX ; decrement CX
JNZ L1; if CX is not zero, find the next value of F by jumping to L1
ZERO: MOV BX, OFFSET ARRAY ; initialize BX to point the first location
MOV CX, n ; load CX with the value of n
DEC CX ; decrement CX by 1
ADD CX,CX ; multiply CX by 2 since each function is stored in the form of
;word
ADD BX, CX ; add BX with CX to get the location of Fn-1
MOV AX, [BX] ; get the value of Fn-1 in AX
ADD AX, [BX-2] ; add Fn-1 and Fn-2 to get Fn
© Oxford University Press 2011
Microprocessors and Microcontrollers
Kumar, Saravanan & Jeevananthan
MOV RESULT, AX ; store AX in the location RESULT
MOV AH,4CH
INT 21H
CODE ENDS
END START
© Oxford University Press 2011
Download