Software Techniques
8.8
Parameters and Local Variables
The DSP56800 software stack supports structured programming techniques, such as parameter passing to
subroutines and local variables. These techniques can be used for both assembly language programming
and high-level language compilers.
Parameters can be passed to a subroutine by placing these variables on the software stack immediately
before performing a JSR to the subroutine. Placing these variables on the stack is referred to as building a
"stack frame." These passed parameters are then accessed in the called subroutines using the stack
addressing modes available on the DSP56800. This is demonstrated in the following example (which
destroys the x0 register):
; Example of Subroutine Call With Passed Parameters
MOVE
X:$35,X0
LEA
(SP)+
MOVE
X0,X:(SP)+
MOVE
X:$21,X0
MOVE
X0,X:(SP)+
MOVE
X:$47,X0
MOVE
X0,X:(SP)
JSR
ROUTINE1
POP
POP
POP
ROUTINE1
MOVE
#5,N
LEA
(SP)+N
;
(instructions)
MOVE
X:(SP-9),r0
MOVE
X:(SP-7),B
MOVE
X:(R0),X0
ADD
X0,B
MOVE
B,X:(SP-8)
;
(instructions)
MOVE
#-5,N
LEA
(SP)+N
RTS
In a similar manner it is also possible to allocate space and to access variables that are locally used by a
subroutine, referred to as local variables. This is done by reserving stack locations above the location that
stores the return address stacked by the JSR instruction. These locations are then accessed using the
DSP56800's stack addressing modes. For the case of local variables, the value of the stack pointer is
updated to accommodate the local variables. For example, if five local variables are to be allocated, then
the stack pointer is increased by the value of five to allocate space on the stack for these local variables.
When large numbers of variables are allocated on the stack, it is often more efficient to use the (SP)+N
addressing mode.
It is possible to support passed parameters and local variables for a subroutine at the same time. In this case
the program first pushes all passed parameters onto the stack (see Figure 8-1) using the technique outlined
in Section 8.5, "Multiple Value Pushes." Then the JSR instruction is executed, which pushes the return
address and the SR onto the stack. Upon being entered, the subroutine first allocates space for local
variables by updating the SP. Then, both passed parameters and local variables can be accessed with the
stack addressing modes.
8-28
; Pointer variable to be passed to subroutine
; Push variables onto stack
; First data variable to be passed to subroutine
; Push onto stack
; Second data variable to be passed to
; subroutine
; Push onto stack
; Remove the three passed parameters from
; stack when done
; Allocate room for local variables
; Get pointer variable
; Get second data variable
; Get data pointed to by pointer variable
; Store sum in first data variable
DSP56800 Family Manual