No. You would like to call a useful subroutine from many locations in a program.
Especially useful subroutines are put into subroutine libraries and can be called from many locations in many programs, and so, of course, they must return to many different locations.
There seems to be a problem with using j
for the call of and return from a subroutine.
T he
problem is illustrated at right.
The main
routine is written
to call a useful subroutine
sub
at several locations
in the code.
But, as it is written, sub
always returns
control to the same location.
Usually this will not work.
In the past, before the concept was completely understood, hardware support for subroutines was missing. Various nasty tricks were used to implement the idea.
What is needed is a way to send a return address to the subroutine when it is called. Now when the subroutine finishes, it passes control to that return address.
Of course, "passing control to a return address" means that the subroutine loads the PC (program counter) with the return address. The next instruction fetch of the machine cycle will get the instruction from that address.
(Hardware Design Question: ) How should the return address be passed to the subroutine? (i) By placing it in main memory somewhere, or (ii) By placing it in a register designated for this purpose.