Jump to content
Sign in to follow this  
fred

'rules' To Write Efficient Code

Recommended Posts

I'm working on a 'project' which reaches the available 4K code space limits of the 16f648.

 

The problem is that it's not 100% bug free yet (is this even possible.. B) ) so sometimes I have to add\change a line of code.

This brings me immediately into new problems because the linker will fail then because of overfilling the available memory with some words..

 

It appears to be very difficult to gain some bytes by rewriting peaces of code. I already changed some algorythms and did some recoding. Sometimes I rewrite a line - having the idea it's more efficient - and instead of gaining something I'll lose 80 bytes or it doesn't make any diffrence. The behaviour looks pretty unpredictable sometimes. Probably I'm just exceeding a boundary or so somewhere which gives a big boost in gaining\loosing bytes.

 

Does somebody have some tips which can help me to find the very last bytes of code space ... :)

Share this post


Link to post
Share on other sites

Fred,

 

Does somebody have some tips which can help me to find the very last bytes of code space ... B)

I always start by using the Code window Function Info tab as it show you how much code memory is used by each function. Look for the most largest users and start work there. I then use the Code window Assembly tab and look at the code produce to see if it appears over the top.

 

Something you can generally consider; always use the smallest data type possible, eg use char instead of int for a loop counter if the value never needs to exceed 255. This will make a big different if you aren't already doing that.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Dave,

 

Unfortunately the "Code window Function" is not there when linking fails B) (Sometimes I temporarely put some code on comment to get that window generated..)

 

I only use int's when I really need them. (that's not often!)

For conversions between the two I created this construction which appears to be very efficient.

 

in combi with timer1:

typedef union {
unsigned  int   num;
struct {
	 unsigned char lo;
	 unsigned char hi;
	 } hl;
} _cnvint_ cnvint;


int value;

cnvint.hl.lo= tmr1l;
cnvint.hl.hi= tmr1h;
value= cnvint.num;

 

 

 

 

I then use the Code window Assembly tab and look at the code produce to see if it appears over the top.

this one is a bit harder for me however.. :)

 

I expect that simple loops like this do contain a relatively lot of overhead and are probably more efficient to create using inline asm (when you know how to.. B) )

unsigned char i;
volatile bit signalOn@

for( i= 100; i; i--) { 
 delay_us(10);
 if  ( signalOn ) break;
}

Share this post


Link to post
Share on other sites
I then use the Code window Assembly tab and look at the code produce to see if it appears over the top.

this one is a bit harder for me however.. B)

 

I expect that simple loops like this do contain a relatively lot of overhead and are probably more efficient to create using inline asm (when you know how to.. :) )

unsigned char i;
volatile bit signalOn@

for( i= 100; i; i--) { 
 delay_us(10);
 if  ( signalOn ) break;
}

 

Ah, the simple loop. It's better to use do-while for a loop that you know will execute at least once. The following is the most efficient form I've found with the PIC16 compiler:

 

i = 100;
do {
// Loop contents
} while (--i != 0);

 

I can save only one instruction by rewriting it in assembly. In fact, I've given up using for loops other than for the infinite loop: for ( ; ; ).

 

See my posts on bank switching. Sometimes the compiler will put multiple bank switching instructions inside a loop which 'go away' depending on where the last variable accessed before the loop is. In the above example, try to make sure the counter variable 'i' is in the same bank as anything accessed inside the loop.

 

Orin.

Share this post


Link to post
Share on other sites

Take a look at "Code size vs Data Types" chapter in BoostC help. It explains how use of different data types affects size of the generated code.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...