6-Stack implementation
6-Stack implementation
The stack is a region of memory used to store temporary data, such as return addresses, procedure
parameters, and local variables.
It operates on the LIFO (Last-In, First-Out) principle.
The stack is essential for subroutine calls, parameter passing, and interrupt handling.
The stack is implemented in a section of memory called the Stack Segment (SS).
It grows downwards, meaning that with each new data entry, the stack pointer (SP) decreases.
The top of the stack is always pointed to by the Stack Pointer (SP).
o During a subroutine call, the return address is pushed onto the stack.
o On returning from the subroutine, the address is popped from the stack, ensuring the program resumes
from the correct location.
B. Interrupt Handling:
o When an interrupt occurs, the processor automatically pushes the flags, code segment, and instruction
pointer onto the stack.
o The interrupt service routine (ISR) executes.
o At the end, an interrupt return operation (IRET) pops these values to restore the previous program state.
Stack Operations:
1. Stack Setup:
o The stack segment (SS) is assigned a memory block for stack operations.
o The stack pointer (SP) is initialized to point to the top of this segment.
o Before adding data, the stack pointer (SP) decreases by 2 (for 16-bit data).
o The data is stored at the new address indicated by SP.
Assume:
SS = 5000H
Data: 1234H
SP = 0FFEH (decreases by 2)
Data: 5678H
SP = 0FFCH (decreases by 2)
Memory [SS: SP] = 5678H at address 50FFCH
Stack Status:
SP = 0FFEH (increases by 2)
Stack Overflow: Occurs when the stack grows beyond its allocated memory (SP crosses lower limit).
Stack Underflow: Occurs when data is popped from an empty stack (SP crosses upper limit).
During an Interrupt:
o The processor automatically saves the current Flags, CS, and IP on the stack.
o When the Interrupt Service Routine (ISR) completes:
o The processor retrieves these values from the stack to restore the previous program state.
Summary:
o The stack is a critical component in the 8086 microprocessor for managing subroutine calls, interrupts,
and local data storage.
o It operates using the LIFO principle and relies on the SP, BP, and SS registers.
o Proper stack management is essential for efficient and error-free program execution.