Subroutine in 8085 Microprocessor YASH PAL, March 24, 2026March 24, 2026 Subroutine in 8085 Microprocessor – A subroutine is a sub-program (group of instructions) written separately from the main program to perform a particular task. The subroutine may be called repeatedly in the main program. A single subroutine may be used many times in a main program, and many programs may use the same subroutine many times. As the subroutine can be called by many of the programs, it should be written in such a way that it does not make any undesired change in the status (register and flags) of the main program.Subroutine Related InstructionsThe 8085 Microprocessor has two instructions to implement subroutines: CALL and RET. The CALL instruction is used to make a call to the subroutine, and the RET instruction is used in the subroutine to return to the main program.CALL Address (16 bit)CALL Address (16 bit)Call a subroutine unconditionally whose starting address is given within the instruction.Operation[SP – 1] ← PCH[SP-2] ← PCLSP ← SP – 2PC ← AddressLength3 BytesAddressing modeDirect addressing modeFlagsFlags are not affected.CALL Address (16 bit)Execution of instruction CALL address (16 bit) – The instruction CALL address is used to transfer the program control to a subroutine. This instruction loads the current program counter (PC) contents into the stack and loads the address of the subroutine into the program counter. Since the address of the next instruction in the main program is stored in the stack, it decrements the stack pointer by two. The timing diagram for the CALL instruction is shown in the image below.Memory AddressCode (H)2000CD200170200220Instruction: CALL 3000HTiming diagram for CALL instructionThe CALL instruction has two parts – the Read cycle and the Execution cycle. The read cycle of this instruction takes three machine cycles: Opcode fetch and two memory read machine cycles. The execution cycle includes two memory write machine cycles to store the address of the next instruction in the main program into the stack memory. After the CALL instruction is fetched, the 16-bit address is read during T2 and T3 states. In the next two T-states, T4 and T5, the contents of the program counter are stored in the stack memory.RET InstructionRETReturn from the subroutine unconditionallyOperationPCL ← [SP]PCH ← [SP + 1]SP ← SP + 2Length1 ByteAddressing modeIndirect addressing modeFlagsFlags are not affected.RET InstructionExecution of instruction RET – This instruction retrieves the return address (address of the instruction next to CALL in the main program) from the stack and loads the program counter with this return address. This instruction transfers program control to the instruction next to CALL in the main program.Execution of instruction RET with instruction CALLLikewise, other instructions, this instruction also has two parts: Read cycle and Execution cycle. The read cycle has one machine cycle, i.e., opcode fetch. The execution cycle includes two memory read machine cycles to retrieve the address (16 bits or 2 bytes) from the stack. The timing diagram for the instruction RET is shown in the figure below.Timing diagram for instruction RETNote: To use CALL and RET instructions, the stack must be initialized through the stack pointer (SP).Conditional CALL InstructionsIn addition to the unconditional CALL instruction, the 8085 microprocessor instruction set includes eight conditional CALL instructions. In case of conditional CALL instructions, the program control is transferred to the given address if the condition is satisfied; otherwise, the main program continues. Conditional CALL instructions are listed below:CC – Call subroutine if carry flag is set (CY = 1)CNC – Call subroutine if carry flag is reset (CY = 0)CZ – Call subroutine if zero flag is set (Z = 1)CNZ – Call subroutine if zero flag is set (Z = 0)CM – Call subroutine if sign flag is set (S = 1)CP – Call subroutine if sign flag is set (S = 0)CPE – Call subroutine if parity flag is set (P = 1)CPO – Call subroutine if parity flag is set (P = 0)Conditional RET InstructionsThe conditional RET instructions transfer the program control to the main program from the subroutine if the condition is satisfied; otherwise, the sequence in the subroutine is continued. There are eight conditional RET instructions. These are listed as follows:RC – Return subroutine if carry flag is set (CY = 1)RNC – Return subroutine if carry flag is reset (CY = 0)RZ – Return subroutine if zero flag is set (Z = 1)RNZ – Return subroutine if zero flag is set (Z = 0)RM – Return subroutine if sign flag is set (S = 1)RP – Return subroutine if sign flag is set (S = 0)RPE – Return subroutine if parity flag is set (P = 1)RPO – Return subroutine if parity flag is set (P = 0)Difference between CALL and JMP Instructions – Both the instructions CALL and JMP are branching instructions. Both copies the new address given in the instruction into the program counter, and program execution is transferred to the new address. The basic difference between these two is that the instruction CALL stores the address of the next instruction in the main program before transferring the program control to a new address, but the JMP instruction directly transfers the program control to a new address. It means if a subroutine is called by using the JMP instruction, the microprocessor does not store the returning address of the main program, and this becomes the responsibility of the programmer to provide the returning address when writing the subroutine.Another limitation given by the instruction JMP is that if the program control returns to the main program from a subroutine using the JMP instruction, it always returns to a specified address. Because of this, this subroutine can not be used more than one time. To understand the differences between CALL and JMP as mentioned above, the assembly language program with the JMP instruction listed below can be taken into consideration.Main ProgramMemoryAddressHexCodeLabelInstructionOpcodeInstructionOperandCommentsXX10HC3JMPXX60HJump to the starting address of the subroutine i.e., XX60HXX11H60JMPXX60HXX12HXXJMPXX60HXX13HNext InstructionXX2FH76HLTStop the executionSubroutineMemoryAddressHexCodeLabelInstructionOpcodeInstructionOperandCommentsXX60HBeginning of subroutine.XX65HC3JMPXX13HGo back to the main program.XX66H13XX67HXXUsing the JMP instruction, the subroutine becomes program-specific. The subroutine always returns to address XX13H. If a subroutine is called more than one time, each time it will return back to the same location, which is not desirable. This problem will not be there if CALL and RET instructions are used.Subroutine Parameter Passing TechniquesSubroutines are used to process some data or address variables from the main program. Therefore, it is required that some data or an address be passed from the main program to the subroutine. This is known as parameter passing. There are four ways to pass the parameters to and from the subroutine.Using registersUsing General memoryUsing pointersUsing StackUsing registers – The registers of the microprocessor are accessible in the main program as well as in a subroutine. It means the data to be passed is stored in registers, and these registers are accessed in the subroutine to process the data. In this technique, the registers are loaded with the appropriate values to be passed, and then the subroutine is called.Using General Memory – Parameter passing technique using general memory is used when a large number of parameters have to be passed to and from a subroutine. This memory may be a part of general memory. In this technique, the main program loads the pre-defined memory locations with appropriate values before calling the subroutine. Subroutine can process these values by referring to these predefined memory locations. The subroutine program stores the results in memory locations before executing the instruction RET. The main program accesses results from these memory locations for further processing.Using Pointers – In Passing parameter technique using pointers, the passing parameters are stored in consecutive memory locations. Then the starting address of the parameter list is stored in a register pair or pre-defined memory locations. The consecutive memory locations can be accessed by the subroutine.Using Stack – The stack can also be used to pass parameters to and from a subroutine. In the parameter passing technique using a stack, the parameters are first pushed on the stack, and then the subroutine is called. The subroutine can access the data on the stack directly. Whenever the stack is used to pass parameters, it is very important to keep track of what is pushed on the stack and where the stack pointer points at all times in the program.Subroutine NestingWhen one subroutine calls another subroutine to complete the specified task, the operation is known as nesting. The second subroutine may also be allowed to call another subroutine. In a similar manner, the third subroutine may call the next subroutine. These subroutines are known as nested subroutines.The program control returns in the reverse order, i.e., fourth subroutine to third subroutine, third subroutine to second subroutine, and second subroutine to first subroutine. Whenever nested subroutines are used, the stack-related operations must be handled very carefully, as transfer of control may go in the wrong direction. Nested subroutines are classified as follows:Re-entrant SubroutineRecursive SubroutineRe-entrant Subroutines – In a re-entrant subroutine, the first subroutine (SUB1) calls the second subroutine (SUB2), and the second subroutine calls the third subroutine (SUB3). In such a type of subroutine, the program execution flows from one subroutine to another and then another, and soon. The flow of program execution for a re-entrant subroutine is shown in the figure below.Flow of program execution for a reentrant subroutineRecursive Subroutine – A recursive subroutine is a subroutine that calls itself. This type of subroutine is used to work with complex data structures like trees. Such a type of subroutines makes the call to itself till the condition is satisfied; otherwise return to the main program. The figure below shows the flow of the program for the recursive subroutine.Flow of program execution for a recursive subroutine engineering subjects Microprocessor microprocessor