SUBROUTINES

SUBROUTINES

Subroutines definition
Subroutines working

Hello everyone!
I am here again on another topic ..

WHAT ARE SUBROUTINES?

What do you think is subroutine?

Well I will walk you through this!

Lets begin!

Subroutines :
A Subroutine is a group of instruction, that performs a particular subtask which is executed number of times. It is written separately.
The processor executes this subroutine by transferring program control to the subroutine program. After completion of subroutine program execution, the program control is returned back to the main program. The use of subroutine is very important technique in designing software for processor systems because it eliminates the need to write the subtasks repeatedly; thus it uses memory more efficiently. When a program branches to a subroutine we say that it is calling the subroutine. The instruction that performs the branch operation is referred to as call instruction. After completion of subroutine program execution, the program control is returned back to the main program by executing a return instruction. Since the subroutine may be called from different places in a calling program, provision must be made for returning to the appropriate location, i.e., the next instruction from where the subroutine is called from the calling program. When the call instruction is executed the PC is updated to point to the next instruction. The updated content of the PC are saved by the call instruction to enable correct return to the calling program.

The technique by which a computer makes it possible to call and return from subroutines is referred to as its subroutine linkage method. The simplest method is to save the return address in a register dedicated to this function. Such a register is called link register. Lets see how the call and return operations are performed.

Call operation :
  • It stores the contents of the PC in the link register.
  • It then branches the program control to the target address specified by the instruction.
Return operation :
  • It loads the PC with the contents of link register and branches to the next instruction from where the subroutine is called.
The fig below shows the call and return operation.








0 comments