Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!uunet!cbmvax!bj From: bj@cbmvax.commodore.com (Brian Jackson) Newsgroups: comp.sys.amiga.programmer Subject: Re: How are some programs SO DAMN SMALL! Message-ID: <17828@cbmvax.commodore.com> Date: 21 Jan 91 16:22:31 GMT References: <1991Jan20.210328.18087@hoss.unl.edu> <702@caslon.cs.arizona.edu> <188e595a.ARN262e@starsoft> Reply-To: bj@cbmvax.commodore.com (Brian Jackson) Organization: Commodore, West Chester, PA Lines: 58 In article <188e595a.ARN262e@starsoft> david@starsoft writes: >In article <702@caslon.cs.arizona.edu>, Dave P. Schaumann writes: > >> In article <1991Jan20.210328.18087@hoss.unl.edu> 231b3678@fergvax.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. How does the >> >program get so small! It seems that if I compile a program like >> > >> >#include >> >main(){printf("hey man!\n");} [...] >> >Can 'C' code get small? I have a LIST program in the works. It's 14000 >> >bytes compared to c:list's 2500. Assembly must be the only decent way to >> >go. >> >> As you guessed, if you want *really* small code, you have to use assembler. >> Then you only have the code you need to run your program, and no extra hidden >> stuff. >> >True, but there IS a way to get smaller C code. > >You have to write your own startup code. Most compilers supply the code >for their startup routine (SAS calls theirs c.c). You can copy it and >modify it to take out the extra stuff. The "Hey Man" code can be made real small in C. (I used Manx 5.0 for this but I assure you all that this is not a "slam" on SAS :) ) : extern struct DOSBase *DOSBase ; _main() { if(DOSBase = (struct DOSBase *)OpenLibrary("dos.library", 0L )) { Write(Output(), "hey man!\n", 9L ) ; CloseLibrary( DOSBase ) ; } } This compiles and links to just 392 bytes. So you can get C code pretty small. Most C compilers asume a "worst case scenario" and include a lot of extraneous stuff. Each compiler differs in the way it does this so the ways needed to circumvent the problem differ as well. The above code totally blows off the startup and exit code and goes directly to AmigaDOS (Hence the need to open DOS Library myself.) This is about as close to doing it in assembler as C gets. bj ----------------------------------------------------------------------- | Brian Jackson Software Engineer, Commodore-Amiga Inc. GEnie: B.J. | | bj@cbmvax.cbm.commodore.com or ...{uunet|rutgers}!cbmvax!bj | | "Homer, I couldn't help overhear you warping Bart's mind." | ----------------------------------------------------------------------- > Dave Lowrey | david@starsoft or {uhnix1,lobster}!starsoft!david