Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!oberon!skat.usc.edu!blarson From: blarson@skat.usc.edu (Bob Larson) Newsgroups: comp.lang.c Subject: Re: Passing Arguments In C Keywords: What Goes On Under The Covers? Message-ID: <12241@oberon.USC.EDU> Date: 18 Sep 88 18:04:22 GMT References: <2232@ssc-vax.UUCP> Sender: news@oberon.USC.EDU Reply-To: blarson@skat.usc.edu (Bob Larson) Organization: USC AIS, Los Angeles Lines: 49 In article <2232@ssc-vax.UUCP> dmg@ssc-vax.UUCP (David Geary) writes: > I'm trying to get a grasp on what actually goes on under the covers >when passing arguments in C. [...] >When main() calls DoIt(), the values of a,b, and c are loaded on >a stack somewhere in memory. When main() calls DoIt(), the arguements are calculated in random order then passed via whatever trick the compiler writer thought approprate. (Stack? Why do you assume there is one?) >Am I thinking correctly? Your sceme should work, but it certainly isn't the only or even the most common way. >Does the compiler have the freedom >to, say, grow the stack towards the end of memory instead of the beginning? The compiler can use anything that works. Os9/68k passes the first two arguments in registers, Prime 64v mode C creates temporaries then passes the address of them, etc. >Where is all this kind of stuff spelled out? It isn't spelled out on purpouse. The compiler writer can choose what is best under the circumstances. (K&R, K&R II, ansi drafts, and H&S are possible referneces.) >I teach an Advanced C class, and am showing my students how to write variadic >functions. There are two semi-portable ways of doing it, known as varargs and stdargs. (varargs comes from bsd, stdargs from ansi.) If using varargs, remember to get ALL arguments to the function that way. (Some varargs implemetations depend on this.) stdargs implementations are still rare, and will frequently require compiler support. For C implementations that don't come with varargs.h, I have written my own. To do this, you read the compiler specific documentation, and write some example programs and study the assembler output. If you must show an example of argument passing, you should show as wide a variety of them as possible so your students don't get stuck in the all the worlds a vax philosiphy. Bob Larson Arpa: Blarson@Ecla.Usc.Edu blarson@skat.usc.edu Uucp: {sdcrdcf,cit-vax}!oberon!skat!blarson Prime mailing list: info-prime-request%ais1@ecla.usc.edu oberon!ais1!info-prime-request