View Full Version : SUBīS place in code
Fredrick
- 15th August 2009, 22:11
I have a program with lotīs of subs, and if I place all my subs near the top of the program, PBP compiles a smaller code then if I place all subs in the bottom of the program.
And I just wonder why? :)
Darrel Taylor
- 16th August 2009, 00:03
Most likely, your program is written for a 16F.
In which case there is additional "Page switching" code required.
16F's have their flash program memory divided into 2K "pages".
Whenever PBP does a GOSUB, it has to set PCLATH to the proper page before it can jump there.
For pages other than 0, it usually does it by setting 2 different bits separately, requiring 2 instructions.
But if the routine is in Page0, all it does is CLRF PCLATH (1 instruction).
So putting the subroutines first, and if they fit in Page0, it takes less code for all the GOSUB's or GOTO's.
hth,
Fredrick
- 16th August 2009, 11:27
The program is written for 18F2550.
Iīm not the only one that wonder why.
http://www.rcfaq.com/PIC/optimize.htm#CodeLocation
Darrel Taylor
- 16th August 2009, 22:22
Similar things happen with 18F's.
PBP does a pretty good job of optimizing code size.
If the subroutine or library routine is within -1024/+1023 then it will use an RCALL (relative Call) instead of CALL. RCALL uses 1 word and CALL uses 2 words.
If the subroutines do most of the work, then they probably call library routines frequently.
By placing them close to the library (top of program), a large number of CALL's will change to RCALL's, reducing the code size.
<br>
Fredrick
- 16th August 2009, 22:26
Thank you.
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.