Page 176 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 176

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, S P, 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       ; Allocate space for parameters
                   MOVW     V, aLOCV, S P ; Initialize parameter LOCV,SP
                   MOVW     W, aLOCW, SP     ; Initialize parameter LOCW,SP
                   BSR      DOTPRD
                   MOVW     aLOCDP, SP, DTPD ; Place output in global variable
                   LEAS     P SIZ 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.
   171   172   173   174   175   176   177   178   179   180   181