Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!bcm!dimacs.rutgers.edu!seismo!uunet!cbmvax!cbmehq!cbmdeo!adspdk!hclausen From: hclausen@adspdk.UUCP (Henrik Clausen) Newsgroups: comp.sys.amiga.programmer Subject: Re: How are some programs SO DAMN SMALL! Message-ID: <18911727.ARN1608@adspdk.UUCP> Date: 23 Jan 91 08:23:51 GMT References: <1991Jan20.210328.18087@hoss.unl.edu> Reply-To: hclausen@adspdk.UUCP Followup-To: comp.sys.amiga.programmer Organization: Graffiti Data Lines: 98 In article <1991Jan20.210328.18087@hoss.unl.edu>, CS 231 section 2 writes: > I was looking through some programs that were written in C, and I noticed that > a few were really SMALL! Like only 2k for a full-blown clock. Hmm, I did such a beast quite recently. Used the _main() routine for entry point. > #include > main(){printf("hey man!\n");} > > the result is 4424 bytes, OPTIMIZED and all. How can a HUGE clock compile > smaller then a printf statement. printf() is a VERY general output statement that knows about floats, doubles, hex, decimal, chars and various sizes of arguments. Also using the main() entry point in itself is quite costly, doing WB stuff, argument parsing, opening stdio, malloc() stuff and more. > What are steps to compile the smallest Lattice code? > > 1) Use protos and include files whenever possible. > 2) Utilize .library calls when possible and don't forget to OPEN it so > the pragmas dont go to waste. > 3) Try not to use printf and any amiga.lib function for the matter. > 4) Compile with -Lvrt -curfist -rr -mat -O -b1 file to create registerized > code. 5) Use as little startup code as possible. > Can 'C' code get small? How about 88 bytes? Yes, C can get small indeed. Try using tinymain, or call your entrance point _main(), or even skip the startup code completely. printf() drags in most of stdio and makes for several K of code. And look at the c.o module, it's added to your program as well. The following example compiles into 88 bytes of code! No global variables, no main(), no DOSBase ready, no argument parsing, no WB awareness. Note the blink line - there is nothing added to the object file during linking! Use under 2.0 only, it will enable the '*' character for wildcard in the Shell. Thanks to Doug Walker for the neat tricks, including it's self-making ability! ; /* ;EXECUTE THIS FILE AS A SHELL SCRIPT AND IT WILL COMPILE ITSELF lc -cwus -O -v -j73 Star blink from Star.o to Star SmallData SmallCode quit */ #include #include #include #include void star(void) { struct DosLibrary *DOSBase; struct RootNode *Root; if (DOSBase = (struct DOSBase *)OpenLibrary("dos.library", 36)) { Root = DOSBase->dl_Root; Root->rn_Flags |= 1 << 24; CloseLibrary((struct Library *)DOSBase); } } And here's the executable for documentation - much shorter than the source! begin 777 Star M #\P ! - #Z0 TO#D/Z "1P)"QX 1.X KKOW82H!G$") (&D (@CH -$ZN_F(L7TYU9&]S+FQI8G)A