C (151/304)

From:meyersrl
Date:16 Aug 2000 at 19:48:25
Subject:Re: Just a wee question

On 16-Aug-00, Charles Barr wrote:

> I just have a we question that been enoying me for some time.
> what does __saveds and __asm actually mean?

A compiler may keep register A4 pointing to the start of all global
variables, and then access these as an offset to A4 (because this is a
little faster), but that also means that A4 must always point to the
data segment, so if you have a function which is called from outside
your code (like a hook) you put the save-data-segment keyword in front
of it, to indicate that the compiler should load register A4 before
making access to global variables (as it won't be pre-set). A program
coded 'properly' have very few accesses to global variables, so here it
may actually be better not to compile/link the program to make use of a
data segment register, due to the occasional loads, and the occupied
register -- but I haven't tested it (I always link my code without a DS
register, as it saves me for the trouble of thinking of __saveds).

I have no idea about 'asm' -- it's not supported by StormC. The name
indicates that it's to indicate that the function may e.g. be called
from assembly, so the compiler should do any special tricks, which
require the caller to be aware of them, but I'm only guessing... I'm
looking forward to the real explanation :-)

__asm is an SAS/C specific keyword used during function prototyping
that tells the compiler you want to specify exactly which function
arguments go into which registers, e.g.,

LONG __asm SomeFunc(register __d0 ULONG IntArg1, register __d1 LONG
IntArg2,
register __a0 struct SomeStruct *StructPointer);

In the default mode (no __asm usage) SAS/C puts the first two function
arguments into D0/D1, and the first two pointer arguments into A0/A1,
any other arguments then go on the stack. Also, any return value is
placed into D0. If you need to control which arguments go into which
registers you would then use the __asm keyword, but this is not usually
a good idea since it limits the benefits that the optimizers give you
(since you are constraining them to certain registers).

One specific time you need to use __asm is building an Exec shared system
library or device. Then you need to document which register is for which
argument so that other languages besides C can use your library. In this
case you would be forced to pick the registers with __asm to "lock" the
choice in place.

Anyway there was a thread about this issue a few days back on
c.s.a.programmer,
search Deja.news for "SAS/C parameters in registers".

Bob Meyers