As well as the usual things a good programmer will do, there are some issues specific to mspgcc which you should be aware of.
- If you are sure your main routine will never exit, you can use the "-mendup-at=main" flag when compiling. This will save 6 bytes of ROM.Avoid passing long argument lists to functions.
- Avoid returning long values from functions. The most efficient function types to use are void, int, or pointer.
- Avoid initializing global variables within a small function. Instead, assign a value during variable definition.
- Use int instead of char or unsigned char if you want a small integer within a function. The code produced will be more efficient, and in most cases storage isn't actually wasted.
- Inspect assembler code (-S compiler flag). The compiler cannot eliminate dead code in some cases. Do not write dead code :)
- Do not declare your own SFRs. They are all declared in include files in the right way to achieve maximum code performance.
- Try to minimise the use of addition and subtraction with floating point numbers. These are slow operations.
- Use shift instead of multiplication by constants which are 2^N (actually, the compiler may do this for you when optimization is switched on).
- Use unsigned int for indices - the compiler will snip _lots_ of code.
- Use 'switch/case' constructs rather than a chain of 'if/else' constructs.
- When defining bit fields, try to use signed integers. This produces more compact code that bit fields of unsigned integers.
- Use 'alloca' instead of 'malloc' for locals. In embedded applications trying to avoid any dynamic memory allocation is usually even better ;).
source: http://mspgcc.sourceforge.net
0 Comments