PDA

View Full Version : "Words Used" changes from PIC to PIC



sayzer
- 20th March 2006, 12:44
Compiling a code for a PIC chip uses less or more words then compiling the same code for another PIC.

For example, in terms of the words used, compiling a code for F877 is approx. 25% more then compiling the same code for F628A. I had 2500 words for F877 but 2043 words for F628A.

May I have some information why this happens?

Thanks

skimask
- 20th March 2006, 23:51
When a PIC has to change the bank select register to access another section of code memory, ram, or a register, that takes another instruction to do so, depending on where in the memory map this particular register or instruction is located.
So, if you've got code all spread out thru the memory map, or ram defined in a bunch of different locations, or are using registers in different banks, etc.etc. or doing all of the above a lot, you use an extra instruction almost each time.
I found this out awhile back when I was using the '877 on a large program and I ran out of room.

Try this little experiment:

Take a look at your program, specifically the variables in your program. Count the number of times each variable is used, take the highest used variable and assign it to BANK 0. Keep doing that, going down thru the list from the most used to the least used and keep trying to assign them to BANK 0 until the compiler complains that you are out of memory space.

Example:
Use 'TEMP1 var byte BANK 0'
instead of 'TEMP1 var byte'

You should find that your total code space usage will go down as you assign the most used variables to BANK 0. The reason variables assigned to BANK 0 used less memory is because it only takes one instruction to address those variables, whereas variables in a different bank (usually 2 or 4) take a second instruction to set up the pointer to those variables.
I did this with my mp3 player firmware a long time ago and found a savings of roughly 20% in code space.

Hope that somewhat answers your question. If not, look in the 'memory organization' section of your particular PIC's datasheet.

JDG

Archilochus
- 21st March 2006, 00:13
It's kind of an interesting subject that I'd like to see more info on.
I've written some code for 16F84, 16F628, etc and the reported code size is apparently correct- but when I compile code for the 16F877 or 877A (using PM), I get all sorts of odd results - latest program reports 13,433 words used! (program compiles with no errors and runs fine).

Arch

sayzer
- 21st March 2006, 07:10
Hi JDG,

Thanks for the information.

I tried your little experiment using the most frequently used variables but it did not change anything for me (both for F877 and F628A, there is no change).

skimask
- 23rd March 2006, 01:51
Well, I suppose it might also have to do with how many variables you are using in the first place. If you use more than what will fit in BANK 0, then you'll never see a difference in code size.
If your program is really short, try forcing all the variables to BANK 0, then try forcing them all to BANK 3 (or another BANK). You should see a difference.
Again, if you read the memory organization section of the datasheet and compare that to how different areas of ram and code space are accessed, you'll see what I mean.
By the way, none of this code space stuff works with the 18F series of PICs. Practically everything can be addressed with one instruction on those.
JDG