Stack Assignment 1
Instructions:
Use individual memory locations to simulate the stack.
Use a top variable to track the top of the stack.
Implement push and pop operations according to the task.
1: Push & Pop 5 Numbers
Problem Statement:
1. Define 5 memory locations for stack elements:
num1 db ?
num2 db ?
num3 db ?
num4 db ?
num5 db ?
top db 0 ; 0 = stack empty
2. Push the following numbers in order: 10, 20, 30, 40, 50.
3. Pop the numbers one by one and display them in text mode.
4. Use the top variable to track the current top of the stack.
Expected Outcome:
Numbers should appear in reverse order of insertion (LIFO behavior).
2: Sum of Stack Elements
Problem Statement:
1. Using the same stack from Task 1, calculate the sum of all stack elements.
2. Pop each number temporarily, add it to a sum variable, then push it back so the stack
remains unchanged.
3. Display the final sum on the screen.
Hints:
Use a memory variable sum db 0 to store the sum.
Ensure stack remains unchanged after sum calculation.
Expected Outcome:
Correct sum of all 5 numbers is displayed.
Stack order is preserved.
3: Reverse Stack Elements
Problem Statement:
1. Use the stack from Task 1.
2. Pop all numbers one by one and store them in new memory locations in reverse order:
rev1 db ?
rev2 db ?
rev3 db ?
rev4 db ?
rev5 db ?
3. Display the reversed numbers.
Hints:
Use top variable to track stack pointer.
This task demonstrates the practical use of LIFO.
Expected Outcome:
Original stack: 10, 20, 30, 40, 50
Reversed memory: 50, 40, 30, 20, 10