
Decreasing Code Size 143
Decreasing Code Size
As I said earlier, when it comes to reducing code size your best bet is to let the
compiler do the work for you. However, if the resulting program is still too large
for your available ROM, there are several programming techniques you can use to
further reduce the size of your program. In this section we’ll discuss both auto-
matic and manual code size optimizations.
Of course, Murphy’s Law dictates that the first time you enable the compiler’s opti-
mization feature your previously working program will suddenly fail. Perhaps the
most notorious of the automatic optimizations is “dead code elimination.” This
optimization eliminates code that the compiler believes to be either redundant or
irrelevant. For example, adding zero to a variable requires no runtime calculation
whatsoever. But you might still want the compiler to generate those “irrelevant”
instructions if they perform some function that the compiler doesn’t know about.
For example, given the following block of code, most optimizing compilers would
remove the first statement because the value of *pControl is not used before it is
overwritten (on the third line):
*pControl = DISABLE;
*pData = 'a';
*pControl = ENABLE;
But what if pControl and pData are actually pointers to memory-mapped device
registers? In that case, the peripheral device would not receive the DISABLE com-
mand before the byte ...