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

6.2 Passing Parameters                                               161


            Before we look at other argument passing techniques using our running inner
        product subroutine example, we illustrate a common subroutine used in C and C++
        compilers to implement the case statement by means of an in-line argument list of
        addresses. Figure 6.31 illustrates how, when a variable between 0 and 3 is in accumulator
        B, the program can jump to label LO if the variable is 0, to LI if the variable is 1, to L2
        if the variable is 2, and to L3 if the variable is 3. For each case, an address is put in the
        in-line argument list; the SWITCH subroutine reads one of these arguments into the PC,
        as selected by the value in accumulator B. Note that this technique is more efficient than
        a decision tree (Figure 2.14) when the same variable is tested for numbers, which happen
        to be consecutive, going to a program segment that the number indicates. However, this
        example is neither position independent nor does it use 8-bit in-line arguments to further
        improve efficiency. The reader is encouraged to improve this technique to do this.





                                                       TABLE: DS 6
                                                       PARV: EQU 0
                                                       PARW: EQU 2
                                                       PARDP: EQU 4



                                                    b. Assembler directives


                              Figure 632. Parameters in a Table




            We now consider the technique of passing parameters via a table. The argument list,
        which is in-line when parameters are passed after the call, can be a table stored anywhere
        in memory. This technique is quite similar to passing parameters after the call. For our
        example, suppose that one uses a table whose address is passed in X and that looks like
        Figure 6.32, where, as before, the suffixes H and L stand for the high and low bytes of
        the 2-byte parameters PARV, PARW, and PARDP. The subroutine shown in Figure 6.33a
        is called by a sequence shown in Figure 6.33b.
            Passing parameters by a table is often used to control a floppy disk in a way that is
        transparent to the user. The number of parameters needed to control a disk can be very
        large; therefore the table can serve as a place to keep all the parameters, so only the
        address of the table is sent to each subroutine that deals with the floppy disk.
            In this section, we considered ways to pass arguments to and from subroutines. The
        register technique is best for small subroutines that have just a few arguments. The stack
        technique is best for larger subroutines because it is the most general. The in-line
        argument list that passes parameters after the call is used in FORTRAN subroutines, and
        the table technique is commonly used in operating system subroutines. The technique
        that passes parameters in global variables was covered for completeness and is useful in
        very simple micrcontrollers such as the 6805, but it is discouraged in the 6812.
   179   180   181   182   183   184   185   186   187   188   189