Skip to content
The Computer Science
TheCScience
  • Engineering Subjects
    • Human Values
    • Computer System Architecture
    • Digital Communication
    • Internet of Things
  • NCERT Solutions
    • Class 12
    • Class 11
  • HackerRank solutions
    • HackerRank Algorithms Problems Solutions
    • HackerRank C solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
The Computer Science
TheCScience

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 Instructions

The 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] ← PCL
SP ← SP – 2
PC ← Address
Length3 Bytes
Addressing modeDirect addressing mode
FlagsFlags 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)
2000CD
200170
200220
Instruction: CALL 3000H
Timing diagram for CALL instruction
Timing diagram for CALL instruction

The 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 Instruction

RETReturn from the subroutine unconditionally
OperationPCL ← [SP]
PCH ← [SP + 1]
SP ← SP + 2
Length1 Byte
Addressing modeIndirect addressing mode
FlagsFlags are not affected.
RET Instruction

Execution 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 CALL
Execution of instruction RET with instruction CALL

Likewise, 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 RET
Timing diagram for instruction RET

Note: To use CALL and RET instructions, the stack must be initialized through the stack pointer (SP).

Conditional CALL Instructions

In 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:

  1. CC – Call subroutine if carry flag is set (CY = 1)
  2. CNC – Call subroutine if carry flag is reset (CY = 0)
  3. CZ – Call subroutine if zero flag is set (Z = 1)
  4. CNZ – Call subroutine if zero flag is set (Z = 0)
  5. CM – Call subroutine if sign flag is set (S = 1)
  6. CP – Call subroutine if sign flag is set (S = 0)
  7. CPE – Call subroutine if parity flag is set (P = 1)
  8. CPO – Call subroutine if parity flag is set (P = 0)

Conditional RET Instructions

The 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:

  1. RC – Return subroutine if carry flag is set (CY = 1)
  2. RNC – Return subroutine if carry flag is reset (CY = 0)
  3. RZ – Return subroutine if zero flag is set (Z = 1)
  4. RNZ – Return subroutine if zero flag is set (Z = 0)
  5. RM – Return subroutine if sign flag is set (S = 1)
  6. RP – Return subroutine if sign flag is set (S = 0)
  7. RPE – Return subroutine if parity flag is set (P = 1)
  8. 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 Program

Memory
Address
Hex
Code
LabelInstruction
Opcode
Instruction
Operand
Comments
XX10HC3JMPXX60HJump to the starting address of the subroutine i.e., XX60H
XX11H60JMPXX60H
XX12HXXJMPXX60H
XX13HNext Instruction
XX2FH76HLTStop the execution

Subroutine

Memory
Address
Hex
Code
LabelInstruction
Opcode
Instruction
Operand
Comments
XX60HBeginning of subroutine.
XX65HC3JMPXX13HGo back to the main program.
XX66H13
XX67HXX

Using 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 Techniques

Subroutines 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.

  1. Using registers
  2. Using General memory
  3. Using pointers
  4. Using Stack

Using 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 Nesting

When 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:

  1. Re-entrant Subroutine
  2. Recursive Subroutine

Re-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 re entrant subroutine
Flow of program execution for a reentrant subroutine

Recursive 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 recursive subroutine
Flow of program execution for a recursive subroutine
engineering subjects Microprocessor microprocessor

Post navigation

Previous post
Next post

TheCScience

We at TheCScience.com are working towards the goal to give free education to every person by publishing in dept article about Secondary, Senior-Secondary, and Graduation level subjects.

Pages

About US

Contact US

Privacy Policy

DMCA

Our Tools

Hosting - get 20% off

Engineering Subjects

Internet of Things

Human Values

Digital Communication

Computer System Architecture

Programming Tutorials

Data Structure and Algorithm

C

Java

NCERT

Class 12th

©2026 TheCScience | WordPress Theme by SuperbThemes