- The stack is a reserved area of the memory where temporary
information may be stored.
- An n-bit stack is used to hold the address of the most recent stack
entry. This location which has the most recent entry is called as the
top of the stack.
- When the information is written on the stack, the operation is called
PUSH.
- When the information is read from the stack, the operation is called
POP. The stack works on the principle of Last in First Out or first
in Last Out.
- The microprocessor stores the information/data like stacking plates.
Fig below shows stacked plates.
- If we want to remove the first stack plate, then we have to remove
all the plates above the first i.e. we have to remove the fourth
plate, third plate, second plate and then finally the first plate.
- This indicates that the first plate pushed onto the stack is the last
one to be popped from the stack. This operation is called as First In
Last Out (FILO).
- The stack is implemented with the help of special memory pointer
register called as the stack pointer. The stack pointer’s contents
are automatically adjusted to point to the stack.
- The memory location that is currently pointed by the stack pointer is
called as top of the stack.
- As shown in the figure the 4th stacked plate represents top of stack.
- When data is to be stored on the stack, the SP increments before
storing the data on stack and when the data is to be retrieved/popped
from the stack the SP decrements to point next available byte of
stored data.
- The stack array can reside anywhere in the on-chip memory.
In short,
Stack is used to store information (data) temporarily.
Stack is a part of memory defined in program by THE USER.
STACK, the name itself informs about the way data is stored i.e. stack of data.