Friday 9 December 2016

How to Reduce Code Size In Embedded Software?

Rules to Reduce Code Size in Embedded Programming

"In this Blog we will understand how to reduce code size in Embedded C programming. There are different coding rules which we can follow to reduce code size"

In microcontroller the Program memory also called as Flash Memory and RAM memory is limited. Microcontrollers have fixed amount of memories which are varies from one microcontroller to another. Sometimes problem occurs such as program Size Overflow, RAM Size over Flow.  To overcome these problems there are some rules ad methods, if keep in mind these rules and method when developing software we can easily overcome these problems up to a limit.

DATA TYPE SELECTION

DATA type selection can plays a main role in reducing code size. Select the data type of variable according to maximum value held by variable. For example if a variable value goes maximum up to 250 and you taken its data type as integer (16 bit) then there is a wastage of memory. In this case you should initialize variable data type as Char (8 bit). Try to initialize variable as unsigned.

LOOP JAMMING

Loop jamming refers to integrating the statements and operations from different loops one loop, thus reduce the number of loops in the code.We can merge two loop in one loop to reduce code size. The statements which are executed same no of times should be placed in a loop. This will require only single loop index to execute these commands. This will result in reducing program and ram memory requirement.

USE MACROS INSTEAD OF FUNCTION

The functions which are small and executed with few lines of assembly code can be replaced by defining a macros for that function. Macros execution is fast and require less memory to store.  This will result in reduce code size and increase in execution speed. This process is called function in lining. you can use function inlining or write the whole function where required.

USE OF const KEYWORD

Variable which have constant values should be initialized with const storage class. Variable with const will be saving in Program Memory.  This will result in reducing RAM memory use. 

PASS BY REFERENCE

Try to pass the parameter to a function using reference not by value. I call by reference we directly pass the address of variable. This will result increase execution speed and reduce code size.

 LOCAL VARIABLE vs GLOBAL VARIABLE

If a variable used in a function then it should be declare locally to function. Declaring variable globally require a Separate ram memory location to store the variable. If a variable declare locally in a function, it will be come in scope when function is called. This variable will be store in stack memory and flushes when function return will be executed.


No comments:

Post a Comment