Xref: utzoo comp.lang.c:8785 comp.sys.ibm.pc:13978 Path: utzoo!utgpu!water!watmath!clyde!att-cb!att-ih!pacbell!ames!ncar!boulder!sunybcs!bingvaxu!leah!itsgw!steinmetz!uunet!vsi!friedl From: friedl@vsi.UUCP (Stephen J. Friedl) Newsgroups: comp.lang.c,comp.sys.ibm.pc Subject: Re: cdecl keyword in MSC Summary: arg-passing direction is not defined by the language Keywords: cdecl MicroSoft C keywords Message-ID: <476@vsi.UUCP> Date: 2 Apr 88 23:29:39 GMT References: <1238@wjvax.UUCP> <297@ho7cad.ATT.COM> <1242@wjvax.UUCP> <185@premise.ZONE1.COM> Organization: V-Systems, Inc. -- Santa Ana, CA Lines: 41 In article <185@premise.ZONE1.COM>, chang@premise.ZONE1.COM writes: > > [ comments about "useless" cdecl and function calling conventions in MSC ] > > The calling convention we're talking about here isn't call-by-name or > call-by-value. It's simply the order in which parameters are passed > on the stack. And in C, that order *must* be right to left in order > to support variable length functions like printf. If it were left to > right, then the printf function wouldn't know where to find its format > string. Note that while right-to-left arg passing may be used in the PC, this is defined *by* the PC and/or MSC and not by the C language (it is specifically "Unspecified Behavior"). There are indeed machines such as the WE32100 (AT&T 3B2) and I believe the PDP-11 that are happy to pass args in the other direction, and *my* 3B2 printf() still says "hello, world\n" :-) My limited cause-and-effect detectors indicate that when the stack grows up (i.e., "push" increments the stack pointer) then args go left->right, and if the stack grows down then args go right->left. Upward-growing stacks seem to be much less common; anybody know of others? Which direction do their args pass? Also, I imagine that a clever compiler writer could manage to define a calling convention that would work properly in either direction, albeit less efficiently in the "wrong" direction. Try this on your favorite compiler to see what you get. main() { static char *dirwords[] = { "left", "right" }; char **dirptr = dirwords; printf("Direction is %s to %s\n", *dirptr++, *dirptr++); } -- Steve Friedl V-Systems, Inc. "Yes, I'm jeff@unh's brother" friedl@vsi.com {uunet,ihnp4}!vsi.com!friedl attmail!vsi!friedl