![]() |
VOOZH | about |
A stack is a storage device in which the information or item stored last is retrieved first. Basically, a computer system follows a memory stack organization, and here we will look at how it works.
A portion of memory is assigned to a stack operation to implement the stack in the CPU. Here the processor register is used as a Stack Pointer (SP). The above figure shows the portion of computer memory divided into three segments: Program Instructions, Data, and Stack.
As we can see in the figure, these three registers are connected to a common address bus and either one of them can provide an address for memory.
👁 Memory Stack OrganizationStack Pointer is first going to point at the address 3001, and then the stack will grow with the decreasing addresses. It means that the first item is going to be stored at address 3001, the second item at address 3000, and the items can keep getting stored in the stack until it reaches the last address 2000 where the last item will be held.
Here the data which is getting inserted into the Stack is obtained from the Data Register and the data retrieved from the Stack is also read by the Data Register.
Now, let's see the working of PUSH and POP operations in Memory Stack Organization.
This operation is used to insert a new data item into the top of the Stack. The new item can be inserted as follows:-
SP ←SP-1
M[SP]← DR
In the first step, the Stack Pointer is decremented to point at the address where the data item will be stored.
Then, by using the memory write operation, the data item from Data Register gets inserted into the top of the stack ( at the address where the Stack Pointer is pointing).
This operation is used to delete a data item from the top of the Stack. Data item can be deleted as follows:-
DR←M[SP]
SP←SP+1
In the first step, the top data item is read from the Stack into the Data Register. The Stack Pointer is then incremented to point at the next data item in the stack. Push or Pop operations can be performed with the help of the following microoperations:
It totally depends upon the organization of the stack whether the Stack Pointer (SP) is updated by incrementing or decrementing the address values.
In this case, the Stack Pointer grows by decreasing the memory address. The Stack may be made in a way that the Stack Pointer grows by increasing the memory also.
Since the address is always available and automatically updated in the Stack Pointer, the CPU can refer to the Memory Stack without having to specify an address.