[fa.info-mac] register saving conventions

info-mac@uw-beaver (12/21/84)

From: Mike Schuster <MIKES@CIT-20.ARPA>

I'm sure most everyone is familiar with the Toolbox register saving
conventions, in which each stack based routine preserves the contents
of all registers except A0, A1, and D0-D2.  There are, however, some
less well known points which are worth noting.

Consider the toolbox routine TrackControl, which is called when the
mouse is pressed in a visible, active control.  TrackControl follows
the movements of the mouse and calls the optional ActionProc parameter
repeatedly for as long as the mouse button is down.

The important points are these: 
   1) Registers are not restored to their states before the
      TrackControl call when the ActionProc is called.
   2) ActionProc must preserve the contents of all registers except
      A0, A1, and D0-D2.  
Of course, these points apply not only to TrackControl, but any
situation where the toolbox calls one of your routines.

Failing to note these points can lead to subtle bugs.  Consider a desk
accessory that maintains a text window with some scroll bars.  Suppose
the the desk accessory dedicates register A4 to point to its private
storage (a common practice).  When ActionProc is called to scroll the
text, A4 will not have its proper value and must be restored.
Furthermore, before it is restored, the current A4 value (and the
other registers too) must be preserved for proper operation of
TrackControl.

Restoring the value of A4 can be tricky.  Here is what I do: Just
before the TrackControl call, I save A4 in some location using PC
relative addressing.  Then ActionProc first saves registers D2-D7 and
A2-A4 and then loads A4, using PC relative addressing.  There are
other possibilities, but I found this technique to be most convenient.

Mike Schuster
mikes@cit-20
-------