SUBROUTINES IN 8085 - (Document) (2022BCSE017)
SUBROUTINES IN 8085 - (Document) (2022BCSE017)
<<2022BCSE017>>
SUBROUTINES IN 8085
A subroutine is a group of instruction or small program written separately from the main
program to perform a function that occur repeatedly in the main program
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.
Subroutine is performed by 8085 with use of CALL and RET instruction
Calling a Subroutine:
• In the 8085, subroutines are called using the CALL instruction.
• Syntax: CALL <address>
• When a CALL instruction is executed, the 8085:
o Stores the address of the instruction following the CALL onto the stack (this allows
returning to the main program after subroutine execution).
o Loads the Program Counter (PC) with the address of the subroutine, starting its
execution.
Returning from a Subroutine:
• The RET (Return) instruction is used to exit a subroutine.
• When executed, the 8085 retrieves the return address from the stack and loads it into
the Program Counter (PC), resuming the main program execution from there.
Stack Operations:
• The stack plays an essential role in managing the flow between the main program and
subroutines.
<<Raghav Dabgotra>>
<<2022BCSE017>>
• The stack stores the return address of the main program, which is used by RET to return
to the correct location
Example of a Subroutine
SUBROUTINE:
ADD A ; Add register A to itself (A = A +
A)
RET ; Return to the calling program
Conditional Instructions:
• Conditional instructions execute only if a specific condition, often related to the status
flags, is met.
• Conditional calls and returns allow subroutines to be called or exited based on
conditions, adding flexibility in program control flow.
Examples:
<<Raghav Dabgotra>>
<<2022BCSE017>>
• Conditional CALL Instructions:
o CC <address> - Calls the subroutine if the Carry flag is set (CY=1).
o CNC <address> - Calls the subroutine if the Carry flag is reset (CY=0).
o CZ <address> - Calls the subroutine if the Zero flag is set (Z=1).
o CNZ <address> - Calls the subroutine if the Zero flag is reset (Z=0).
o CP <address> - Calls the subroutine if the Sign flag is reset (S=0).
o CM <address> - Calls the subroutine if the Sign flag is set (S=1).
o CPE <address> - Calls the subroutine if the Parity flag is set (P=1).
o CPO <address> - Calls the subroutine if the Parity flag is reset (P=0).
• Conditional RET Instructions:
o RC - Returns from the subroutine if the Carry flag is set (CY=1).
o RNC - Returns if the Carry flag is reset (CY=0).
o RZ - Returns if the Zero flag is set (Z=1).
o RNZ - Returns if the Zero flag is reset (Z=0).
o RP - Returns if the Sign flag is reset (S=0).
o RM - Returns if the Sign flag is set (S=1).
o RPE - Returns if the Parity flag is set (P=1).
o RPO - Returns if the Parity flag is reset (P=0).
Example:
MVI A, 0 ; Load 0 into A
MOV B, A ; Copy A to B
CMP B ; Compare A and B (Zero flag is set since A == B)
CZ SUBROUTINE ; Conditional call to SUBROUTINE if Zero flag is set (Z=1)
; Other code
<<Raghav Dabgotra>>
<<2022BCSE017>>
SUBROUTINE:
; Code for subroutine
RET ; Unconditional return
Advantages of Subroutines
• Code Reusability: Subroutines allow the same code to be used in multiple places
without duplication.
• Modularity: Each subroutine can be developed and tested independently.
• Efficiency: Programs can become smaller and more organized.
• Reducing duplicate code within a program
• Enabling reuse of code across multiple programs.