This is the second in a series of postings on how to minimize the memory consumption of an embedded system.
As the title suggests, you’ll often get a nice reduction in code size if you are completely consistent in your HLL coding style. To show how this works, its necessary to take a trip into assembly language.
When you write in assembly language you soon find that you perform the same series of instructions over and over again. For example, to add two numbers together, you might have pseudo assembly language code that looks something like this:
LD X, operand1 ; X points to operand 1 LD Y, operand2 ; Y points to operand 2 LD R0,X ; Get operand 1 LD R1,Y ; Get operand 2 ADD ; ST R0 ; Store the result in R0
After you have done this a few times, it becomes clear that the only thing that changes from use to use is the address of the operands. As a result, assembly language programmers would typically define a macro. The exact syntax varies from assembler to assembler, but it might look something like this:
MACRO ADD_BYTES(P1, P2) LD X, P1 ; X points to parameter 1 LD Y, P2 ; Y points to parameter 2 LD R0,X ; Get operand 1 LD R1,Y ; Get operand 2 ADD ; ST R0 ; Store the result in R0 ENDM
Thereafter, whenever it is necessary to add two bytes together, one would simply enter the macro together with the name of the operands of interest. However, after you have invoked the macro a few dozen times, it probably dawns on you that you are chewing up memory un-necessarily and that you can save a lot by changing the macro to this:
MACRO ADD_BYTES(P1, P2) LD X, P1 ; X points to parameter 1 LD Y, P2 ; Y points to parameter 2 CALL LDR0R1XY ENDM
It is of course necessary to now define a subroutine ‘LDR0R1XY’ that looks like this:
LDR0R1XY: LD R0,X ; Get operand 1 LD R1,Y ; Get operand 2 ADD ; ST R0 ; Store the result in R0 RET
Clearly this approach starts to save a few bytes per invocation, such that once one has used ADD_BYTES several times one achieves a net saving in memory usage. If one uses ADD_BYTES dozens of times then the savings can be substantial.
So how does this help if you are programming in a HLL? Well, decent compilers will do exactly the same optimization when told to perform full size optimization. However, in this case, the optimizer looks at all the code sequences generated by the compiler and identifies those code sequences that can be placed in a subroutine. A really good compiler will do this recursively in the sense that it will replace a code sequence with a subroutine call, and that subroutine call will in turn call another subroutine and so on. The results can be a dramatic reduction in code size – albeit at a potentially big increase in demand on the call stack.
Now clearly in order to take maximal advantage of this compiler optimization, it’s essential that the compiler see the same code sequences over and over again. You can maximize the likelihood of this occurring by being completely consistent in your coding style. Some examples:
- When making function calls, keep the parameter orders consistent. For example if you call a lot of functions with two parameters such as a uint8_t and a uint16_t, then ensure that all your functions declare the parameters in the same order.
- If most of your variables are 16 bit, with just a handful being 8 bit, then you may find you get a code size reduction if you convert all your variables to 16 bits.
- Don’t flip randomly between case statements and if-else-if chains.
Note that notwithstanding the fact that being completely consistent can save you a lot of code space, I also think that code that is extremely consistent in its style has other merits as well, not the least of which is readability.As a final note, does anyone know the formal name for this type of optimization?