Subroutine in 8085

In computers, a subroutine is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task have to be performed. A subroutine is often coded so that it can be started (called) several times and from several places during one execution of the program, including from other subroutines, and then branch back (return) to the next instruction after the call, once the subroutine’s task is done. It is implemented by using Call and Return instructions. The different types of subroutine instructions are

Unconditional Call instruction –
CALL address is the format for unconditional call instruction. After execution of this instruction program control is transferred to a sub-routine whose starting address is specified in the instruction. Value of PC (Program Counter) is transferred to the memory stack and value of SP (Stack Pointer) is decremented by 2.

Conditional Call instruction –
In these instructions program control is transferred to subroutine and value of PC is pushed into stack only if condition is satisfied.



INSTRUCTION PARAMETER COMMENT
CC 16-bit address Call at address if cy (carry flag) = 1
CNC 16-bit address Call at address if cy (carry flag) = 0
CZ 16-bit address Call at address if ZF (zero flag) = 1
CNZ 16-bit address Call at address if ZF (zero flag) = 0
CPE 16-bit address Call at address if PF (parity flag) = 1
CPO 16-bit address Call at address if PF (parity flag) = 0
CN 16-bit address Call at address if SF (signed flag) = 1
CP 16-bit address Call at address if SF (signed flag) = 0

Unconditional Return instruction –
RET is the instruction used to mark the end of sub-routine. It has no parameter. After execution of this instruction program control is transferred back to main program from where it had stopped. Value of PC (Program Counter) is retrieved from the memory stack and value of SP (Stack Pointer) is incremented by 2.

Conditional Return instruction –
By these instructions program control is transferred back to main program and value of PC is popped from stack only if condition is satisfied. There is no parameter for return instruction.

INSTRUCTION COMMENT
RC Return from subroutine if cy (carry flag) = 1
RNC Return from subroutine if cy (carry flag) = 0
RZ Return from subroutine if ZF (zero flag) = 1
RNZ Return from subroutine if ZF (zero flag) = 0
RPE Return from subroutine if PF (parity flag) = 1
RPO Return from subroutine if PF (parity flag) = 0
RN Return from subroutine if SF (signed flag) = 1
RP Return from subroutine if SF (signed flag) = 0

Advantages of Subroutine –

  1. Decomposing a complex programming task into simpler steps.
  2. Reducing duplicate code within a program.
  3. Enabling reuse of code across multiple programs.
  4. Improving tractability or makes debugging of a program easy.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.