Sample SPIM for procedure call/return

Function f will have two integer parameters named x and y that are passed by value and it returns a single integer. Also in function f are two local variables a and b.

Static Allocation

Stack allocation

In the following, all local information is maintained in the stack and accessable via the stack pointer ($sp). In the stack activation record the following offsets are used:
offset(s)         Data              size
  0               return address      4		
  4               old frame ptr       4		
		    (dynamic link)
  8               t registers        32
 40               a registers        16
 56               local var 1         4
 60               local var 2         4
 64               local var 3         4
  .
  .
  .
The stack pointer appears to be set correctly to point at the 'top' of the stack when SPIM execution starts. The frame pointer always points to the last word of the current activation. I maintain this setup in the code below, although it is not clear whether I need maintain the frame pointer or not.

In the code below, the 'push' assumes that the overall activation record size is 104, which corresponds to space for 12 local variables in the activation. Obviously, different functions can have different size activations.