In: Computer Science
4. Explain the purpose of jump-and-link(jal) instruction. (10%)
Question 4 :
Solution:-
The Jump and Link instruction usually known as JAL instruction.These instructions are mainly used for jumping to a subprogram or function and also to return from the subprograms.The JAL perform an unconditional jump to the address specified and store the address of the next instruction in the return address register or $ra register. The next instruction refers to the instruction specified in the next address after jal.
The purpose of JAL instructions and jump instructions are almost similar because both instructions jump to the address provided except that, in jal instruction it saves the address of the next instruction in the $ra register.Hence,JAL instruction allows a subroutine or a function to return to the main program or simply we can say that ,it allows a subroutine to return to the main program that responsible for the call to that function.Thus linking the subprogram to the main program.
For example,
Consider a main program with the label say mainprog and a subroutine with label subprogram :
Here ,
Another example explaining the detailed flow of control over main program and subroutine is given below :
The diagram explains how the flow of execution happens while using jal instruction.The flow of execution described in step by step is given below :