Subroutines

·

2 min read

1.What is subroutine? => Subroutine is repeatative block of code for example sort function.

2.Why and how we use subroutine? => We place this repeatative code somewhere in memory and whenever we require that code we branch or jump to that code using CALL instruction.

3.What after executing the subroutine? => Before subroutine PC contents are saved somewhere and when RETURN instruction is applied by subroutine saved PC content is fetched and normal code execution begins

4.What are different ways of calling a subroutine? => processor stack method & subroutine linkage method

5.Give an inside view of CALL instruciton => 1. Store the current value in link register

  2. Branch to the address denoted by CALL instruction
  3. Branch back to the address stored in link register

6.What is subroutine nesting? => It is like nested for loop.

7.Why we cant use link register for subroutine nesting? => If we use link register for subroutine nesting then PC value of previous subroutine will get lost.

8.What & why we will use that for subroutine nesting instead of link register? => As generating of addressess follow last in first out order & we have problem loseing PC address of previous subroutine thats why we will use stack.To avoid memory operation time we will use stack that present in processor or that is near to processor that is processor stack.

9.Why we need parameter passing in subroutines? => Asking this question is like why we need input for machines if we want desired output,lol.Ok back to concept,take example of add(a,b) here add() is subroutine and a& b are parameters required for computation and a+b is result of computation,in order to use them we need to pass them ,this is called parameter passing.

Where in computer are these parameters stored? => parameters are stored in memory location or registers or processor stack.