[comp.sys.mac] Calling CODE segments

leeke@glacier.STANFORD.EDU (Steven D. Leeke) (09/28/88)

Let's suppose I want to load a CODE segment from some other file and
call it as a C function - making sure it is declared as a C main
and pass it the correct parameters - one of which is a table of
entry points into the program that is loading it - the "main" program.
The entrypoints are setup as an array of Ptr's which are created dynamically
just before the call. e.g.


	entryPoints	=	NewPtr(sizeof(ProcPtr)*NoOfEntries);
	
	if (entryPoints != NULL) {
		entryPoints[0]	=	<function name>;
		...
	};


LSC reqires me to use their RememberA4/SetupA4 stuff on entry in the
CODE segment and RestoreA4 on exit to get at the globals (if any) in
the CODE segment - so I do. e.g. the CODE segment's main looks like:


void main(p1,p2,p3,...,entryPoints)
{
	RememberA4();
	SetupA4();

	/*
		Intervening code...including jumps back into the
		"main" program - see below.
	*/

	RestoreA4();
};


What I want to know is if A5 is getting bashed when I call the CODE
segment?  I am able to load the segment, lock it, dereference it and
call it successfully e.g.:


	code	=	GetResource(zzzz,yyy);
	
	if (code != NULL) {
		HLock(code);

		(**code)(p1,p2,p3,...,entrypoints);

		HUnlock(code);
	};


Then, I use the array of entrypoints to call various fctns (C) in the
"main" program, e.g.:


	(*entryPoints[0])();


This seems to work well until I try to draw something from within the
"main" program (I don't draw in the code segment) - usually activating
or updating a window when a dialog the CODE segment loaded is being drawn.

So what is it?  Is A5 getting bashed in the CODE segment?  I am using a
project with only the MacTraps library for the CODE segment.  This makes me
think that as soon as I call the code segment the QuickDraw globals are
getting bashed since MacTraps defines them.  e.g. A5 is corrupted.  This
is my prime culprit since crashing only occurs when something is drawn.

I've tried to use SetupA5 right after SetupA4 when I start my CODE segment,
for what it's worth, but it doesn't seem to make a difference.  I'm not
an experienced ASM hacker period - especially on the Mac.

Any help would be appreciated,

Steve Leeke

tim@hoptoad.uucp (Tim Maroney) (09/29/88)

In article <17742@glacier.STANFORD.EDU> leeke@glacier.UUCP (Steven D. Leeke)
writes:
>LSC reqires me to use their RememberA4/SetupA4 stuff on entry in the
>CODE segment and RestoreA4 on exit to get at the globals (if any) in
>the CODE segment - so I do. e.g. the CODE segment's main looks like:
>
>void main(p1,p2,p3,...,entryPoints)
>{
>	RememberA4();
>	SetupA4();
>	/* ... */
>	RestoreA4();
>};

Look at page 85 of the User's Manual.  You need to call RememberA0(), not
RememberA4().  They're very different macros.  Things are getting trashed
because A4 is set to something silly, so all your global accesses are writing
into random memory locations.

>This seems to work well until I try to draw something from within the
>"main" program (I don't draw in the code segment) - usually activating
>or updating a window when a dialog the CODE segment loaded is being drawn.

That's probably just the way it happens; change a few variables and you might
have problems with drawing controls or dragging windows instead.  But if you
really want to know whether A5 is problematic, just drop into MacsBug or TMON
at the beginning of the main program callback routine, and see if A5 is the
same as the low-memory global CurrentA5.  I'm pretty sure there is no reason
to explicitly set A5 to CurrentA5 inside the callback routine, since the
code resource shouldn't modify A5.  But if you want, setting A5 to CurrentA5
in the callback routine couldn't hurt.

>I've tried to use SetupA5 right after SetupA4 when I start my CODE segment,
>for what it's worth, but it doesn't seem to make a difference.  I'm not
>an experienced ASM hacker period - especially on the Mac.

This, I think, confirms my feelings that your problems are with A4.
-- 
Tim Maroney, Consultant, Eclectic Software, sun!hoptoad!tim
"I see little divinity about them or you.  You talk to me of Christianity
 when you are in the act of hanging your enemies.  Was there ever such
 blasphemous nonsense!" - Shaw, "The Devil's Disciple"