Stack and Subroutine in 8085 Microprocessor are two important concepts used in computer programming. The 8085 microprocessor has a simple and efficient architecture that includes a stack and subroutines.
Stack in 8085 Microprocessor
- The stack is a special area in memory used by the CPU to store register information or general data information during program execution. The stack has a top and a bottom.
- A stack is a region of memory that is used to store data temporarily. In the 8085 microprocessor, the stack is implemented using a dedicated block of memory known as the stack pointer (SP).
- The stack pointer (SP) register in 8085 contains the 16-bit offset from the start of the segment to the memory location where a word was most recently stored on the stack. The memory location where a word was most recently stored is called the top of stack.
- The stack pointer points to the current top of the stack, which is the location where the next item will be stored.
- The stack used is a Last in first out (LIFO) structure that uses push and pull (pop) operations.
- When data is pushed onto the stack, the stack pointer is decremented by one to point to the next available memory location. When data is popped from the stack, the stack pointer is incremented by one to point to the next item on the stack.
The 8085 microprocessor provides a set of instructions that can be used to manipulate the stack. These instructions include:
- PUSH: This instruction is used to push a register or memory location onto the stack. The contents of the register or memory location are first copied to the memory location pointed to by the stack pointer, and then the stack pointer is decremented.
- POP: This instruction is used to pop the topmost item from the stack. The contents of the memory location pointed to by the stack pointer are first copied to the register or memory location specified in the instruction, and then the stack pointer is incremented.
Subroutines in 8085 Microprocessor
When the same function is required more than once in a program, it is frequently written as a subroutine, that is, a subprogram that can be used any number of times by the main program.
Subroutines are a powerful programming construct that allow a program to break down a complex task into smaller, more manageable pieces.
A subroutine is a block of code that can be called from anywhere in the program, and then returns control back to the calling code when it is done.
Note that the stack state in the figure corresponds to the time at which the processor executes instructions within the second subroutine.
Also note that if the stack is used for register savings in a subroutine, the recovery should be done in the reverse order and in a balanced fashion, otherwise the return address cannot be recovered properly into the PC.
The 8085 microprocessor provides a set of instructions that can be used to implement subroutines. These instructions include:
- CALL: This instruction is used to call a subroutine. The current program counter (PC) is pushed onto the stack, and then the program counter is set to the address of the first instruction in the subroutine.
- RET: This instruction is used to return from a subroutine. The topmost item on the stack is popped into the program counter, which restores the original program counter and continues execution of the calling code.
In summary, the stack and subroutines are important concepts in computer programming, and the 8085 microprocessor provides a set of instructions that can be used to manipulate the stack and implement subroutines. The stack is used to store data temporarily, while subroutines allow a program to break down a complex task into smaller, more manageable pieces.