Next: 5.1 Notes on MCS51
 Up: SDCC Compiler User Guide
 Previous: 4.7 Cyclomatic Complexity
     Contents 
     Index 
Here are a few guidelines that will help the compiler generate more
efficient code, some of the tips are specific to this compiler others
are generally good programming practice.
- Use the smallest data type to represent your data-value. If it is
known in advance that the value is going to be less than 256 then
use a 'char' instead of a 'short' or 'int'.
 
- Use unsigned when it is known in advance that the value is not going
to be negative. This helps especially if you are doing division or
multiplication.
 
- NEVER jump into a LOOP.
 
- Declare the variables to be local whenever possible, especially loop
control variables (induction).
 
- Since the compiler does not do implicit integral promotion, the programmer
should do an explicit cast when integral promotion is required.
 
- Reducing the size of division, multiplication & modulus operations
can reduce code size substantially. Take the following code for example. 
 
foobar(unsigned int p1, unsigned char ch) 
{ 
    unsigned char ch1 = p1 % ch ; 
    ....     
} 
For the modulus operation the variable ch will be promoted to unsigned
int first then the modulus operation will be performed (this will
lead to a call to support routine _muduint()), and the result will
be casted to an int. If the code is changed to 
 
foobar(unsigned int p1, unsigned char ch) 
{ 
    unsigned char ch1 = (unsigned char)p1 % ch ; 
    ....     
} 
It would substantially reduce the code generated (future versions
of the compiler will be smart enough to detect such optimization oppurtunities). 
Subsections
 
 
 
 
 
 Next: 5.1 Notes on MCS51
 Up: SDCC Compiler User Guide
 Previous: 4.7 Cyclomatic Complexity
     Contents 
     Index 
Johan Knol
2001-07-13