PDA

View Full Version : Subroutine placement - must they come first?



BrianT
- 13th July 2011, 09:37
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

Bruce
- 13th July 2011, 12:59
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.


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.

mister_e
- 13th July 2011, 18:09
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


MyWhateverString Macro Str
local STRStart, STREnd
GOTO STREnd
STRStart
DT Str
STREnd
MOVE?CW STRStart, _WhateverAddrVar
L?CALL _MainSub
ENDM
To


MyWhateverString Macro Str
local STRStart, STREnd
L?GOTO STREnd
STRStart
DT Str
STREnd
MOVE?CW STRStart, _WhateverAddrVar
L?CALL _MainSub
ENDM
problem solved

Archangel
- 14th July 2011, 04:45
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 . . .

mister_e
- 14th July 2011, 05:03
A snip of...few hundred lines....

BrianT
- 28th August 2012, 11:45
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

Charlie
- 28th August 2012, 12:10
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)