This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Source code optimization MSP430

Other Parts Discussed in Thread: MSP430G2332

Dear Sirs, i'm writing a source code for a msp430g2332 and i'm quickly reaching the maximum size code (now 1980bytes on 2k). My IDE is CCS 4.2.3.00004. Is there any way to optimize source code's size? And which are the drawbacks in doing this ? Thank you in advance, best regards.

B.M

  • burningmosfet said:
    Is there any way to optimize source code's size

    If not already on, you can enable code optimization in your compiler settings.
    The other way to reduce code size is to use explicitely uninitialized variables if you already initialize them in the code. Same for arrays, don't initialize them in the declaration if you are already filling/overwriting them later before reading them. Variable initialization uses flash space.

    YOu can tell the linker to emit a map file. It tells you what part of your code uses how much space.

    Another thing is to stay away from things that require runtime library code. Such as float/double variables, printf and such things.

    The equation is simple:
    Code needs space. Less code needs less space. If there is not enough space, put less code in your project - explicitely or implicitely. If that's not possible, buy an MSP with more space.
    Even if this sounds trivial, it is a truth you cannot deny, even if you would like to. And it's the reason why MSPs with more space exist.

  • Not sure you are talking about the size of your source code or the size of the object code CCS generates. There is no limit for the size of source code.

  • Maybe I expressed myself in the wrong way. I mean, if there is a way to optimize size of HEX (not source), which is the way to do it with CCS ? Is there any guide?

    If i go to project -> proprieties -> MSP430 compiler -> Basic options and i select

    Optimization level (-opt_level, -O) = 4

    Control Speed vs size trade-offs (0=size, 5=speed) (-opt_for_speed, -mf) = 0

     

    Could, this configuration cause any drawback?

  • optimizing for size means taking loops when optimizing for speed would unroll loops into repeated instructions. e.g. x>>7 can be done as a loop of 7 interations over one shift operation or 7 sequential shift operations. The sequential operations are much faster but take more space. Especially if x is a 32bit variable and you have to shift two registers with carry.

    If you're running short on space, optimizing for size is the only option, even if it slows the code down here and there.

    The compiler does a good job optimizing your C code by size and speed. However, its possibilities are limited - the code must still do what it is intended to, so there's a limit to optimization. The compiler only knows what you tell him to compile, not what you want to achieve with this code.
    So often much more optimization is possible by rewriting the source code. If you do somehting the hard way, the compiler cannot know that there could be a much smaller way to get to the same result, by using a different algorithm, or by doing things in a different order.

    IAR is usually a bit better with code optimization. For some more bucks.

  • thank you sir for your exhaustive and complete answer

**Attention** This is a public forum