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

6.2 Passing Parameters                                              159


        *   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
         *
         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
                 LDY     PARV,X
                  LDAA 1, Y
                 LDY     PARW,X
                 LDAB 1, Y
                 MUL
                 ADDD    TERM, SP    ; Dot product into D
                  STD    [ PARDP, X ] ; Place dot product in out parameter
                  LEAS   NBYTES, SP ; Deallocate local variables
                  JMP    0, X
               Figure 6.29. A Subroutine With In-Line Parameters that Are Addresses
                         BSR    DOTPRD
                         DC.W ADDRV          ; Address for V
                         DC.W ADDRW          ; Address for W
                         DC.W ADDRDP         ; Address for dot product
                Figure 630. An In-Line Argument List of Addresses for Figure 6.29
            In the first alternative, the parameters are placed after the BSR or JSR instructions
        in what is called an in-line argument list. Looking at Figure 6.24, we see that the return
        address, which is pushed onto the hardware stack, points to where the parameters are
        located. When parameters are passed this way, sometimes referred to as after the call, the
        subroutine has to increment the return address appropriately to jump over the parameter
        list. If this is not done, the MPU would, after returning from the subroutine, try to
        execute the parameters as though they were instructions. For our dot product example,
        assume that the parameter list appears as shown in Figure 6.24. Notice that the
        subroutine must skip over the six bytes in the parameter list when it returns to avoid
        "executing the parameters." The subroutine shown in Figure 6.25 does this.
   177   178   179   180   181   182   183   184   185   186   187