0% found this document useful (0 votes)
116 views4 pages

SUBROUTINES IN 8085 - (Document) (2022BCSE017)

The document explains subroutines in the 8085 microprocessor, detailing how they are called using the CALL instruction and returned from using the RET instruction. It distinguishes between unconditional and conditional instructions, providing examples of each, and highlights the advantages of using subroutines such as code reusability and modularity. Additionally, it discusses the role of the stack in managing program flow between the main program and subroutines.

Uploaded by

physicist sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views4 pages

SUBROUTINES IN 8085 - (Document) (2022BCSE017)

The document explains subroutines in the 8085 microprocessor, detailing how they are called using the CALL instruction and returned from using the RET instruction. It distinguishes between unconditional and conditional instructions, providing examples of each, and highlights the advantages of using subroutines such as code reusability and modularity. Additionally, it discusses the role of the stack in managing program flow between the main program and subroutines.

Uploaded by

physicist sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

<<Raghav Dabgotra>>

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

MAIN: MVI A, 05H ; Load 5 into register A


CALL SUBROUTINE ; Call the subroutine
HLT ; Halt the program

SUBROUTINE:
ADD A ; Add register A to itself (A = A +
A)
RET ; Return to the calling program

In 8085 microprocessor subroutines, instructions can be classified into unconditional and


conditional based on whether they execute without checking any condition or depend on a
specific condition.
Conditional Call instruction –
Unconditional Call Instructions:
• Unconditional instructions always execute when encountered, without any conditions.
• These instructions do not check any flags or conditions and simply proceed to the
given address.

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.

You might also like