lOMoARcPSD|17517997
Subroutine-8085 Microprocessor
Microprocessor and embedded system (APJ Abdul Kalam Technological University)
Scan to open on Studocu
Studocu is not sponsored or endorsed by any college or university
Downloaded by simeon kiplagat ngeno (sk.ngeno42@gmail.com)
lOMoARcPSD|17517997
SUBROUTINES
➢ Subroutine is a sequence of program instruction that performs a
specific task,packaged as a unit .
➢ This unit can be used in programs wherever that particular task is to
be performed.
➢ Sub routines may be defined with in a program or separately in
libraries that can be used by multiple programs.
➢ In different programming languages, a subroutine may be called
procedure,function,a routine,a method, or a subprogram.
➢ Generic term ‘callable unit ’ is sometimes called.
➢ 8085 has 2 instructions for delaying with subroutines;
1) CALL : Call instruction is used to transfer program sequence to
subroutine.
2) RET : Ret instruction is used to transfer the program sequence
from the subroutine to calling instruction.
1. CALL INSTRUCTION
➢ It is a 3 byte instruction.
➢ The call instruction transfers the program sequence to the memory
address given in the operand.
➢ For eg :
CALL 4000H
➢ When this instruction is fetched ,then Microprocessor knows the
next 2 memory locations contain the 16 bit subroutine address.
➢ It reads subroutine address and stores the higher order 8 bit of
address in W register and lower order 8 bit of address in Z register.
➢ Also pushes the current value of PC(Program counter) on to
Stack.(This is return address)
Downloaded by simeon kiplagat ngeno (sk.ngeno42@gmail.com)
lOMoARcPSD|17517997
2000 CALL 4000 H
2003
4000 H W Z Register
2. RET INSTRUCTION
➢ It is a one byte instruction.
➢ It has no parameters.
➢ RET is the instruction used to mark the end of sub-routine.
➢ RET instruction retrieve the return address from the top of the Stack.
➢ Load the PC with the return address .
Downloaded by simeon kiplagat ngeno (sk.ngeno42@gmail.com)
lOMoARcPSD|17517997
4000H
.
.
.
.
4012H
4013H RET
✓ Conditional CALL and conditional RET : It does its operation
based on the condition of status flag of PSW.
❖ Things to be considered in Subroutine
I. You must set the SP(Stack pointer) correctly before using CALL
instruction.
II. Donot modify the SP in a subroutine.It causes to lose return address.
III. Number of the PUSH and POP instruction used in the Subroutine
must be same.Otherwise RET instruction will pick a wrong RET
address from the Stack.
Downloaded by simeon kiplagat ngeno (sk.ngeno42@gmail.com)
lOMoARcPSD|17517997
❖ A proper Subroutine should be as follows ;
I. Only entered with CALL and Exited with RET instruction.
II. Should have a single entry point.
III. Donot use CALL instruction to jump into different point of same
subroutine.
IV. It should have a single exit point
Downloaded by simeon kiplagat ngeno (sk.ngeno42@gmail.com)