Contents | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Previous | Next


6. Procedure Calls

Every language has procedure calls or some equivalent feature. To implement these, you need a way of saving your place in the calling program so that you can return to it later.


Calling and Returning Top

To see how the Transputer manages procedure calls, load call1 into the simulator. The first five instructions form the main program, and the remaining instructions form a squaring procedure. The main program is:

         ldc 7
         call 4
         ldc 7
         sub
         stopp

The first ldl instruction loads 7 into Areg as the argument to the procedure. The call instruction does several things. First, it reserves 4 words of workspace, as if the instruction ajw -4 had been executed. Next, it stores the three data registers Areg, Breg and Creg into three of the new spaces, as if the instructions stl 1 and stl 2 and stl 3 had been executed. These are treated as the arguments to the procedure. If there are less than three arguments, some of the slots are unused, as in the example program. If there are more than three arguments, the calling program has to have extra instructions before and after the call to allocate more slots for them in the workspace and to fill the slots in. Next, the call instruction saves its place in the program by storing Iptr in the remaining workspace slot. The value of Iptr stored is the address of the instruction immediately following the call, ie the place where execution should continue when the call returns. Finally, the call instruction jumps to the procedure being called as if with the j instruction using the argument (4 in the example) as the number of instruction bytes to skip.

The procedure consists of the instructions:

         ldl 1
         ldl 1
         mul
         ret

The ldl instructions load up two copies of the first (and only) argument to the procedure. The mul instruction forms the square of the argument, which is returned in Areg to the caller.

The ret instruction undoes what the call instruction did. First, it loads the saved value of Iptr back into Iptr, which acts like a jump back to where the call came from. Then it gives back the space which was allocated by the call using the equivalent of the ajw 4 instruction.


Contents | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Previous | Next


Copyright 1998 University of Bristol. All rights reserved.
Author: Ian Holyer
Last modified: 14 Sep 1998 11:31
Authored in CALnet