Making Watcom C(++) Programs Tiny

Hamburg (Germany), the 27th May 1997. Written by Nils Pipenbrinck aka Submissive/Cubic & $eeN

Introduction

As a demo-coder Pascal and I where faced with some big problems. If you ever tried to write an Intro using Watcoms fine compilers you might already noticed, that lots of space will be wasted because the linker always included the standard libraries. Even if you don't need the libraries the initialization code will be called and thus there are still lots of routines in your final code which will of no use.
This tutorial will show you how you can save some KB. I haven't ever done a comparison, but you might be able to get more than 20k of space.. It just depends how many libraries you can kick

Replace the Math Library

The math library is the first part where you should take a look at. If you use floating point math (I guess you will if you code for the Pentium) you might already noticed, that you actually don't call too much floating point routines. Mainly most of the stuff you need is SIN, COS and SQRT. Interesting is, that you can already use the floating point type float witout calling the library at any time. Additions and Multiplications are inlined into your code.
Pascal write a nice headerfile with some enhanced math functions. There are all classics like trigonometric functions and some extra goodies. All these functions have been written as inline pragmas. The code is available at the bottom of this document.
Some weeks ago a guy from Italy calling himself ArtNouveaU gave me a enhanced version of the math headerfile (with some new functions...) you can get it here.

Replace the I/O Library

Damn! If you write an Intro you shouldn't ever use file I/O!. You should note, that the File I/O Library takes use of the heap and the math-library. Therefore it's a good idea not to use any I/O at all. However, if you can't keep your fingers from it you should use the stuff I wrote.I wrote a couple of inline functions which allows you to do the following things:

There is absolutely no support for errorhandling. If you create a file which exists or do other things that would normally be handled strange things will happen. Even if you need File I/O it's still a good idea to use this code. If you want to display a string on the screen you can use the file-write function and pass the HSTDOUT handle to it.. This way you can communicate with the console and save a lot of space. To completely eliminate the need of standard libraries you even have no other chance.

Replace the Heap

A lot of memory can be saved if you replace the standard heap. The standard heap works great, but it's has never been designed to be a part of programs where space is an issue. I written a nice replacement for the heap. It won't work with new and delete, but you can use it instead of malloc and free. My heap doesn't do any checks. If you corrupt it your program WILL crash.
My heap is also a little bit different. Its faster than the standard heap (Yes!), but it needs some initialisation. Before you use it you have to pass a memory-block and it's size. The example code privided with the source will show you how to do it.

Forcing Watcom C++ to link without libraries

Now we're going into the guts of Watcom C++. Basically removing the default libraries is pretty simple. Just add this two lines to your linker-file:

OPTION NODEFAULTliBS
FILE   TINYSTRT.OBJ

TinyStrt is a really interesting ASM file. It initializes your stack and defines a couple of functions which are essencial for Watcom C++. Pascal wrote the first version for our Intro Lasse Rein Boeng and I added some additional functions. After you linked the file the libraries won't be initialized anymore. Therefore some library functions will fail or even crash if you use them.
The NODEFAULTliBS option will not link the standard libraries anymore. If you however use still library functions the linker will give you a lot of errormessages. Eleminate them all! There might be still a call to ABS and other functions in your code. You can be sure not to call any standard library function if you remove all includes of STDliB, STDIO, MATH, STRING and so on. After you've done this the program will link and be a lot smaller.

Download this stuff

This would be a bad tutorial without code... I promised that you can download the source. Maybe I'll also add an example program. (plan for the future). Click here to download all the stuff.

Final Words

Most of the stuff you read is based on the work of Pascal. I only added this and that function to make coding witout defaultlibs as comforable as possible. I should also note, that the heap replacement is a rewrite of a 16-bit code I found some years ago.. It's pure assembler. (Hint to all the assembler coders, if you want to experience the joys of a heap you might check out the code. It works well under ASM.)
Pascals mxmplay player fits perfectly into this stuff. It's a really tiny XM player for the GUS(only).

If you have any hints, wrote other replacement functions or have problems using the stuff I presented you can of cause contact me.


© by submissive Valid XHTML 1.1!