The concept of stack came into limelight with the usage of terms “bury” and “unbury” where those corresponds to calling and returning from the subroutine functions. To be clear in the concepts of Stack and Stack Pointer, initially, we need to understand the terms what is an“Array” and “Linked list”? An array is defined as the randomly accessible data structure where one can easily access the elements in this and in steady time. Binary search is a clear example of a random-access data structure. While a linked list is defined as the sequentially accessible data structure where every element in this has to be accessed only in a specific order. A roll of tape is a clear example of a sequential-access data structure.
So, we are clear about what a sequential access data structure is? Now, we move our discussion to the subcategory of sequential access data structures and those are “Limited access data structures”. And Stack is the one that comes under limited access data structures. Let’s get into the detailed concepts behind Stack and Stack pointer.
What are the Stack and Stack Pointer?
A stack is stated as the container of elements where insertion and removal of the elements follow with the last-in-first-out (LIFO) theory. Here, the insertion of elements is done through push operation and removal of elements is done through a pop operation. Also, the other definition in the technical aspect is, a stack is a LIFO data structure which is employed in the RAM area where one can store temporary data and addresses when the microprocessor jumps to a subroutine from its current task. Then the retrieved address would be used by the processor to jump back to the previous routine/job/task. A stack is also called a recursive data structure where pushing and popping happens continously.
It is the tiny register which stores the last program request’s address in a stack. It is the particular kind of buffer that stores the information in the order of top-down. When the new requests arrive, those will push down the previous requests. So, the latest request always is placed at the stack top position and the program will get its requests only from the top position. With the entry of new requests, the stack pointer moves ahead to the subsequent physical memory address and the latest element is replicated in the new address location. In the same way, when an element is removed from the stack, the SP falls back to the next present item on the top position in the stack.
In general, processors store SPfsac in the hardware registers and the ALU (arithmetic and logic unit) functions to calculate its value. Usually, push and pop are converted as micro-operations to individually add in or remove the SP and carry out load and store in the memory.
Stack Pointer in 8085
In the context of a programmer, an 8085 processor has only A. B, C, D, E, H and L, and the Flag registers. While in the complete view of 8085, it has the other two special purpose registers which are 16-bit each. Those are program counter PC and the stack pointer SP. The purpose of SP in 8085 is to store the stack top location address. While PC is the register that holds the memory location address and from here the next executing instruction will be retrieved. A view of 8085 with all the registers is shown below:
The stack pointer is a 16-bit register having a memory address. For example, when the SP contents are of FC78H, then the 8085 address locations are interpreted as below.
The memory locations FC78H, FC79H, FC80H……… FFFFH holds useful data and these memory locations are considered as filled locations. Whereas FC77H, FC76H….0000H does not have any data and those are considered empty locations. So, SP contents indicate the uppermost locations in the stack which means that it specifies memory location having the smallest address with useful data. It can be illustrated as:
How an SP Function?
The stack is generally a reversed area in RAM to store the information. Anywhere in the memory map, a stack can be initialized, but usually, it will be initialized at the higher memory location to get rid of any programming interface. Coming to 8085, the initialization of stack is instructed as
LXI SP, 16 bit
LXI SP is a 16-bit state which loads a 16-bit address into stack register. Before the utilization of stack, it has to be initialized to one higher value which is more than the stack’s highest memory location. The initialization of the stack pointer can be done by Load Stack Pointer.
The stack in 8085 performs both PUSH and POP operations. Both the operations work together with register pairs following the LIFO principle. Until the new data is inserted into the stack’s memory location, the previous data will not get obliterated.
PUSH Operation in a Stack is as Below
- The SP is decreased, and the higher-order register pair contents (B in BC and D in DE) are replicated to the stack.
- Again, the SP is decreased, and now lower order register pair contents are replicated to the stack. Here no flags or register pair contents will get modified.
When the instruction is PUSH D – It pushes the contents of DE. Suppose we consider D = 15 H and E = 23 H. Let’s assume that SP = 2300 H. Then the execution of PUSH P will result in the output as below:
SP = 22 FE
POP Operation in the Stack is as Below
- Stack’s top location contents are replicated into stack lower register (C in BC). Then SP gets increased by 1.
- Then the contents of stack location as shown by SP are replicated into stack’s higher register. Now, SP is increased by 1.
Let SP = 22FE H, the execution of POP H will result in the output as
SP = 2300
Here H = 24 H and L = 10 H
Use of Stack Pointer
The stack pointer is mainly used as a memory pointer which specifies to the memory location that read and write memory to that location.
- The typical usage of the stack pointer is to hold stack bits that belong to the present function.
- It can be used for both the user (as passed parameters and local variables) and CPU information (returning addresses at the time of subroutine call).
1. What is the stack pointer in the ARM?
As the general usage of the stack is to hold automatic variables and parameters that are across function calls. In the context of ARM, the register SP R13 is utilized as the pointer to the active stack.
2. Why stack pointer is of 16-bit?
PC and SP are utilized to store the memory locations and as the previous location address is 16-bits and so stack pointers are also of 16-bits. So that they hold a 16-bit data address.
3. What is the role of the stack pointer?
Following the principle of top-down, the stack pointer’s role is to store the last program request address. So, the arrival of new requests pushes down the previous requests.
4. Which stack is used in 8085?
Stack in 8085 follows the LIFO stack where the last stored data will be received initially.
5. Is stack pointer a register?
Yes, the stack pointer is a tiny register that stores the last program request address.
So, the concepts of Stack and Stack Pointer are as disccussed above and almost every programming application needs to implement the operation of these. Know more concepts on what are their implementations and other applications?