Subroutine in 8085 Microprocessor

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

Subroutine in 8085 Microprocessor

A Subroutine in 8085 Microprocessor 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 Microprocessor

Whenever a Subroutine in 8085 Microprocessor 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 deal with subroutines:

  • The CALL instruction in 8085 Microprocessor is used for redirecting the program execution to the subroutine.
  • The RET instruction in 8085 Microprocessor is used for returning the execution to the calling routine.

The CALL function

    1. The microprocessor reads the subroutine address from the following two memory locations and stores the higher order of 8 bits of the address in the W register and stores the lower order of 8 bits 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 in 8085 Microprocessor?

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

Call by reference Subroutine in 8085 Microprocessor

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 Subroutine in 8085 Microprocessor

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 in 8085 Microprocessor?

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

Subroutine nesting in 8085 Microprocessor

What is the use of subroutine 8085 Microprocessor?

  • Repetition of instructions can be avoided
  • The efficiency of the program is improved
  • Size is reduced

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

Thanks for learning Subroutine in 8085 Microprocessor with me.