PDA

View Full Version : how to shrink code



Peter1960
- 23rd January 2008, 12:37
a minor :) oversight !
The demo PIC had 8k,
final PIC ONLY 4k !
Suggestions to shrink code, please !
I have already used ASM/ENDASM where possible,
have used minimal GoSubs,
I started with 7996 and am down to 5100, only 1100 bytes to go !
any other suggestions ?

kindest regards

Jerson
- 23rd January 2008, 13:38
have used minimal GoSubs,
Try re-using code fragments by making them subroutines. Use either Gosub or call depending on whether you call from PBP or ASM section of your code

JF

skimask
- 23rd January 2008, 14:02
a minor :) oversight !
The demo PIC had 8k,
final PIC ONLY 4k !
Suggestions to shrink code, please !
I have already used ASM/ENDASM where possible,
have used minimal GoSubs,
I started with 7996 and am down to 5100, only 1100 bytes to go !
any other suggestions ?
kindest regards

Which PIC?
If it's a 16F series, you can probably mess with the variables a bit.
Figure out which variable you 'access' the most and ensure it's forced into BANK 0.

For that matter, let's see the code.
You'll probably get a 1000 ideas on how to shrink the code.

sayzer
- 23rd January 2008, 15:09
Also,
1. If you have small subroutines and/or small loops , keep them at the beginning.

Also,
2. if you are comparing a byte variable to a word variable, make sure, you compare the byte variable to the lowbyte of the word variable.

Ex:
A var byte
B var word

Instead of
IF A = B THEN Home

use,

IF A = B.LowByte THEN Home

Also,
3. If you have HIGH and LOW commands, make them direct access to PORT register.

Ex:
Instead of
HIGH PinA

Use,
PinA = 1


=================

paul borgmeier
- 23rd January 2008, 15:44
See Cliff Griffin's hints here
http://www.rcfaq.com/PIC/optimize.htm

tenaja
- 24th January 2008, 01:39
If minimal code size is the goal, then PBP is a LOT of work.

Reducing gosubs is not always the best rule, unless you are getting rid of the subs that were only added to make the code more "readable". If you can insert a few subroutines to eliminate duplicate code, they are very helpful.

One thing to do is eliminate as many built-in basic commands as possible. For instance, hserout is very inefficient. You can manually load your values into the TXreg and save a lot of space. The ADCIN command is probably even worse.

Peter1960
- 24th January 2008, 08:14
Hi and thanks everyone.

Using a combination of Lookup and assembler

the code is now 3768 !!!! beauty !

I noticed that lookup can reduce code but can also
increase code, so be carefull !

thanks

kind regards

Peter:)

sayzer
- 24th January 2008, 08:44
...

I noticed that lookup can reduce code but can also
increase code, so be carefull !..

Peter:)


Peter, it depends on the location of the LookUp statement in your program.

Just do a little experiment by changing its location in the program.

tenaja
- 24th January 2008, 18:48
Peter, it depends on the location of the LookUp statement in your program.

Just do a little experiment by changing its location in the program.
A lot also has to do with how many elements you are looking up. It does not pay off until you are looking up more than just a few... 2-3, and it is a waste; a dozen and it saves a lot.

mister_e
- 24th January 2008, 20:42
I tend to use internal EEPROM for that when at all possible.

If you want how nasty we could shrink it... challenge us a little bit and post it here ;)

Who take he challenge? Darrel? Bruce? Melanie? Alain? Skimask? Anyone?

skimask
- 24th January 2008, 20:53
I tend to use internal EEPROM for that when at all possible.
If you want how nasty we could shrink it... challenge us a little bit and post it here ;)
Who take he challenge? Darrel? Bruce? Melanie? Alain? Skimask? Anyone?

I'm up...but I'm afraid all I'll get is spanked...

sayzer
- 25th January 2008, 08:06
I tend to use internal EEPROM for that when at all possible.

If you want how nasty we could shrink it... challenge us a little bit and post it here ;)

Who take he challenge? Darrel? Bruce? Melanie? Alain? Skimask? Anyone?

Steve,

Do you mean a scan routine that will get an input variable, then scan EEPROM and provide an output variable?

________________________________

mister_e
- 25th January 2008, 08:27
depending which part of the message you refer. But yeah, when possible i replace LOOKUP by a READ ... depending what also on-board i may use an external EEPROM. I don't have any conventions. Since awhile all my projects are different. I just try to find the best compromise to reduce cost, pcb space etc etc.

Code shrinking is a kind of funny challenge... Peter says he already shrink it and maybe he's still working on, i don't know. What i suggest to him, is to post his whole code here to see how much we could reduce it.

Not a must, be as i said, that's fun. I know Darrel like that kind of thing ;)