msgbartop
Adam Palmer MBCS CITP, Linux, PHP Programmer, MySQL Developer, Embedded Hardware, Security Consultant
Did my blog help you? Please link to me!
  dns test
 
RSS Feed
msgbarbottom

15 Jan 10 Embedded Linux Programmer

As an embedded linux programmer, I’ve had the opportunity to work on a number of different platforms, MIPS being one of my favorites.

There are a few general limitations that you’ll find. You have limited CPU power available, you have very little RAM available, and for more advanced operations and optimizations, your CPU will generally have a limited function set.

The usual good programming practices apply, but are of much greater importance. Specifically, don’t allocate memory that you don’t need, and dont put the CPU under undue stress with unnecessary or badly optimized loops. Taking C syntax and some pseudo code;

char *myvar = malloc(10240);
strcpy(myvar, somevar);

Where somevar has previously been limited to say 16 bytes of data, why just allocate an arbitrary large number (10KB) to myval?

Here’s another:

int ctr = 0;
int maxlen = 0;
for (ctr = 0; ctr < get_count(data); ctr++) { … }

Although this looks nice and tidy, why are we calling get_count to get the number of records each time the loop runs when it will never change?

int data_count = get_count(data);
for (ctr = 0; ctr < data_count; ctr++) { … }

makes much more sense.

Years ago, there was also a lot of manual assembler optimization that you could do before compiling. A popular one was replacing “movl %eaX, 0″ with “xorl %eax %eax” as the exclusive or result of two of the same value will be 0 and requires less processor time than the movl operation. Now, not only can most of this be done for you by passing the -OX flag to gcc, but a loop of 1 million movl’s took no more time than 1 million xorl’s on my processor, and I’m guessing the processor deals with this for you now anyway. Today, if you’re on a nonstandard platform and you’ve got the information that certain instructions are favored over others on your target, you’d be able to better optimize the assembler code before compile time.

The main thing to remember, is that sloppy programming that would have gone unnoticed on your super powerful CPU will bring your embedded application to a halt.

Tags: , , , , ,



Leave a Comment