More topics in Microprocessors Course

Stack, Stack pointer and Subroutines in 8085 – With coding examples

The stack is a reserved area of the memory in RAM where we can store temporary information. Interestingly, the stack is a shared resource. As it can be shared by the microprocessor and the programmer. The programmer can use the stack to store data. And the microprocessor uses the stack to execute subroutines. The 8085 has a 16-bit register known as the ‘Stack Pointer’.

This register’s function is to hold the memory address of the stack. This control is given to the programmer. The programmer can decide the starting address of the stack by loading the address into the stack pointer register at the beginning of a program.

The stack works on the principle of First In Last Out. The memory location of the most recent data entry on the stack is known as the Stack Top.

How does a stack work in assembly language?

We use two main instructions to control the movement of data into a stack and from a stack. These two instructions are PUSH and POP.

PUSH – This is the instruction we use to write information on the stack.
POP – This is the instruction we use to read information from the stack.

There are two methods to add data to the stack: Direct method and Indirect method

Direct method

In the direct method, the stack pointers address is loaded into the stack pointer register directly.

LXI SP, 8000H
LXI H, 1234H
PUSH H 
POP D
HLT

Explanation of the code

LXI SP, 8000H – The address of the stack pointer is set to 8000H by loading the number into the stack pointer register.

LXI H, 1234H – Next, we add a number to the HL pair. The most significant two bits will enter the H register. The least significant two bits will enter the L register.

PUSH H – The PUSH command will push to contents of the H register first to the stack. Then the contents of the L register will be sent to the stack. So the new stack top will hold 34H.

POP D – The POP command will remove the contents of the stack and store them to the DE register pair. The top of the stack clears first and enters the E register. The new top of the stack is 12H now. This one clears last and enters the D register. The contents of the DE register pair is now 1234H.

HLT – HLT indicates that the program execution needs to stop.

Indirect method

In the indirect method, the stack pointers address is loaded into the stack pointer register via another register pair.

LXI H, 8000H
SPHL
LXI H, 1234H
PUSH H 
POP D
HLT

Explanation of the code

LXI H, 8000H – The number that we wish to enter into the stack pointer, 8000H, is loaded into the HL pair register.

SPHL – This is a special command that we can use to transfer data from HL pair to Stack pointer (SP). Now, the contents of the HL pair are in the SP.

LXI H, 1234H – Next, we add a number to the HL pair. The most significant two bits will enter the H register. The least significant two bits will enter the L register.

PUSH H – The PUSH command will push to contents of the H register first to the stack. Then the contents of the L register will be sent to the stack. So the new stack top will hold 34H.

POP D – The POP command will remove the contents of the stack and store them to the DE register pair. The top of the stack clears first and enters the E register. The new top of the stack is 12H now. This one clears last and enters the D register. The contents of the DE register pair is now 1234H.

HLT – HLT indicates that the program execution needs to stop.

Both the methods can be shown diagrammatically with the following diagram.

 working of a stack - direct and indirect method example

What is a Subroutine is assembly language?

A subroutine is a small program written separately from the main program to perform a particular task that you may require repeatedly in the main program. Essentially, the concept of a subroutine is that it is used to avoid the repetition of smaller programs.

Subroutines are written separately and are stored in a memory location that is different from the main program. You can call a subroutine multiple times from the main program using a simple CALL instruction.

What are the conditional CALL statements in assembly language?

You can use conditional CALL statements too according to your need. These statements enter a subroutine only when a certain condition is met.

CCCall at address if cy (carry flag) = 1
CNCCall at address if cy (carry flag) = 0
CZCall at address if ZF (zero flag) = 1
CNZCall at address if ZF (zero flag) = 0
CPECall at address if PF (parity flag) = 1
CPOCall at address if PF (parity flag) = 0
CNCall at address if SF (signed flag) = 1
CPCall at address if SF (signed flag) = 0

The subroutine can be exited from using a return (RET) instruction.

What are the conditional return (RET) statements in assembly language?


Akin to the CALL instruction, we have conditional RET statements too. These statements ensure that a subroutine is exited only when a certain condition is met.

RCReturn from subroutine if cy (carry flag) = 1
RNCReturn from subroutine if cy (carry flag) = 0
RZReturn from subroutine if ZF (zero flag) = 1
RNZReturn from subroutine if ZF (zero flag) = 0
RPEReturn from subroutine if PF (parity flag) = 1
RPOReturn from subroutine if PF (parity flag) = 0
RNReturn from subroutine if SF (signed flag) = 1
RPReturn from subroutine if SF (signed flag) = 0

After the completion of the subroutine, the main program begins from the instruction immediately following the CALL instruction.

What are the advantages of using subroutines?

  • Avoids the repetition of instructions.
  • Gives an aspect of modular programming to the entire program.
  • Improves efficiency by reducing the size of the memory needed to store the program.

About The Writer

CLOSE

Leave a Reply

Your email address will not be published. Required fields are marked *