[net.micro.mac] Toolbox action procedures and C

gijs@ark.UUCP (Gijs Mos) (10/18/85)

(rn crashed. This is a follow-up to some article)

Action procedures or functions are called from toolbox trap code.
The toolbox has been coded with Lisa Pascal in mind. So it uses
the Lisa Pascal procedure/function calling sequence:
	Reserve space on stack for function result (not done for proc)
	Push parameters
	JSR	(Push return address)
The procedure or function saves the return address, removes parameters
and pushes back the return address before returning.

The calling sequence for C functions is different. Consular C - as an
example - expects its first 7 arguments in D0 - D6. Remaining arguments
are pushed on the stack (in reverse order). The function result is in
A0 if pointer and in D0 otherwise.
The solution is to call your C action function from an assembly routine
that converts calling sequences. As an example (again MacC) a piece 
of code for a scroll control action procedure.



DoScrollAction()
/*
	Convert Pascal to C calling sequence and jump to
	ScrollAction
 */
{
#asm
	MOVE.L	(SP)+,A0	;Save return address
	MOVE.W	(SP)+,D1	;partCode
	EXT.L	D1		;MacC int's are 32 bit
	MOVE.L	(SP)+,D0	;myControl
	MOVE.L	A0,-(SP)	;Return address back on stack
	JMP	ScrollAction
#endasm
}


ScrollAction( myControl, partCode )
ControlHandle myControl;
int partCode;
{
	int value, max, min, page, startCount;
	
	startCount = TickCount();
	
	value = GetCtlValue( myControl );
	max = GetCtlMax( myControl );
	min = GetCtlMin( myControl );
	...
	
Literature:
	- IM chapter on Programming applications in Assembly Language
	- Your C compiler's manual
	
Gijs Mos
Dept. of Biology
Vrije Universiteit
Amsterdam
{seismo,decvax,philabs}!mcvax!vu44!gijs