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.
|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.
|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 –
- Decomposing a complex programming task into simpler steps.
- Reducing duplicate code within a program.
- Enabling reuse of code across multiple programs.
- Improving tractability or makes debugging of a program easy.
- Pin diagram of 8085 microprocessor
- ROTATE Instructions in 8085
- Bus organization of 8085 microprocessor
- COMPARE Instructions in 8085
- 8085 program to add two 16 bit numbers
- Registers of 8085 microprocessor
- 8085 program to add 2-BCD numbers
- Interrupts in 8085 microprocessor
- 8085 program to add two 8 bit numbers
- Differences between 8085 and 8086 microprocessor
- 8085 program to reverse 8 bit number
- 8085 program for pulse waveform
- 8085 program to find the sum of a series
- 8085 program to subtract two BCD numbers
- 8085 program for hexadecimal counter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.