Home » Microprocessors and Microcontrollers » What is Subroutine in Microprocessor?

What is Subroutine in Microprocessor?:

Some operations/functions are repeatedly performed in a main program like multiplication, division, and time delay between two operations, etc., Groups of instructions are written to perform these operations and these groups of instructions are known as Subroutine in Microprocessor, which are called by the main program whenever required. When a main program calls a subroutine, the program execution is transferred to the subroutine and after the completion of the subroutine, the program execution returns to the main program. The microprocessor uses the stack to store the return address of the subroutine. For example, generally, subroutine are written for sine, cosine, logarithms, square root, time delay, and multiplication functions in 8085 microprocessors.

A subroutine is implemented with two associated instructions, namely, Call and Return. Call is used to call a subroutine and the Call instruction is written in the main program. Return is used to return from the subroutine and the Return instruction is written in the subroutine to return to the main program. When a subroutine is called, the contents of the program counter are stored on the stack, and the program execution is transferred to the subroutine address. When the Return instruction is executed at the end of the subroutine, the memory address stored in the stack is retrieved and the sequence of execution is resumed in the main program. The syntax of CALL and RET are given below:

Subroutine in Microprocessor

The general characteristics of CALL and RET instructions are given below:

  1. The Call instructions are 3-byte instructions; the second byte specifies the lower-order byte, and the third byte specifies the higher-order byte of the subroutine address.
  2. The Return instructions are 1-byte instructions.
  3. A Call instruction must be used in conjunction with a Return instruction (conditional or unconditional) in the subroutine.

The following three types of subroutines are generally used in microprocessors:

  1. Multiple CALL subroutines
  2. Nested subroutines
  3. Multiple ending subroutines

Multiple CALL Subroutines

Figure. 4.6 shows the basic concept of multiple CALL sub­routines. This is a subroutine called from many locations in the main program. For example, the DELAY routine is a multiple CALL subroutine. These types of routines are easy to trace and need minimal stack space. Initially, stack pointer content is XX55H so that the return address can be stored on the stack. When the CALL instruction starts to execute, the subroutine is called from the 8050 memory location. The return address is stored on the stack and the stack pointer is decrement by two locations to XX53H.

Subroutine in Microprocessor

Nested Subroutine

When the subroutine is called by another subroutine, it is called a nested subroutine. When a subroutine calls another subroutine, all return addresses are stored on the stack. Therefore, only the number of available stack locations limits the extent of nesting. The structure of a nested subroutine is depicted in Fig. 4.7.

Subroutine in Microprocessor

The main program calls Subroutine I from location 8050H. The address of the next instruction, 8053H, is placed on the stack, and the program is transferred to Subroutine I at 8150H. During the execution of Subroutine I, it calls Subroutine II from location 8190H. The address 8193H is placed on the stack, and the program is transferred to Subroutine II. The sequence of execution returns to the main program as shown in Fig. 4.7.

Multiple Ending Subroutines

When a subroutine can be terminated at more than one place, it is called a multiple ending subroutine, as illustrated in Fig. 4.8. The subroutine has con­ditional returns such as RET Z (RZ) – Return On Zero and RET C (RC) — Return on Carry. This subroutine has an unconditional return (RET). While the Z flag is set, the subroutine returns from location 8050H, and if the CY flag is set, it returns from location 8090H. If neither flag is set, the subroutine returns from location 80A0H.

Subroutine in Microprocessor