Subroutine placement - must they come first?


Closed Thread
Results 1 to 7 of 7
  1. #1

    Default Subroutine placement - must they come first?

    In any new program I first have any DATA statements, then I declare the hardware assignment then the software variables and follow that with subroutines. I jump over the subroutines to get to the Initialise block and then get to MAIN where the program code loops as it runs.

    Must it be this way? Can I place all the subroutines at the very end of the program? This would make the code easier to read in some cases.


    Cheers
    BrianT

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: Subroutine placement - must they come first?

    If you're using a 16F part, your code is relatively small, and all fits into code page 0 with PBP library code, it doesn't matter where you place your subroutines.

    As your code grows beyond code page 0 it might matter where you place them. All PBP library code has to fit into code page 0 on 16F type parts. If your program extends into page 1, and your subroutines are on page 1, then it takes more code to switch back & forth between code pages.

    If your subroutines all fit into page 0, and your main routine ends up in page 1, it will save code space because your subroutine calls to PBP library code are on the same code page 0.

    I normally put a large number of subroutines in an include file so I don't have to scroll through them all to work on code in my main loop.
    Code:
     
    GOTO Main
     
    INCLUDE "MySubs.INC"
     
    Main:
    Call subs from here.
    GOTO Main
    If all my subroutines won't fit into code page 0.. I'll try to get the ones used most often - or the ones with the most library calls squeezed in.

    You can experiment with shifting code around to see the difference.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Subroutine placement - must they come first?

    One good thing to remember about asm DATA, DA, DT...

    Recently I've messed a lot with the Nokia GLCD thing and while I added Strings, some could not be properly displayed, code went to lalaLand. The reason is like Bruce said... page boundary. In the end my macro just needed a simple modification... from something like
    Code:
    MyWhateverString Macro Str
               local STRStart, STREnd
               GOTO STREnd
    STRStart
               DT Str
    STREnd
               MOVE?CW STRStart, _WhateverAddrVar
               L?CALL _MainSub
               ENDM
    To
    Code:
    MyWhateverString Macro Str
               local STRStart, STREnd
               L?GOTO STREnd
    STRStart
               DT Str
    STREnd
               MOVE?CW STRStart, _WhateverAddrVar
               L?CALL _MainSub
          ENDM
    problem solved
    Last edited by mister_e; - 13th July 2011 at 18:11.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Subroutine placement - must they come first?

    Quote Originally Posted by mister_e View Post
    One good thing to remember about asm DATA, DA, DT...

    Recently I've messed a lot with the Nokia GLCD thing and while I added Strings, some could not be properly displayed, code went to lalaLand.
    Oh Steve, that was Your code I saw . . .
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Subroutine placement - must they come first?

    A snip of...few hundred lines....
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Subroutine placement - must they come first?

    My ignorance is on clear display here so please be gentle.
    I have a system of two PIC16F88 and three 18F4620 which all chatter amongst themselves over a multiwire parallel bus. Some of the code takes up over 63000 bytes on a PIC 18F4620.
    Currently all the subroutines are placed after the hardware and variable declarations. I would like to have the subroutines at the bottom of the code if possible for simpler reading.

    I recall some earlier (PBP2.xx?) comments that subroutines should be at the top of the program but is there any more explicit advise for the 16F88 or the 18F4620. Bruce's comments pass over my head.

    Cheers
    BrianT

  7. #7
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Subroutine placement - must they come first?

    You can put subroutines anywhere you like and PBP worries about page swaps for you. If, however, you have real time or compiled code size issues, putting them at the beginning can help optimize. There are also special issues when crossing page boundaries that PBP *usually" takes care of - a notable exception is mentioned above.

    I always put my subroutines at the end for readability. in 99% of cases this is fine.
    These days I like to use 18F parts which tend to have a better sense of humour about these things, as well as usually being faster and having more memory. (at slightly highter cost)

Members who have read this thread : 1

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

Tags for this Thread

Posting Permissions

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