The stack in an 8085 can be described as a reserved area of the memory in the R/W memory where we can store temporary information. It is a shared resource as it can be shared by the microprocessor and the programmer. Programmers use the stack to store data and the microprocessors use the stack to execute subroutines.
The 8085 has a 16-bit register known as the Stack Pointer. The function of the stack pointer is to hold the starting address of the stack. This address can be decided by the programmer.
The stack operates on the Last In, First Out (LIFO) principle. The location of the most recent data on the stack is known as the TOP of the stack. The stack pointer always points to the top of the stack. Contents can be stored in the stack using the PUSH
instruction and can restore the contents by using the instruction POP
.
MNEMONIC | DESCRIPTION |
---|---|
LXI SP, 16-bit | Load the stack pointer register with a 16-bit address. |
PUSH Rp | Copies the contents of the specified register pair on the stack |
POP Rp | Copies the contents of the top two memory locations of the stack into the specified register pair. |
PUSH and POP Operation in 8085
PUSH Rp
This is a 1-byte instruction. This instruction copies the contents of the specified register pair on the stack as described below:
- The stack pointer is decremented and the contents of the higher-order register are copied to the location shown by the stack pointer register.
- The stack pointer is again decremented and the contents of the low-order register are copied to that location.
POP Rp
This is a 1-byte instruction. This instruction copies the contents of the top two memory locations of the stack into the specified register pair.
- First, the contents of the memory location indicated by the stack pointer register are copied into the low-order register and then the stack pointer register is incremented by 1.
- The contents of the next memory location are copied into the high-order register and the stack pointer register is again incremented by 1.
Example
LXI SP,2099H LXI H, 42F2H PUSH H Delay Counter POP H
- The instruction
LXI SP, 2099H
will initialize the stack pointer with the address of 2099H. LXI H, 42F2H
will initialize or load HL register pair with42F2H
data soH = 42
andL = F2
.
![stack in 8085 1](https://teachics.org/wp-content/uploads/2021/11/stack-in-8085-1.png)
- After the execution of
PUSH H
instruction the stack pointer is decreased by one to2098H
and the contents of the H register are copied to memory location2098H
. - The stack pointer is again decreased by one to
2097H
and the contents of the L register are copied to memory location2097H
.
![push operation in 8085 stack](https://teachics.org/wp-content/uploads/2021/11/push-operation-in-8085-stack.png)
- After the execution of
POP H
instruction, the contents of the top of the stack location shown by the stack pointer are copied in the L register and the stack pointer is increased by one to2098H
. - The contents of the top of the stack are copied in the H register and the stack pointer is increased by one.
- The contents of the memory locations
2097H
and2098H
are not destroyed until some other data bytes are stored in these locations.
![pop operation in 8085 stack](https://teachics.org/wp-content/uploads/2021/11/pop-operation-in-8085-stack.png)