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

6.2 Passing Parameters                                               355


         * SUBROUTINE DOTPRD - LOCAL VARIABLES
         TERM:     EQU       0            ; First term of the dot product
         MBYTES:   EQU       2
         * PARAMETERS
         LOCV:     EQU       0
         LOCW:     EQU       2
         LOCDP:    EQU       4
         PSIZE:    EQU       6
         *
         DOTPRD:   LEAS     -MBYTES ,SP ; Allocation for local variables
                   LDAA     LOCV+NBTTTES+2, SP
                   LDAB     LOCW+NBYTES+2,SP
                   MUL
                    STD     TERM, SP ; First term to local variables
                   LDAA     LOCV+NBYTES+2+1,SP
                   LDAB     LOCW+NBYTES+2+l,SP
                   MUL
                   ADDD     TERM, SP      ; Dot product into D
                    STD     LOCDP+NBYTES+2, SP ; Dot product to output parameter
                   LEAS     MBYTES, SP    ; Deallocate local variables
                   RTS
           Figure 6,22. Revised Subroutine with Local Variables and Parameters on the Stack
                   LEAS     -PSIZE, SP    ; Allocate space for parameters
                   MOVW     V, LOCV, SP   ; Put copy in parameter location
                   MOVW     W, LOCW, SP   ; Put copy in parameter location
                   BSR      DOTPRD
                   MOVW     LOCDP,SP,DTPD ; Put in global location
                   LEAS     PSIZE, SP     ; Deallocate space for parameters
            Figure 6.23. Calling a Subroutine with Parameters on the Stack for Figure 6.22
            Notice several things about the way this version of the subroutine is written. We do
        not need the local variables to hold copies of vectors V and W as we did in the earlier
        versions because the copies are already on the stack as parameters where we can access
        them using the extended local access technique described in Section 6.1. Because the
        number of parameters and local variables is small and because each is equal to two bytes,
        we can easily calculate the stack offsets ourselves, particularly if we use a dummy
        parameter RETN for the return address. Notice particularly how we have redefined the
        labels LOCV, LOCW, and LOCDP in the subroutine with the EQU directive to avoid
        adding an additional offset of 4 to each parameter to account for the number of bytes in
        the return address and the local variables. Suppose now that we write the subroutine as
        shown in Figure 6.22. When EQU is used in this way, the additional offset of
        MBYTES-1-2 is needed to access the parameters to account for the local variables and the
        return address. No EQU directives are needed in the calling sequence, however, because
        EQU is a global definition; that is, the labels LOCV, LOCW, LOCDP, and PSIZE are
        fixed, respectively, at 0, 2, 4, and 6 throughout the program. The calling sequence for
        this case is shown in Figure 6.23.
   173   174   175   176   177   178   179   180   181   182   183