SUBīS place in code


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187

    Default SUBīS place in code

    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?

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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,
    DT

  3. #3
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Default

    The program is written for 18F2550.

    Iīm not the only one that wonder why.
    http://www.rcfaq.com/PIC/optimize.htm#CodeLocation

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

  5. #5
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Thumbs up

    Thank you.

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts