Stack

The stack in the Intel 8085 microprocessor is an essential component that plays an important role in handling subroutines and interrupts and in temporary data storage.

The stack concept

The stack is a special area of memory in an 8085 system. It operates on the last-in-first-out (LIFO) principle. This means that the last data item pushed onto the stack is the first one taken from it.

It is mainly used to store return addresses during subroutine calls and interrupt service routines. When a subroutine is called or an interrupt occurs, the current value of the program counter (PC) (which points to the next instruction after the call or interrupt) must be saved so that the program can resume execution from the correct point after the subroutine or the interrupt service routine has finished.

Stack pointer (SP)

The stack pointer is a 16-bit register in the 8085 microprocessor. It holds the address of the top of the stack. The stack pointer must first be initialized so that it points to a specific memory location where the stack will begin.

The programmer can initialize the stack pointer with the instruction LXI SP, <address>. For example, LXI SP, 2000h sets the stack pointer to memory location 2000h. This means that the stack will grow downwards from that address.

Stack operations

PUSH operation


The PUSH instruction is used to save the contents of a register pair on the stack.

Steps of the PUSH operation:

1. First, the stack pointer is decremented by 1. Thus, if the stack pointer initially pointed to address 2000h, after the first decrement it will point to 1FFFh.

2. The high-order byte of the register pair is then stored in the memory location pointed to by the stack pointer.

3. The stack pointer is decremented by 1 again.

4. Finally, the low-order byte of the register pair is stored in the new memory location pointed to by the stack pointer.

Example: for the instruction PUSH B, which involves the BC register pair, the contents of register B (high-order byte) and C (low-order byte) are placed on the stack in the manner described above.

POP operation


The POP instruction is used to take data from the stack and load it into a register pair.

Steps of the POP operation:

1. The low-order byte of data is read from the memory location pointed to by the stack pointer and loaded into the low-order register of the register pair.

2. The stack pointer is incremented by 1.

3. The high-order byte of data is read from the new memory location pointed to by the stack pointer and loaded into the high-order register of the register pair.

4. The stack pointer is incremented by 1 again.

Example: for the instruction POP D, the data from the stack is loaded into the DE register pair according to the steps described above.

Using the stack for subroutine calls and interrupts

Subroutine calls


When the CALL instruction is executed, the current value of the program counter (PC) is pushed onto the stack. This happens because the CALL instruction transfers program control to a subroutine, and the return address (the address of the next instruction after CALL) must be saved.

After the subroutine finishes, the RET (return) instruction is executed. The RET instruction pops the return address from the stack and loads it into the program counter, allowing the program to resume execution from the point where it stopped.