Page 187 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 187
164 Chapter 6 Assembly Language Subroutines
used to call and return from a program segment that is very much like a subroutine. Such
a calling and returning mechanism is used in the Motorola 500 series of RISC
microcomputers discussed in Chapter 12. In the spirit of showing design alternatives, we
will survey these techniques, pointing out advantages and disadvantages of each approach.
As we proceed, we will discuss several related important topics. We will look at
hardware interrupts and at the fork and join mechanisms used in timesharing. These
important topics are best covered in this section on calling and returning mechanisms.
There are alternatives to the most commonly used BSR/RTS mechanisms. We first
discuss alternative returning mechanisms and then alternative calling mechanisms.
The alternative to the returning mechanism can be used to greatly improve the
clarity of your programs. A significant problem in many programs that call subroutines
occurs when the subroutine alters the contents of a register, but the writer of the calling
routine does not think it does. The calling routine puts some number in that register,
before calling the subroutine, and expects to find it there after the subroutine returns to
this calling routine. It will not be there. Two simple solutions to this problem come to
rnind. One is to assume all subroutines will change any register, so the calling routine
will have to save and restore any values in registers that it wants to save. The other is to
assume that the subroutine will save and restore all registers except those used for output
parameters. The latter solution is generally more statically efficient because any
operation done by a subroutine requires instructions that are stored just once, where the
subroutine is stored, whereas any operation done in the calling routine requires
instructions to be stored at each place that the subroutine is called.
SUB: TFR X,D
LEAY D,Y
RTS
Figure 634. Simple Subroutine
SUB: PSHD ; Save D
TFR X f D
LEAY D,Y
PULD ; Restore D
RTS ; Return
Figure 635. A Subroutine Saving and Restoring Registers
Suppose that a subroutine is called ten times. Then the former solution needs ten pairs of
program segments to save and restore the register values. The latter solution requires
only one pair of segments to save and restore the registers.
Consider the simple example in Figure 6.34 to add the contents of the register X to
that of register Y without altering any register except Y. If we use TFR X, D, followed
by LEAY D, Y, accumulator D is changed. However, as shown in Figure 6.35, if we
save D first and restore it after the addition is done by the LEAY instruction (which
doesn't affect condition codes), we don't affect other registers when we add X to Y. This
technique for saving registers can be used to save all the registers used in a subroutine
that do not return a result of the subroutine.