Prior Page     Next Page     This Chapter    Prior Chapter    Next Chapter





Subroutine Calls and Returns using TST and JSJ

Subroutine calls are usually done with an unconditional TST jump. That is one with a ccm of 15, or all one bits. This causes the processor to jump to the subroutine and store the current Nr into ir[15]. Thus when you start a subroutine you always know that R15 points back to the point in memory just after the subroutine jump. You could place subroutine arguments there, or you choose some other convention as to where to place them. In all the example in these notes have put arguments right after the subroutine jump.

The subroutine can access arguments by using the effective address calculation to get at them. The first argument will be located where R15 points, so one could load register 5 with the value of the argument using a L 5,0(15) instruction.   The second argument would be located right after the first one, so one would use a L 6,4(15) instruction to get it's value in R6.  

To return to where you came from it is usual to do a JSJ 0,n(15) where `n' depends on the number of argument.  If there was one argument then `n' would be 4, to return you right after the argument. The JSJ is usually used because it leaves Jr untouched; but a TST 15,n(15) would work as well.

Note that subroutines should be polite and not destroy the registers of the calling program.  It is most annoying to the person who wants to call your subroutine to find out the subroutine has destroyed all the registers. So the first thing a subroutine should do is save the registers, and just before it returns it should restore all the registers and then return. Note that any branching in a subroutine will destroy Jr (ir[15]) so you must be careful to save R15 early and restore it before you jump back, or you may suffer serious debugging pangs when your return jump goes astray.








Prior Page     Next Page     This Chapter    Prior Chapter    Next Chapter


Copyright © 1995 R. Uzgalis. All rights reserved.
Contact: buz@cs.aukuni.ac.nz