UNIT 3 Computer Organization
UNIT 3 Computer Organization
Stack structure of 8086, Interrupts and Interrupt service routines, Interrupt cycle of 8086,
Interrupt programming, Passing parameters to procedures, Macros, Timings and Delays.
Lecture
Topic to be covered Student Learning Outcomes URLs
Number
http://www.nptel.ac.in/courses/
Webcourse-contents/IISc-
Student will be able to write BANG/Microprocessors%20
1. Machine level programs
machine level programs. and%20Microcontrollers/pdf/,
https://www.youtube.com/
watch?v=HXYhBCpDoVc
http://www.nptel.ac.in/courses/
Machine coding the Student will be able to cod e the Webcourse-contents/IISc-
2.
programs ALP for various problems. BANG/Microprocessors%20
and%20Microcontrollers/pdf/,
Programming with an http://www.nptel.ac.in/courses/
assembler, Assembly Student will be able to work with Webcourse-contents/IISc-
3.
Language example Assembler. BANG/Microprocessors%20
programs and%20Microcontrollers/pdf/
Student will be able to explain
https://www.youtube.com/
4. Stack structure of 8086 functioning of stack for 8086
watch?v=d-2Peb3pCBg
processor.
https://www.youtube.com/
Student will be able to watch?v=C02weCM9yWA
Interrupts and Interrupt
5. differentiate different types of http://www.dauniv.ac.in/
service routines
interrupts. downloads/EmbsysRevEd_
PPTs/
https://www.youtube.com/
watch?v=C02weCM9yWA
Student will be able to
6. Interrupt cycle of 8086 http://www.dauniv.ac.in/
demonstrate interrupt cycle.
downloads/EmbsysRevEd_
PPTs/
https://www.youtube.com/
Student will be able to develop watch?v=C02weCM9yWA
7. Interrupt programming various ALP programs using an http://www.dauniv.ac.in/
interrupt service routines downloads/EmbsysRevEd_
PPTs/
Passing parameters to Student will be able to explain https://www.youtube.com/
8.
procedures parameter passing techniques. watch?v=K4YMXyRcWaI
Macros, Timings and Student will be able to analyze the https://www.youtube.com/
9.
Delays. timing delays in 8086 processor. watch?v=K4YMXyRcWaI
110 Dept. of CSE
Practical Inferences
Develop/ Write Assembly Language Programs to solve various problems.
Notes:
3. Assembly language program with 8086
3.1. A Few Machine Level Programs:
A few machine level programming examples, rather, instruction sequences
are presented for comparing the 8085 programming with that of 8086.
This programs are in the form of instructions sequences just like 8085
programs.
These may even be hand coded, entered byte by byte and executed on an 8086
based system due to complex instruction set of 8086 and its tedious opcode
conversion procedure, most of the programmers prefer to use assemblers.
Example 3.1
Write a program to add a data byte located at offset 0500H in 2000H segment to
another data byte available at 0600H in the same segment and store the result at
0700H in the same segment.
Solution:
The flow chart for this problem may be drawn as shown below.
Start
MOV AX.2000H : Initializing DS with value
MOV DS,AX : 2000H Initialize Segment Register
MOV AX,[500H] : Get first data byte from 0500H
Offset Get content of 0500H in a
G.P. Register
ADD AX,[600H] : Add this to the second byte
From 0600H
Perform addition
MOV [700H], AX : Store AX in 0700H (result).
HLT : Stop
Store result in 0700H
Stop
Example 3.1.2:
MOV BL, CL
For hand coding this instruction, we will have to first note down the following
features:
(i) It fits in the register/Memory to/from register format.
(ii) It is an 8-bit operation.
(iii) BL is the destination register and CL is the source register.
Now from the feature (i) the opcode format is given as shown.
If d=1, then transfer of data is to the register shown by the REG field, i.e., the
destination is a register (REG).
Id d=0, the source is a register shown by the REG field.
112 Dept. of CSE
It is an operation, hence w bit is 0. If it had been a 16-bit operation, the w bit would
have been 1.
Refer to addressing modes, to search the REG to REG addressing in it, i.e., the last
column with MOD 11. According to the data sheet when MOD is 11, the R/M field
is treated as a REG field. The REG field is used for source register and the R/M
field is used for the destination register, if d is 0. If d=1, the REG field is used for
destination and the R/M field is used to indicate source.
Now the complete machine code of this instruction comes out to be
MOV BL, CL
D7 D0 D7 D0
Code d w MOD REG R/M
100010 w 0 11 001 011
3.2.1 Finding out Machine Code for Conditional JUMP (Intrasegment) Instructions:
• To each of the conditional jump instructions, the first byte of the opcode is
fixed and the jump displacement must be less than or equal to 127(D) bytes
and greater than or equal to -128(D).
• The following example explains how to find the displacement.
• The displacement is an 8-bit signed number. If it is positive, it indicates a
forward jump, otherwise it indicates a backward jump.
• The following example is a sequence of instructions rather than a single
instruction to elaborate the procedure of the calculation of positive
displacement for a forward jump.
Example 3.1.2.1
2000 , 01 XOR AX,BX
2002,03 JNZ OK
2004 NOP
2005 NOP
2006 , 7, 8, 9 ADD BX, 05H
200A OK : HLT
• The above sequence shows that the programmer wants a conditional jump to
label OK, if the zero flag is not set.
• For finding out the displacement corresponding to the label OK, subtract the
address of the jump instruction (2002H), from the address of label (200AH).
• The required displacement is 200AH – 2002H = 08H. The 08H is the
displacement for the forward jump.
113 Computer Organization
Let us find out the displacement for a backward jump. Consider the following
sequence of instructions.
Example 3.1.2.2
2000, 01, 02 MOV CL, 05H
2003 Repeat : INC AX
2004 DEC CL
2005, 2006 JNZ Repeat
• For finding out the backward displacement, subtract the address of the label
(repeat) from the address of the jump instruction. Complement the subtraction.
• The lower byte gives the displacement .
• In the above example, the signed displacement for the JNZ instruction comes
out to be (2005H – 2003H = 02, complement – FDH) The magnitude of the
displacement must be less than or equal to 127(D).
• The MSB of the displacement decides whether it is a forward or backward
jump. If it is 1, it is a backward jump or else it is a forward jump.
A similar procedure is used to find the displacement for intra segment short calls.
3.2.2 Finding out Machine Code for Unconditional JUMP Intra segment:
• For this instruction there are again two types of jump, i.e., short jump and
long jump.
• The displacement calculation procedures are again the same as given in case
of the conditional jump.
• The only new thing here is that, the displacement may be beyond -+127(D).
• This type of jump is called the long jump. The method of calculation of the
displacement is again similar to that for short jump.
3.2.3 Finding out Machine Code for Inter segment Direct jump:
• This type of instruction is used to make a jump directly to the address lying in
another segment. The opcode itself specifies the new offset and the segment
of jump address, directly.
Example 3.1.2.3
JUMP 2000 : 5000
This instruction implies a jump to a memory location in another code segment with
CS = 2000H and offset = 5000H. The code formation is as shown:
Code 1110 1010 0000 0000 0101 0000 0000 0000 0010 0000
Formation Opcode Offset LB Offset HB Seg. LB Seg. HB
114 Dept. of CSE
• The opcode forms the first byte of this instruction and the successive bytes are
formed from the segment and the offset of the jump destination.
• While specifying the segment and offset, the lower byte (LB) is specified first
and then the higher byte (HB) is specified.
• Finally, the opcode comes out to be EA 00 50 00 20. The procedure of coding
the CALL instructions is similar.
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA : Initialize data segment
MOV DS, AX :
MOV AX, OPR1 : Take 1st operand in AX
MOV BX, OPR2 : Take 2nd operand in BX
CLC : CLEAR PREVOIUS CARRY IF ANY
ADD AX, BX : Add BX to AX
MOV DI, OFFSET RESULT : Take offset of result in DI
MOV [DI], AX : Store the result at memory address in DI
MOV AH, 4CH : Return to DOS prompt
INT 21H
CODE ENDS : CODE segment ends
END START : Program ends
116 Dept. of CSE
• he 8086 PUSHes data and addresses to the stack one word at a time. Each
T
time a register value is to be PUSHed onto the top of the stack, the value in
the stack pointer is first decremented by 2 and then the contents of the register
are written into memory.
• In this way, we see that the stack grows down in memory from the bottom
of the stack, which corresponds to the physical address derived from SS
and 0FFFFh toward the end of the stack, which corresponds to the physical
address obtained from SS and offset 000016.
• When a value is popped from the top of the stack, the reverse of this sequence
occurs. The physical address defined by SS and SP always points to the
location of the last value pushed onto the stack.
• Its contents are first popped off the stack and put into the specified register
within the 8086; then SP is incremented by 2. The top of the stack now
corresponds to the previous value pushed onto the stack.
• A few things that we must remember are:
• We must make sure to create a Stack, and make it large enough for any
program that has a CALL or any program that will use the PUSH and POP
instructions.
o PUSH all registers that contain data or addresses that we need after the
RETurn
o When w RETurn we must POP the registers in the reverse order of the
PUSH
o We must POP each register that we PUSH
o If we PUSH before the CALL, we must after the RETurn (in the calling
module or subroutine)
o If we PUSH after the CALL, we must POP before the RETurn (inside the
subroutine) PUSH Examples
PUSH AX – the contents of a 16-bit register PUSH EBX - the
contents of a 32-bit register
PUSHA (286 and higher) – Preserves all usable registers of 80286
PUSHAD (386 and higher) – Preserves all usable registers of 80386
• here are corresponding POP instructions for each of the above examples.
T
Execution of a PUSH instruction causes the data corresponding to the operand
to be pushed onto the top of the stack.
SP <- SP - 1 ; SP is decremented
SS:SP <= AH ; AH is PUSHed on the Stack
SP <- SP - 1 ; SP is decremented
SS:SP <= AL ; AL is PUSHed on the Stack
• This shows that the two bytes of AX are saved in the stack part of memory and
the stack pointer is decremented by 2 such that it points to the new top of the
stack. On the other hand, if the instruction is POP AX, its execution results in
the following:
AL <- SS:SP ; AL is POPped from the Stack
SP <- SP + 1 ; SP is incremented
AH <= SS:SP ; AH is POPped from the Stack
SP <- SP + 1 ; SP is incremented
Example 3.2.1
Let us assume we wish to write a program that CALLs a subroutine that will need
all the general purpose registers, and those registers contain data that we will need
after the subroutine has finished. We may preserve the contents of the registers
using the PUSH instruction either by:
PUSHing before the CALL and POPping after the RETurn or
PUSHing after the CALL and POPping before the RETurn
Regardless of which method we use, we must POP in the reverse order of the
PUSH. Consider the following.
PUSH AX
PUSH BX
PUSH CX
PUSH DX
CALL SUB1
POP DX
POP CX
POP BX
POP AX
The above code will preserve the registers on the Stack, and execute the subroutine,
which PUSHes the IP onto the Stack. The last instruction of the subroutine is the
RETurn which POPs the contents of the Stack into the IP, and then we restore the
registers with the POP instruction. The above code will perform this correctly.
Notice the register operand of the first POP corresponds to the register operand
of the last PUSH, while the register operand of the last POP corresponds to the
register operand of the first PUSH.
119 Computer Organization
Example 3.2.2
Write a program to change a sequence of sixteen 2-byte numbers from ascending
to descending order. The numbers are stored in the data segment. Store the new
series at addresses starting from 6000H. Use the LIFO property of the stack.
Solution:
ASSUME CS : CODE, DS : DATA, SS : DATA
DATA SEGMENT
LIST DW 10H
STACKDATA DB FFH DUP (?)
0RG6000H
RESULT DW 10H
DATA ENDSCOUNT EQU 10H
CODE SEGMENT
START: MOV AX, DATA ; Initialize data segment and
MOV DS, AX ; Stack segment
MOV SS, AX
120 Dept. of CSE
Example 3.2.2
• Suppose an external device interrupts the CPU at the interrupt pin, either NMI
or INTR of 8086, while the CPU is executing an instruction of a program. The
CPU first completes the execution of the current instruction. The IP is then
incremented to point to the next instruction.
99 The CPU then acknowledges the requesting device on its INTA pin
immediately if it is a NMI , TRAP or Divide by Zero interrupt. If it is
an INT request, the CPU checks the IF flag. If the IF is set, the interrupt
request is acknowledged using the OOA pin.
99 If the IF is not set, the interrupt requests are ignored. Note that the
responses to the NMI, TRAP and Divide-by-Zero interrupt requests are
independent of the IF flag.
99 After an interrupt is acknowledged, the CPU computes the vector address
from the type of the interrupt that may be passed to the interrupt structure
of the CPU internally (in case of software interrupts, NMI, TRAP and
Divide by Zero interrupts) or externally, i.e. from an interrupt controller
in case of external interrupts. (The contents of IP and CS are next pushed
to the stack.
99 The contents of IP and CS now point to the address of the next instruction
of the main program from which the execution is to be continued after
executing the ISR. The PSW is also pushed to the stack). The interrupt
flag (IF) is cleared.
99 The TF is also cleared, after every response to the single step interrupt.
The control is then transferred to the interrupt service routine for serving
the interrupting device. The new address of ISR is found out from the
interrupt vector table.
99 The execution of the ISR starts. If further interrupts are to be responded
to during the time the first interrupt is being serviced, the IF should again
be set to 1 by the ISR of the first interrupt.
122 Dept. of CSE
99 If the interrupt flag is not set, the subsequent interrupt signals will not
be acknowledged by the processor, till the current one is completed. The
programmable interrupt controller is used for managing such multiple
interrupts based on their priorities.
99 At the end of ISR the last instruction should be IRET. When the CPU
executes IRET , the contents of flags, IP and CS which were saved at
the start by the CALL instruction are now retrieved to the respective
registers. The execution continues onwards from this address, received
by IP and CS.
• At the end of each instruction cycle. the 8086 checks to see if any interrupts
have been requested. If an interrupt has been requested, the 8086 responds to
the interrupt by stepping through the following series of major actions.
99 It decrements the stack pointer by 2 and pushes the flag register on the
stack.
99 It disables the 8086 INTR interrupt input by clearing the interrupt flag
(IF) in the flag register.
99 It resets the trap flag (TF) in the flag register.
99 It decrements the stack pointer by 2 and pushes the current code segment
register contents on the stack.
99 It decrements the stack pointer again by 2 and pushes the current
instruction pointer contents on the stack.
99 It does an indirect far jump to the start of the procedure you wrote to
respond to the Interrupt.
Following figure summarizes these steps in diagram form. As you can see, the 8086
pushes the flag register on the stack disables the INTR input and the single-step
function, and does essentially an indirect far call to the interrupt service procedure.
An IRET instruction at the end of the interrupt service procedure returns execution
to the main program.
• We now discuss about how the 8086/88 finds out the address of an ISR. Every
external and internal interrupt is assigned with a type (N), that is either implicit
(in case of NMI, TRAP and divide by zero) or specified in the instruction INT
N (in case of internal interrupts).
123 Computer Organization
• Suppose an external signal interrupts the processor and the pin LOCK goes
low preventing the use of bus for any other purpose. The pin LOCK goes
low at the trailing edge of the first ALE pulse that appears after the interrupt
signal. The pin LOCK remains low till the start of the next machine cycle.
• With the trailing edge of LOCK, the OOA goes low and remains low for two
clock states before returning back to the high state. It remains high till the start
of the next machine cycle, i.e. next trailing edge of ALE.
• Then OOA again goes low, remains low for two states before returning to the
high state. The first trailing edge of ALE floats the bus ADO-AD7 , while the
second trailing edge prepares the bus to accept the Type of the interrupt. The
Type of the interrupt remains on the bus for a period of two cycles.
Example 3.2.3.1:
To read a string form the key board and convert the characters into upper case
letters and display on the screen with enough messages displayed in between.
Solution:
data segment
code segment
assume cs:code, ds:data
start: mov ax,data
mov ds,ax
lea dx,msg1
mov ah,09h
int 21h
mov bx,offset str
mov str[bx],0ah
inc bx
up: mov ah,01
int 21h
cmp al,0Dh
je stop
cmp al,60H
jc dwn
sub al,20H
dwn: mov [bx],al
inc bx
jmp up
stop: mov str[bx],’$’
mov dx, offset msg2
mov ah,09h
int 21h
mov dx,offset str
mov ah,09h
int 21h
mov ah,4ch
int 21h
code ends
end start
G enerally, the following techniques are used to pass input data / parameter to
procedures in assembly language programs.
o Using global declared variable
o Using registers of CPU architecture
o Using memory locations (reserved)
o Using Stack
o Using PUBLIC & EXTRN.
Besides these methods if a procedure is interactive it may directly accept inputs
from input devices.
Example 3.1:
ASSUME CS : CODE1, DS : DATA
DATA SEGMENT
NUMBER EQU 77H GLOBAL
DATA ENDS
CODE1 SEGMENT
START : MOV AX, DATA
MOV DS, AX
.
.
MOV AX,NUMBER
.
CODE1 ENDS
ASSUME CS : CODE2
CODE2 SEGMENT
MOV AX,DATA
MOV DS,AX
MOV BX,NUMBER
CODE2 ENDS
END START
• The CPU general purpose registers may be used to pass parameters to the
procedures. The main program may store the parameters to be passed to the
procedure in the available CPU registers and the procedure may use the same
register contents for execution.
• The original contents of the used CPU register may change during execution
of the procedure. This may be avoided by pushing all the register content to
be used to the stack sequentially at the start of the procedure and by poping
all the register contents at the end of the procedure in opposite sequence.
128 Dept. of CSE
Example 3.2
ASSUME CS : CODE
CODE SEGMENT
START : MOV AX, 5555H
MOV BX, 7272H
.
.
CALL PROCEDURE 1
.
.
PROCEDURE PROCEDURE1 NEAR
ADD AX,BX
RET
PROCEDURE1 ENDP
CODE ENDS
END START
• Memory locations may also be used to pass parameters to a procedure in the
same way as registers. A main program may store the parameter to be passed
to a procedure at a known memory address location and the procedure may
use the same location for accessing the parameter.
Example 3.1.1
A program with more than one segment
129 Computer Organization
Solution:
ASSUME CS : CODE1, DS: DATA1
CODE1 SEGMENT
START : MOV AX, DATA1
MOV DS, AX
.
.
CODE1 ENDS
ASSUME CS : CODE2, DS : DATA2
CODE2 SEGMENT
MOV AX, DATA2
MOV DS, AX
.
.
CODE2 ENDS
DATA1 SEGMENT
DATA1 ENDS
DATA2 SEGMNET
DATA2 ENDS
END START
Example 3.1.2 :
A program with more than one intersegment routine.
Solution :
ASSUME CS : CODE1 , DS : DATA1
DATA SEGMENT
DATA ENDS
CODE1 SEGMENT
START : MOV AX, DATA
MOV DS, AX
3.10 Macros
• The macro is also a similar concept. Suppose, a number of instructions are
repeating through in the main program, the listings becomes lengthy. So a
macro definition i.e., a label, is assigned with the repeatedly appearing string
of instructions.
• The process of assigning a label or macro name to the string is called defining
a macro.
• A macro within a macro is called a nested macro. The macro name or macro
definition is then used throughout the main program to refer to that string of
instructions.
Defining a Macro:
• A MACRO can be defined anywhere in a program using the directives
MACRO and ENDM. The label prior to MACRO is the macro name which
should be used in the actual program. The ENDM directive marks the end of
the instructions or statements sequence assigned with the macro name.
The following macro DISPLAY displays the message MSG on the CRT. The
syntax is as given:
DISPLAY MACRO
MOV AX, SEG MSG
MOV DS, AX
MOV DX, OFFSET MSG
MOV AH, 09 H
INT 21 H
131 Computer Organization
ENDM
• The above definition of a macro assigns the name DISPLAY to the instruction
sequence between the directives MACRO and ENDM. While assembling, the
above sequence of instructions will replace the label ‘DISPLAY’ , whenever
it appears in the program.
• A macro may also be used in a data segment. In other words, a macro may
also be used to represent statements and directives. The concept of macro
remains the same independent of its contents.
The following example shows a macro containing statements. The macro
defines the strings to be displayed.
STRINGS MACRO
MSG1 DB 0AH, 0DH, “Program terminated normally”, 0AH, 0DH, “$”
MSG2 DB 0AH, 0DH, “Retry , Abort, Fail” , 0AH, 0DH, “$”
ENDM
• A macro may be called by quoting its name, along with any values to be
passed to the macro. Calling a macro means inserting the statements and
instructions represented by the macro directly at the place of the macro name
in the program.
Example:
The DISPLAY macro written in above can be made to display two different
messages MSG1 and MSG, AS SHOWN
DISPLAY MACRO MSG
MOV AX, SEG MSG
MOV DS , AX
MOV DX, OFFSET MSG
MOV AH, 09 H
INT 21 H
ENDM
This parameter MSG can be replaced by MSG1 and MSG2 while calling the macro.
Assignment Questions
1. Write an ALP to convert a four digit hexadecimal number into decimal number.
2. Write an ALP find out transpose of a matrix.
3. Write an ALP to set and get the system time.
4. Describe the procedure for coding the intra segment and intersegment jump and
call instructions.
5. Explain the stack structure of 8086 in detail.
6. What is interrupt vector table of 8086? Explain its structure.
7. How do you pass parameters to macro. Explain?
8. Write an ALP to calculate the hexadecimal factorial of a one digit hexadecimal
number?
Review Questions
1. Write an ALP to find square root of a two digit number.
2. Write an ALP to sort list of numbers in ascending order.
3. Write an ALP to find average of a given string of data bytes.
4. Write an ALP to display message “Happy Birthday” on the screen after a key ‘A’
is pressed.
134 Dept. of CSE