Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
46
Добавлен:
27.04.2015
Размер:
4.26 Mб
Скачать

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

; Pointer variable to be passed to subroutine

 

LEA

(SP)+

; Push variables onto stack

 

MOVE

X0,X:(SP)+

 

 

MOVE

X:$21,X0

; First data variable to be passed to subroutine

 

MOVE

X0,X:(SP)+

; Push onto stack

 

MOVE

X:$47,X0

; Second data variable to be passed to

 

 

 

; subroutine

 

MOVE

X0,X:(SP)

; Push onto stack

 

JSR

ROUTINE1

 

 

POP

 

; Remove the three passed parameters from

 

POP

 

; stack when done

 

 

 

 

POP

 

 

ROUTINE1

 

 

 

MOVE

#5,N

; Allocate room for local variables

 

LEA

(SP)+N

 

;

(instructions)

 

 

MOVE

X:(SP-9),r0

; Get pointer variable

 

MOVE

X:(SP-7),B

; Get second data variable

 

MOVE

X:(R0),X0

; Get data pointed to by pointer variable

 

ADD

X0,B

 

 

MOVE

B,X:(SP-8)

; Store sum in first data variable

;(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

DSP56800 Family Manual

 

 

Time-Critical DO Loops

 

X Data Memory

SP

Fifth Local Variable

 

Fourth Local Variable

 

Third Local Variable

 

Second Local Variable

 

First Local Variable

 

Status Register

 

Return Address

 

Third Passed Parameter

 

Second Passed Parameter

 

First Passed Parameter

 

AA0092

Figure 8-1. Example of a DSP56800 Stack Frame

8.9 Time-Critical DO Loops

Often, a program spends most of its time in time-critical loops. For the efficient execution of these loops, it is important to have an adequate number of registers. However, sometimes the registers already contain data that is not necessary for the critical loop but must not be lost. In this case the DSP56800 architecture provides a convenient mechanism for freeing up these registers using the software stack. The programmer pushes any registers containing values not required in the tight loop, freeing up these registers for use. After completion of the loop, these registers are popped. An example is shown in the following code.

 

Software Techniques

8-29

Соседние файлы в папке DSP568xx