Subroutine in 8085

In this course, we will study the subroutine in 8085, subroutine nesting, its uses, and the difference between function and subroutine.

What is a subroutine?

A subroutine is a function and is used to reduce the size of the program. Instead of writing the same code, again and again, we make the code a function and this reduces the size of the program. Only one copy of the code is stored in the memory. Whenever the code is required we can write a subroutine instruction that will call the subroutine. A subroutine can exist anywhere in the code.

Subroutine in 8085

Whenever a subroutine instruction is encountered in the program the code is executed and then the next instruction in the program is executed. Great care should be taken while returning the subroutine because it can be called from various locations of the memory.

There are two instructions in 8085 that deals with subroutines:

The CALL function

  1. The microprocessor reads the subroutine address from the next two memory locations and stores the higher-order of the 8 bit of the address in the W register and stores the lower-order of 8 bit of address in the Z register.

  2. The addresses of the instruction immediately following the call are pushed onto the stack.

  3. Loads the program counter with the 16-bit address supplied with the call instruction from WZ registers.

The RET function

  1. It retrieves the return address from the top of the stack.
  2. Loads the program counter with the return address.

How do we pass the data to the subroutine?

Data in the subroutine can be passed through two ways- call by value and call by reference.

Call by reference

The subroutine uses the value from the register, the data is stored in one of the registers by the calling program. The register value gets modified within the subroutine. Then these modifications can be transferred back to the calling program upon returning from the subroutine.

Call by value

The subroutine first pushes register values in the stack and after using the registers, the subroutine pops the previous values of the registers from the stack while exiting the subroutine. The data is stored in one of the registers. That is the original values are restored before execution returns to the calling program.

What is subroutine nesting?

Subroutine nesting is calling a subroutine from another subroutine call. It is like calling one function and that function calls another function by itself.

What is subroutine nesting

What is the use of subroutine?

What is the difference between function and subroutine?

FUNCTION SUBROUTINE
A function always returns a value. A subroutine performs a set of tasks and does not return a value.
They can be used as formulas in the spreadsheet. They can not be used directly as formulas in the spreadsheet.
They are called by using a variable They are called by using the call function after their declaration