0% found this document useful (0 votes)
6 views

6-Stack implementation

The stack in the 8086 microprocessor is a memory region that operates on a Last-In, First-Out principle, crucial for subroutine calls, parameter passing, and interrupt handling. It is managed using the Stack Pointer (SP), Base Pointer (BP), and Stack Segment (SS) registers, with operations including push and pop to add or remove data. Proper stack management is essential for efficient program execution and avoiding stack overflow and underflow errors.

Uploaded by

paramkusam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

6-Stack implementation

The stack in the 8086 microprocessor is a memory region that operates on a Last-In, First-Out principle, crucial for subroutine calls, parameter passing, and interrupt handling. It is managed using the Stack Pointer (SP), Base Pointer (BP), and Stack Segment (SS) registers, with operations including push and pop to add or remove data. Proper stack management is essential for efficient program execution and avoiding stack overflow and underflow errors.

Uploaded by

paramkusam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lecture Notes: Stack Implementation

Introduction to Stack in 8086 Microprocessor:

 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.

Role of Stack in 8086 Microprocessor:

 Temporary storage during program execution.


 Storing return addresses during subroutine calls.
 Saving and restoring register values.
 Managing local variables and parameters during procedure calls.
 Handling interrupts effectively by storing the current state of the processor.

Stack Structure in 8086:

 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).

Registers Involved in Stack Operations:

 Stack Pointer (SP):


o A 16-bit register that holds the offset address of the top of the stack.
o It decreases when data is added to the stack (push) and increases when data is removed (pop).
 Base Pointer (BP):
o A 16-bit register used to access data within the stack.
o Commonly used to reference parameters passed to subroutines.
 Stack Segment (SS):
o A 16-bit segment register that defines the starting address of the stack segment.
o The effective address of the stack is calculated using: Effective Address = SS × 16 + SP
( for stack top)

Stack Implementation Process:

A. Subroutine Calls and Return:

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.

2. Adding Data to the Stack (Push):

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.

3. Removing Data from the Stack (Pop):

o The data at the address pointed to by SP is removed.


o The stack pointer (SP) increases by 2.

Example of Stack Behavior:

Assume:

SS = 5000H

SP = 1000H (Stack top at 50000H + 1000H = 51000H)

Step 1: Add Data (PUSH)

Data: 1234H

SP = 0FFEH (decreases by 2)

Memory [SS:SP] = 1234H at address 50FFEH

Step 2: Add Another Data

Data: 5678H

SP = 0FFCH (decreases by 2)
Memory [SS: SP] = 5678H at address 50FFCH

Stack Status:

Top of Stack (SP = 0FFCH) = 5678H

Next (SP+2 = 0FFEH) = 1234H

Step 3: Remove Data (Conceptual POP)

Remove from top of stack: 5678H

SP = 0FFEH (increases by 2)

Stack Overflow and Underflow:

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).

Role of Stack in Subroutine Calls:

When a subroutine is called:

o The Return Address (IP and CS) is stored on the stack.


o When the subroutine ends:
o The Return Address is popped from the stack, ensuring the program resumes from the correct point.

Role of Stack in Interrupt Handling:

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.

You might also like