[comp.sys.atari.st] Turbo C parameter passing

edwin@ruuinf.UUCP (Edwin Kremer) (01/24/89)

		Some peculiarities of the Heimsoeth & Borland
		    Turbo C compiler for the Atari 1040ST.

One should *really* read Chapter 7 "System Programming" in the
manual, especially the paragraph describing how a function gets its
parameters; this might save you from a lot of trouble when porting software.

Turbo C intends to produce fast code by using REGISTERS for passing
parameters rather than the stack. Since it's easy to run out of registers,
the following convention is used:

	- pointer parameters: the first one goes via A0, the second
	  via A1, remaining pointer parameters go on stack.
	- other_type parameters: the first one goes via D0, the second
	  via D1 and the third via D2, remaining parameters go on stack.
	- doubles go via A0/A1, floats via D0/D2, any remaining parameters
	  go on stack of course.

	Functions with a variable number of parameters (printf) ONLY
	use the stack and MUST be defined by prototyping (see stdio.h)
	before calling, otherwise some of its parameters will be passed
	in registers whereas printf expects them on stack (will surely bomb).
	The following well known program is doomed to fail,

			main()
			{
				printf("Hello world\n");
			}
		
	if 'printf' is not defined being a function that takes a
	variable number of parameters. In the above situation, a
	pointer to the string "Hello world\n" is passed via A0
	instead of the stack where 'printf' expects it to be !!!


One of the things that follows from this is that it's no longer possible
to use (or misuse :-) the fact that a pointer and a long are both 32-bits
wide. Trying to call a function expecting a pointer, with a long will
possibly crash, because the function uses A0 whilst the value is in D0 !!!


Something I learned (after dealing with a lot of bombs) is that you'd
better prototype any function you define, be sure the needed include files
are really included and NOT use the "-W-pro" compiler option. This option
supresses warnings like 'calling function Foo with no prototype....'.
A real pre of this kind of prototyping is, that the compiler itself
casts parameters to their correct type when necessary.


Well, I hope this is of any help to Turbo C users; please post known
bugs and other oddities (or features :-) to this newsgroup. I'll be
in touch.

					--[ Edwin ]--


-- 
Edwin Kremer, Department of Computer Science, University of Utrecht
Padualaan 14,  P.O. Box 80.089,  3508 TB  Utrecht,  The Netherlands
Phone: +31 - 30 - 534104        |  UUCP    : ...!hp4nl!ruuinf!edwin
    "I speak for myself ..."    |  INTERNET: edwin@cs.ruu.nl