How to Reduce the ROM Code Size.

Some general tips for reducing the Code size for firmware.

1) Use the Map file.
Configure the tool to generate a MAP file. Most linker/locators will generate a fairly detailed map file, which you can use to hunt down the biggest targets for code reduction.


2) Use compiler optimization.
The Gnu tool chain lets you optimize for size or speed. Choosing to optimize for size may get you a few percent, or it may do no good at all depending on how well written your compiler back end is.  


3) Optimize on Floating point arithmetic.
If your processor can't handle float-ops natively then the compiler will add in code to simulate it and that can take up a good bit of space. If you look at the calculation then, with a bit of thought, you can often keep the numbers as integers if you scale them up for the calulation and then scale them down again afterwards. This will be faster as well as smaller.


4)See if you have similar functions in different places that can be combined, possibly with an if-else to do the variant part.  Sometimes good structured programming not only makes the code better, but makes the footprint smaller.


5) Check to see if you have a little bit of functionality that takes up a huge amount of space.  If you have one printf call in your whole application, you can save a bunch of space by replacing it with simpler calls.


6) Check any algo you are using which can  replaced by a simpler algorithm that takes longer.

7)  Check to see if there's any old code that's not doing anything any more. Are there obsolete features that you can safely remove?