Stack Structure
Stack Structure
STACK
The stack is a block of memory that may be used for temporarily storing the contents of the
registers inside the CPU. It is a top-down data structure whose elements are accessed using the
stack pointer (SP) which gets decremented by two as we store a data word into the stack and
gets incremented by two as we retrieve a data word from the stack back to the CPU register.
The process of storing the data in the stack is called ‘pushing into’ the stack and the
reverse process of transferring the data back from the stack to the CPU register is known as
‘popping off’ the stack. The stack is essentially Last-In-First-Out (LIFO) data segment. This
means that the data which is pushed into the stack last will be on top of stack and will be popped
off the stack first.
STACK STRUCTURE OF 8086
Stack contains a set of sequentially arranged data types, with the last item appearing on top of
the stack. This item will be popped off the stack first for use by the CPU. The stack pointer is
a 16-bit register that contains the offset address of the memory location in the stack segment.
The stack segment, like any other segment, may have a memory block of a maximum of 64
Kbytes locations, and thus may overlap with any other segments. Stack Segment register (SS)
contains the base address of the stack segment in the memory.
The Stack Segment register (SS) and Stack pointer register (SP) together address the stack-top
as explained below:
Let the content of SS be 5000H and the content of the stack pointer register be 2050H.To find
the current stack top address ,the stack segment register content is shifted left by four bit
positions (multiplied by 10H) and the resulting 20 bit content is added with 16 bit offset value
,stored in the stack pointer register.
If the stack top points to a memory location 52050H, it means that the location 52050H is
already occupied with the previously pushed data. The next 16 bit push operation will
decrement the stack pointer by two, so that it will point to the new stack-top 5204EH and the
decremented contents of SP will be 204EH. This location will now be occupied by the recently
pushed data.
Thus for a selected value of SS, the maximum value of SP=FFFFH and the segment can have
maximum of 64K locations. If the SP starts with an initial value of FFFFH, it will be
decremented by two whenever a 16-bit data is pushed onto the stack. After successive push
operations, when the stack pointer contains 0000H, any attempt to further push the data to the
stack will result in stack overflow.
After a procedure is called using the CALL instruction, the IP is incremented to the next
instruction. Then the contents of IP, CS and flag register are pushed automatically to the stack.
The control is then transferred to the specified address in the CALL instruction i.e. starting
address of the procedure. Then the procedure is executed.
If the stack top points to a memory location 52050 H it means that the location 52050H is
already occupied ie, Previously pushed data is available at 52050 H. The next 16-bit push
operation will decrement the stack pointer by two, so that it will point to the new stack top
5204E H, and the decremented contents of SP will be 204E H. This location will now be
occupied by the recently pushed data. Thus if a 16-bit data is pushed onto the stack ,the push
operation will decrement the SP by two because two locations will be required for a 2-byte data
The maximum value of SP =FFFF H and segment can have maximum of 64K locations. Thus
after starting with an initial vale of FFFF the stack pointer is decremented by two whenever a
16 bit data is pushed into the stack. After successive push operation when the stack pointer
contains 0000 H any attempt to further push the data to the stack will result in stack overflow
Each PUSH operation decrements SP by two, while each POP operation increments the SP.The
POP operation is used to retrieve the data stored on to the stack. Fig 4.2 shows the stack
overflow conditions while Fig 4.3 shows the effect of push and pop operations on the stack
memory block.
Programming using stack
The 8086 has four segement registers namely CS ,DS,SS,and ES.Out of these segments SS
contains the segment value for stack while SP contains the offset for the stack -top Ina
program the stack segment can be defined in a similar way as the data segment .The
ASSUME directives directs the name of stack segment to assembler.
Write a program to calculate squares of BCD numbers 0to 9 and store them sequentially from
2000H offsets onwards in the current data segment. Write a subroutine for the calculation of
the square of a number
INTERRUPTS IN 8086
While the CPU is executing a program, an interrupt breaks the normal execution of instructions,
diverts its execution to some other program called Interrupt Service Routine (ISR) Whenever
an interrupt occurs the processor completes the execution of the current instruction and starts
the execution of an Interrupt Service Routine (ISR) or Interrupt Handler.
ISR is a program that tells the processor what to do when the interrupt occurs. At the end of
the ISR the last instruction should be IRET. After the execution of ISR, control returns back to
the main routine where it was interrupted.
• Whenever a number of devices interrupt a CPU at a time, and if the processor is able to
handle them properly, it is said to have multiple interrupt processing capability.
• There are two interrupt pins in 8086. NMI and INTR
Need for Interrupt: Interrupts are particularly useful when interfacing I/O devices that provide
or require data at relatively low data transfer rate.
NMI
It is a single non-maskable interrupt pin (NMI) having higher priority. When this interrupt is
activated,
HARDWARE INTERRUPTS
Hardware interrupts are generated by hardware devices when something unusual happens; this
could be a key-press or a mouse move or any other action.
It can be divided into two
1. Maskable 2. Non maskable
• Maskable Interrupts:There are some interrupts which can be masked (disabled)or
enabled by the processor.
• Non-Maskable Interrupts:There are some interrupts which cannot be masked out
or ignored by the processor. These are associated with high priority tasks which
cannot be ignored (like memory parity or bus faults).
SOFTWARE INTERRUPTS
Interrupts are generated by a software instruction and operate similarly to a jump or branch
instruction.
• 256 interrupts : INT n is invoked as software interrupts- n is the type no in the range 0
to 255(00 to FF)
Type 0 to Type4 (Dedicated Interrupts or Predefined Interrupts )
- TYPE 0 interrupt represents division by zero situation.
- TYPE 1 interrupt represents single-step execution during the debugging of a program.
- TYPE 2 interrupt represents non-maskable NMI interrupt.
- TYPE 3 interrupt represents break-point interrupt.
- TYPE 4 interrupt represents overflow interrupt.
Type 5 to 31(Not used by 8086, reserved for higher processor like 80286,80386….
Suppose an external device interrupts the CPU at the interrupt pin either NMI or INTR of the
8086,while 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 .The CPU
acknowledges the requesting device on its INTA pin. If it is an INT request ,the CPU checks
the IF flag. If the IF is set ,the interrupt request is acknowledged using INTA pin .If the IF is
not set, the interrupt requests are ignored.
INTERRUPT SERVICE ROUTINE
For every interrupt, there must be an interrupt service routine (ISR), or interrupt handler. When
an interrupt is invoked, the microprocessor runs the interrupt service routine. For every
interrupt, there is a fixed location in memory that holds the address of its ISR. The group of
memory locations set aside to hold the addresses of ISRs is called the interrupt vector table.
When an interrupt is occurred, the microprocessor stops execution of current instruction. It
transfers the content of program counter (CS and IP) into stack.
The first 1Kbyte of memory of 8086 (00000 to003FF) is set aside as a table for storing the
starting addresses of Interrupt Service Procedures (ISP). Since 4-bytes are required for storing
starting addresses of ISPs, the table can hold 256 Interrupt procedures.
The starting address of an ISP is often called the Interrupt Vector or Interrupt Pointer. Therefore
the table is referred as Interrupt Vector Table. Each interrupt type is given a number between
0 to 255 and the address of each interrupt is found by multiplying the type by 4 e.g. for type
11, interrupt address is 11 x 4 = 4410= 0002CH
When the 8086 responds to an interrupt, it automatically goes to the specified location in the
Interrupt Vector Table in 8086 to get the starting address of interrupt service routine
ENDM
The below definition of a macro assign the name DISPLAY to the instruction sequence between the
directives MACRO and ENDM
This parameter MSG can be replaced by MSG1 or MSG2 while calling the macro as shown
Invoking Macros
A macro is called (invoked) by inserting its name in the program, possibly followed by macro
arguments.
The syntax for calling a macro is:
macroname argument-1, argument-2, ...
Macroname must be the name of a macro defined prior to this point in the source code. Each
argument is a text value that replaces a parameter in the macro. The order of arguments must
correspond to the order of parameters, but the number of arguments does not have to match
the number of parameters. If too many arguments are passed, the assembler issues a warning.
If too few arguments are passed to a macro, the unfilled parameters are left blank.
Procedures
Defining a Procedure
Informally, we can define a procedure as a named block of statements that ends in a return
statement. A procedure is declared using the PROC and ENDP directives. It must be assigned
a name (a valid identifier). A procedure must be written in the end of the code segment (before
end directive), and it can't receive any parameter.
When we create a procedure other than your program’s startup procedure, end it with a RET
instruction. RET forces the CPU to return to the location from where the procedure was called.
A procedure is defined using the PROC and ENDP directives.
The syntax is:
procedurename PROC
.
.
Ret
procedurename ENDP
Example :SumOf PROC
add ax,bx
add ax,cx
ret
SumOf ENDP
Invoking procedures
A procedure is called (invoked) by inserting its name in the program Preceded by (call).
The syntax for calling a procedure is:
Call procedurename
Difference between Macro and Procedure :
S.No. MACRO PROCEDURE
Procedure contains a set of instructions which can
Macro definition contains a set of instruction tobe called repetitively which can perform a specific
01. support modular programming. task.
It is used for small set of instructions mostly It is used for large set of instructions mostly more
02. less than ten instructions. than ten instructions.
03. In case of macro memory requirement is high. In case of procedure memory requirement is less.
CALL and RET instruction/statements are not CALL and RET instruction/statements are required
04. required in macro. in procedure.
Assembler directive MACRO is used to define Assembler directive PROC is used to define
macro and assembler directive ENDM is used procedure and assembler directive ENDP is used to
05. to indicate the body is over. indicate the body is over.
Execution time of macro is less than it executes Execution time of procedures is high as it executes
06. faster than procedure. slower than macro.
Here machine code is created multiple times as
each time machine code is generated whenHere machine code is created only once, it is
07. macro is called. generated only once when the procedure is defined.
In a macro parameter is passed as part of In a procedure parameters are passed in registers
08. statement that calls macro. and memory locations of stack.
Overhead time does not take place as there is noOverhead time takes place during calling procedure
09. calling and returning. and returning control to calling program.