Tips and trick-tutorial: 12 steps for efficient programming

As well as the usual things a good programmer will do, there are some issues specific to mspgcc which you should be aware of.

  1. 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.
  2. Avoid returning long values from functions. The most efficient function types to use are void, int, or pointer.
  3. Avoid initializing global variables within a small function. Instead, assign a value during variable definition.
  4. 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.
  5. Inspect assembler code (-S compiler flag). The compiler cannot eliminate dead code in some cases. Do not write dead code :)
  6. Do not declare your own SFRs. They are all declared in include files in the right way to achieve maximum code performance.
  7. Try to minimise the use of addition and subtraction with floating point numbers. These are slow operations.
  8. Use shift instead of multiplication by constants which are 2^N (actually, the compiler may do this for you when optimization is switched on).
  9. Use unsigned int for indices - the compiler will snip _lots_ of code.
  10. Use 'switch/case' constructs rather than a chain of 'if/else' constructs.
  11. When defining bit fields, try to use signed integers. This produces more compact code that bit fields of unsigned integers.
  12. 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

Post a Comment

0 Comments