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

6.4 Calling And Returning Mechanisms                                163


            The most important characteristic of a parameter is whether you pass the value of
         the parameter to the subroutine, or the address of the parameter to the subroutine. In the
         example used throughout §6.2, values of the vectors and the dot product usually were
         passed to and from the subroutine rather than the addresses of these arguments. (The one
         exception was in the discussion of passing parameters after the call, where constant
         addresses were passed in the argument list.) If the value of a parameter is passed, we say
         the parameter is passed or called by value. Output parameters passed by value are also
         said to be passed or called by result. If the address of the parameter is passed, the
         parameter is passed by reference or by name as we describe below.
            The passing of parameters by value is completely general but could be time
         consuming. Consider a simple example where the string STRING of ASCII characters,
         terminated by an ASCII carriage return ($OD), is passed one character at a time into the
         subroutine, and the string is up to 100 characters long. Clearly, a lot of time would be
         used copying the characters into parameter locations. Were there enough registers, we
         would have to load 100 bytes into registers before calling the subroutine. A hundred
         bytes would be moved to global memory using the global technique, or 100 bytes would
         be pushed on the stack using the stack technique. Obviously, it is more efficient to pass
         an address rather than these values. For this case, the address would be the address of the
         first character of the string, or the label STRING itself. The parameter is called by
         reference, or is called by name. There is a slight difference between call by reference and
         call by name, but in assembly language, this difference is not worth splitting hairs
         about. We will refer to this technique, where the address of the data is passed as an
         argument, as call by name. Figures 6.29 and 6.31 are examples of subroutines that pass
         parameters by name. The other figures in this chapter called input parameters by value
         and called output parameters by result.
            Call by name is useful when the parameters themselves are subroutines as, for
         example, in a subroutine that integrates the function FUN between 0 and 1. In this case,
         the function FUN could be supplied as an argument to an integration subroutine and it is
         reevaluated each time the subroutine calculates a new point of FUN. For example, the
         call to FUN may supply a starting point and an increment delta. The nth call to the
         subroutine returns the value of function FUN(x) at x = starting point + (n - 1) * delta.
         This continues until the calling routine changes the value of the starting point. Each call
         to FUN inside the integration subroutine returns a different value for FUN. This, then, is
         an example of call by name, because the address (of a subroutine) is passed.
            In this section, we have described the types of information that are passed about
         parameters. The most important distinction is whether a value is passed (a call by value
         or call by result) or whether an address is passed (a call by name or a call by reference).


         6.4 Calling and Returning Mechanisms

         The 6812 provides several mechanisms to call a subroutine and return from it. The
         standard subroutine uses BSR, or the equivalent instruction, to call the subroutine and
         RTS, or the equivalent instruction, to return from it. We have used this mechanism in
         the earlier sections of this chapter. However, there are the SWI and RTI instructions that
         can be used for the software interrupt handler; the LDX #RETURN instruction can be
   181   182   183   184   185   186   187   188   189   190   191