Файл: Introduction to Microcontrollers. Architecture, Programming, and Interfacing of the Motorola 68HC12 (G.J. Lipovski, 1999).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 1407
Скачиваний: 0
6.2 Passing Parameters |
153 |
which it wants to compute the dot product, call the subroutine,and get the dot product result from DTPD. Note also that
ADDD 2 ,SP+ ; Dot product into D, also deallocate local variable
rendered the last LEAS instruction of the subroutine unnecessary.
MOVE |
LV,SP,V1 |
;CopyV(l) |
|
MOVE |
LV+1,SP, V2 |
; Copy V(2) |
|
MOVE |
LW,SP,W1 |
;CopyW(l) |
|
MOVE |
LW+1, SP ,W2 |
; Copy W(2) |
|
BSR |
DOTPRD |
||
MOVW |
DTPD, LDP, SP ; Place result in local variable LDP |
||
Figure 6.18. Calling a Subroutine for Figure 6.17 |
|||
aLOCV: |
EQU |
0 |
; Input parameter copy of the vector V |
aLOCW: |
EQU |
2 |
; Input parameter copy of the vector W |
aLOCDP: |
EQU |
4 |
; Output parameter copy of dot product |
PSIZEj |
EQU |
6 |
; Number of bytes for parameters |
* |
|||
LEAS |
-PSIZE,SP |
MOVW |
V, aLOCV, S P |
MOVW |
W, aLOCW, SP |
BSR |
DOTPRD |
;Allocate space for parameters
;Initialize parameter LOCV,SP
;Initialize parameter LOCW,SP
MOVW |
aLOCDP, SP, DTPD ; Place output in global variable |
|
LEAS |
PSIZ E , S P |
; Deallocate space for parameters |
Figure 6.19. Calling a Subroutine with Parameters on the Stack for Figure 6.2]
We now consider a very general and powerful method of passing parameters on the stack. We illustrate the main idea, interpreting it as another use of local variables, as well as the technique that makes and erases "holes" in the stack, and we consider variations of this technique that are useful for very small computers and for larger microcontrollers like the 68332.
Input and output parameters can be passed as if they were local variables of the program segment that consists of the calling sequence that allocates and initializes. The local variables are allocated and initialized around the subroutine call. In this mode the parameters are put on the stack before the BSR or JSR. For our particular dot product example, the calling sequence might look like Figure 6.19.
For simplicity, we have assumed that input parameter values come from global variables V and W, and the output parameter is placed in the global variable DTPD.All of these global variables could, however, just as well have been local variables of the calling routine. The idea is exactly the same. The stack is as shown in Figure 6.20 as execution progresses. The dot product subroutine is now as shown in Figure 6.21.
6,2 Passing Parameters |
157 |
*SUBROUTINE DOTPRD - LOCAL VARIABLES
TERM: |
EQU |
0 |
; First term of the dot product |
NBYTES: |
EQU |
2 |
*PARAMETERS
PARV: |
EQU |
0 |
; Copy of vector V |
PARW: |
EQU |
2 |
; Copy of vector W |
PARDP: |
EQU |
4 |
; Dot product of V and W |
PSIZE: |
EQU |
6 |
|
* |
|||
DOTPRD: |
PULX |
; Return address into X |
|
LEAS |
-NBYTES, SP |
; Allocation for local variables |
|
LDAA |
PARV, X |
||
LDAB |
PARW,X |
||
MUL |
|||
STD |
TERM, SP |
; Copy first term into local variable |
|
LDAA |
PARV+1,X |
||
LDAB |
PARW+1,X |
||
MUL |
|||
ADDD |
TERM,SP |
; Dot product into D |
|
STD |
PARDP, X |
; Place dot product in out parameter |
|
LEAS |
NBYTES, SP |
; Deallocate local variables |
|
JMP |
PSIZE, X |
Figure 625. A Subroutine with Parameters after the Call, which Pulls the Return
PARV: |
EQU |
0 |
|
PARW: |
EQU |
2 |
|
PARDP: |
EQU |
4 |
|
* |
|||
MOVW |
V, PARV+L, PCR |
; Copy of V into parameter list |
|
MOVW W,PARW+L,PCR |
; Copy of W into parameter list |
||
BSR |
DOTPRD |
||
L:DS 6
MOVW PARDP+L, PCR, DTPD ; Copy result into DTPD
Figure 626. A Subroutine Calling Sequence for Figure 6.25
rather than a kludge of special methods that are restricted to limited sizes or applications. The compiler has less to worry about and is smaller because less code in it is needed to handle the different cases. This means that many subroutines that you write for highlevel languages such as C may require you to pass arguments by the conventions that it uses. Moreover, if you want to use a subroutine already written for such a language, it will pass arguments that way. It is a good idea to understand thoroughly the stack mode of passing parameters.
158 |
Chapter 6 Assembly Language Subroutines |
*SUBROUTINE DOTPRD
*LOCAL VARIABLES
TERM: |
EQU |
0 |
; First term of the dot product |
MBYTES: EQU |
2 |
||
* |
|||
* |
PARAMETERS |
||
* |
|||
PARV: |
EQU |
0 |
; Copy of vector V |
PARW: |
EQU |
2 |
; Copy of vector W |
PARDP: |
EQU |
4 |
; Dot product of V and W |
* |
|||
DOTPRD: LDX |
0, SP |
; Return address into X |
|
LEAS |
-NBYTES, SP ; Allocation for local variables |
||
LDAA |
PARV+2,X |
||
LDAB |
PARW+2,X |
||
MUL |
|||
STD |
TERM, SP |
; Copy first terra into local variable |
|
LDAA |
PARV+1+2,X |
||
LDAB |
PARW+1+2,X |
||
MUL |
|||
ADDD |
TERM,SP |
; Dot product into D |
|
STD |
PARDP+2, X |
; Place dot product in out parameter |
|
LEAS |
NBYTES, SP |
; Deallocate local variables |
|
RTS |
|||
Figure 6.27. A Subroutine with Parameters after the Call, which Uses RTS
entry: MOVW V, PARV+L, PCR MOW W,PARW+L,PCR BSR DOTPRD
BRA LI
*
L: DS 6
*
;Copy of V into parameter list
;Copy of W into parameter list
L1: |
MOVW PARDP+L, PCR, DTPD ; Copy parameter list into DTPD |
Figure 6.28. A Subroutine Call with Parameters after the Call for Figure 6.27
We now consider another common method of passing arguments in which they are put after the BSR or equivalent instruction. This way, they look rather like addresses that are put in the instruction just after the op code. Two variations of this technique are discussed below.
162 |
Chapter 6 Assembly Language Subroutines |
*SUBROUTINE DOTPRD
*PARAMETERS
*
PARV: |
EQU |
0 |
; Copy of vector V |
PARW: |
EQU |
2 |
; Copy of vector W |
PARDP: |
EQU |
4 |
; Dot product of V and W |
* |
|||
* |
LOCAL VARIABLES |
||
* |
|||
TERM: |
EQU |
0 |
|
MBYTES: EQU |
2 |
||
DOTPRD: LEAS |
-NBYTES, SP ; Allocation for local variables |
||
LDAA |
PARV,X |
||
LDAB |
PARW,X |
||
MUL |
; First term of DP into D |
||
STD |
TERM, SP |
; Store in local variable |
|
LDAA |
PARV+1,X |
||
LDAB |
PARW+1,X |
||
MUL |
; Second term into D |
||
ADDD |
TERM,SP |
||
STD |
PARDP, X |
; Place dot product |
|
LEAS |
NBYTES, SP |
; Deallocate local variables |
|
RTS |
|||
a. Thesubroutine
LDX fTABLE
MOVW V, PARV, X ; Place copy of V into parameter
MOVW W, PARW, X ; Place copy of W into parameter
BSR DOTPRD ; Call Subroutine
MOVW PARDP, X, DTPD ; Copy result into global variable
b. Calling sequence
Figure 633. Calling Sequence for Passing Arguments in a Table
6.3 Passing Arguments by Value, Reference, and Name
Computer science students, as opposed to electrical engineering students, study the passing of parameters in high-level language subroutines on a different level than that used in the preceding section. We include this section to explain that level to you. On the one hand, this level is very important if, say, you are writing or using a subroutine that is used by a high-level language program and that subroutine has to conform to the properties discussed below. On the other hand, the differentiation between some of the characteristics discussed below is rather blurry in assembly language programs.