In this course, we will study the subroutine in 8085, subroutine nesting, its uses, and the difference between function and subroutine.
A subroutine is a function and is used to reduce the size of the program. Instead of writing the same code, again and again, we make the code a function and this reduces the size of the program. Only one copy of the code is stored in the memory. Whenever the code is required we can write a subroutine instruction that will call the subroutine. A subroutine can exist anywhere in the code.
Whenever a subroutine instruction is encountered in the program the code is executed and then the next instruction in the program is executed. Great care should be taken while returning the subroutine because it can be called from various locations of the memory.
There are two instructions in 8085 that deals with subroutines:
Data in the subroutine can be passed through two ways- call by value and call by reference.
The subroutine uses the value from the register, the data is stored in one of the registers by the calling program. The register value gets modified within the subroutine. Then these modifications can be transferred back to the calling program upon returning from the subroutine.
The subroutine first pushes register values in the stack and after using the registers, the subroutine pops the previous values of the registers from the stack while exiting the subroutine. The data is stored in one of the registers. That is the original values are restored before execution returns to the calling program.
Subroutine nesting is calling a subroutine from another subroutine call. It is like calling one function and that function calls another function by itself.
|A function always returns a value.||A subroutine performs a set of tasks and does not return a value.|
|They can be used as formulas in the spreadsheet.||They can not be used directly as formulas in the spreadsheet.|
|They are called by using a variable||They are called by using the call function after their declaration|