Hardware Reference
In-Depth Information
Use the stack. In this method, parameters are pushed into the stack before the
subroutine is called. The stack must be cleaned up after the computation is
completed. This can be done by either the caller or the callee.
Use the global memory. Global memory is accessible to both the caller and the
callee. As long as the caller places parameters in global memory before it calls the
subroutine, the callee will be able to access them.
4.5.2 Result Returning
The result of a computation performed by the subroutine can be returned to the caller us-
ing three methods:
Use registers. This method is most convenient when there are only a few bytes to
be returned to the caller.
Use the stack. The caller creates a hole of a certain size in the stack before making
the subroutine call. The callee places the computation result in the hole before
returning to the caller.
Use global memory. The callee simply places the value in the global memory and
the caller will be able to access them.
4.5.3 Allocation of Local Variables
In addition to the parameters passed to it, a subroutine may need memory locations to hold
temporary variables and results. Temporary variables are called local variables because they
only exist when the subroutine is entered. Local variables are always allocated in the stack so
that they are not accessible to any other program units.
Although there are several methods for allocating local variables, the most efficient one
is using the leas instruction. This instruction loads the stack pointer with an effective address
specified by the program. The effective address can be any indexed addressing mode except an
indirect address. For example, to allocate 10 bytes, we can use the indexed addressing mode
with SP as the base register and 2 10 as the offset:
leas 2 10, sp ; allocate 10 bytes in the stack
This instruction simply subtracts 10 from the SP and puts the difference back to the SP.
The general format for allocating space to local variables is
leas 2 n,sp ; allocate n bytes in the stack
where n is the number of bytes to be allocated.
Before the subroutine returns to the caller, the space allocated to local variables must be
deallocated. Deallocation is the reverse of allocation and can be achieved by the following
instruction:
leas
n,sp
; deallocate n bytes from the stack
4.5.4 Saving the CPU Registers
A subroutine may use CPU registers to hold local variables to perform certain operations.
Sometimes, the subroutine needs to perform some operations that involve certain CPU regis-
ters. For example, the emul and emuls instructions use register D and Y whereas the ediv and the
edivs instructions use registers D, X, and Y. However, these CPU registers may also be used by
 
Search WWH ::




Custom Search