long@oliveb.UUCP (Dave Long) (02/23/85)
[Sorry if you're seeing this twice. There was a typo so I sent out a cancel,
but knowing netnews, the old article will arrive about 1.73 weeks before the
cancel]
The following function will return the number of arguments of the function that
called it. It works with cc(1) for 4.1bsd running on a Vax. I can't think of
any uses for it, but it certainly beats passing an argument telling how many ar-
guments the function will receive.
int
numargs()
{
asm(" clrl r1");
asm(" subb3 8(fp),$92,r1");
asm(" ashl $-2,r1,r0"); /* changed $2 to $-2 */
}
If you would like to know why this works, or if it doesn't work on your 4.1bsd
VAX system after having been compiled by cc(1), or you don't think this was an
appropriate posting, mail to me.
--
gnoL evaD
{msoft,allegra,gsgvax,fortune,hplabs,idi,ios,
nwuxd,ihnp4,tolrnt,tty3b,vlsvax1,zehntel}!oliveb!long
I'd rather be in a universe whose Creator had a bit more mechanical aptitude
and a little less imagination.
gvcormack@watdaisy.UUCP (Gordon V. Cormack) (02/24/85)
This sample program illlustrates a much simpler way to find the number of arguments to a function on the VAX, and to access an indeterminate number of arguments. #include <stdio.h> main() { pr_args(1); pr_args(1, 2, 3); pr_args(1, 2, 3, 4); } pr_args(args) int args; { int nargs, *argp, i; argp = &args; nargs = argp[-1]; printf("Called with %d args: ", nargs); for(i=0; i<nargs; i++) printf( "%d ", argp[i] ); putchar('\n'); } -- Gordon V. Cormack CS Department, University of Waterloo gvcormack@watdaisy.uucp gvcormack%watdaisy@waterloo.csnet
long@oliveb.UUCP (Dave Long) (02/25/85)
It has been pointed correctly pointed out that this will work only as long as the arguments are smaller or equal to int/long size or are pointers. How- ever, it does allow one to see how large an array of ints has to be to hold all of the arguments without losing information. It is possible to see how many btyes the arguments take up by taking out the shift by -2. Perhaps I should have named it "sizeargs". -- gnoL evaD {msoft,allegra,gsgvax,fortune,hplabs,idi,ios, nwuxd,ihnp4,tolrnt,tty3b,vlsvax1,zehntel}!oliveb!long I'd rather be in a universe whose Creator had a bit more mechanical aptitude and a little less imagination.
brooks@lll-crg.ARPA (Eugene D. Brooks III) (02/25/85)
> int > numargs() > { > asm(" clrl r1"); > asm(" subb3 8(fp),$92,r1"); > asm(" ashl $-2,r1,r0"); /* changed $2 to $-2 */ > } I can't fathom how this one works. Here is the one I use for all Vax PCC ports. As the stack frame for the Vax is hardwired by the call instruction it should work on all vax C compilers that use CALLS for their routine linkage. int getnargs() { asm(" movl 8(fp),r0"); /* Move the AP of the caller into r0. */ asm(" movzbl (r0),r0"); /* The arg count is a byte at (AP). */ #ifdef lint return(0); /* Lets keep lint useful. */ #endif } BUGS/FEATURES The value returned is the number of argument words. If there is a double in the argument list it increments the argument count by 2. The same bug/feature exists for structure arguments. For my use of the routine this was a desired feature.
long@ittvax.UUCP (H. Morrow Long [Systems Center]) (02/25/85)
> > The following function will return the number of arguments of the function that > called it. It works with cc(1) for 4.1bsd running on a Vax. I can't think of > any uses for it, but it certainly beats passing an argument telling how many ar- > guments the function will receive. > > int > numargs() > { > asm(" clrl r1"); > asm(" subb3 8(fp),$92,r1"); > asm(" ashl $-2,r1,r0"); /* changed $2 to $-2 */ > } You've just set portability back 1,000 years. -- H. Morrow Long ITT-ATC Systems Center, 1 Research Drive Shelton, CT 06484 Phone #: (203)-929-7341 x. 634 path = {allegra bunker ctcgrafx dcdvaxb dcdwest ucbvax!decvax duke eosp1 ittral lbl-csam milford mit-eddie psuvax1 purdue qubix qumix research sii supai tmmnet twg uf-cgrl wxlvax yale}!ittvax!long