Path: utzoo!utgpu!attcan!uunet!peregrine!elroy!ames!ncar!husc6!rutgers!apple!darin
From: darin@Apple.COM (Darin Adler)
Newsgroups: comp.sys.mac.programmer
Subject: Re: Register saving conventions
Message-ID: <15317@apple.Apple.COM>
Date: 9 Aug 88 23:27:51 GMT
References: <664@iraun1.ira.uka.de> <1133@lzsc.ATT.COM> <15055@santra.UUCP> <6600@umn-cs.cs.umn.edu>
Reply-To: darin@apple.apple.com.UUCP (Darin Adler)
Organization: Apple
Lines: 53

In <6600@umn-cs.cs.umn.edu> imp@crayview.msi.umn.edu (Chuck Lukaszewski) writes:
> In article <15055@santra.UUCP>, jmunkki@santra.HUT.FI (Juri Munkki) writes:
> > In article <1133@lzsc.ATT.COM> mkg@lzsc.ATT.COM (Marsh Gosnell) writes:
> > >When I patch traps I make it a point to save/restore ... (D0-D7/A0-A4)
> 
>In point of fact, given the above about D0-D2/A0-A1, you need no register store
>code at all because the trap dispatcher handles the other registers for you, so
>you're just wasting stack space and CPU cycles.

Wrong!

Here is what happens:

OS traps
--------
Callers:	expect result in D0 (and possibly A0), nothing else trashed
Dispatcher:	saves D1-D2/A1 and sometimes A0, depending on the trap word
Traps:		can trash D0-D2/A0-A1, must preserve D3-D7/A2-A6

Toolbox traps (except Resource Manager)
---------------------------------------
Callers:	expect D0-D2/A0-A1 trashed
Dispatcher:	saves nothing
Traps:		can trash D0-D2/A0-A1, must preserve D3-D7/A2-A6

Resource Manager traps (except LoadResource)
--------------------------------------------
Callers:	expect D0/A0 trashed
Dispatcher:	saves nothing
Traps:		can trash D0/A0, must preserve D1-D7/A1-A6

LoadResource
------------
Callers:	expect nothing trashed
Dispatcher:	saves nothing
Traps:		must preserve D0-D7/A0-A6

You MUST save registers D2-D7/A2-A6 if you use them in a routine. Usually, you
can't use A5, since most routines (QuickDraw, the Toolbox) requires that it
point at QuickDraw globals. Also, the LINK and UNLK instructions save the
specified register, so it is not usually necessary to save A6. If the patch that
you are writing is not called often (or not in time-critical places), saving
all the registers is probably an OK idea, since it's almost always safe, and
prevents the subtle errors that can occur otherwise.

By the way, I agree with Juri about the need for a "register scramble" function
to test applications and system software with. There are currently a number of
places in the ROM and System that do not follow the above rules. For example,
there are a number of places in the ROM that rely on SetPort changing only A0!
--
Darin Adler					       AppleLink: Adler4
UUCP: {sun,voder,nsc,mtxinu,dual}!apple!darin	  CSNET: darin@Apple.com