Shop OBEX P1 Docs P2 Docs Learn Events
Why does it matter WHERE you declare SUB? — Parallax Forums

Why does it matter WHERE you declare SUB?

basicstampedebasicstampede Posts: 214
edited 2008-08-31 15:44 in General Discussion
I noticed that it DOES matter where in the program you declare SUB.

I found out that declaring SUB before Start: is the only reliable way to get program to work.
Declaring it after Start: or after Main: will not work.

Why is that?· I would have thought that as long as you declare it to the compiler before the compiler sees first subroutine should suffice.

Thanks.


' =========================================================================
· PROGRAM Start
' =========================================================================
'Pgm_ID:
· 'DATA· "SX/B Template", 0

'
' Subroutines / Jump Table
'
GET_LOC SUB 0················· 'works if declared here···
GET_LED_VALUE SUB 0
GRAPH_IT SUB 0

'
' Program Code
'
Start:
· ' initialization code here
'GET_LOC SUB 0··················· 'sometimes does NOT work
'GET_LED_VALUE SUB 0
'GRAPH_IT SUB 0·

····
Main:
· ' main code here
'GET_LOC SUB 0···················· 'sometimes does NOT work
'GET_LED_VALUE SUB 0
'GRAPH_IT SUB 0
DO
·
·· GET_LOC
·· GET_LED_VALUE
·· GRAPH_IT

LOOP

Comments

  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-08-31 14:56
    It matters because SX/B uses your definitions to create a jump table and due to the SX architecture the jump table needs to be located in the first half of a code page. If you'll simply use the template that is in the help file you will rarely have a problem (I've attached my present version; download and install it in your SX Templates folder). The SX and SX/B have rules, follow them and your SX/B life will be easy. In case you're not sure where the Templates folder is, the location on my system is as follows (yours should be similar):

    C:\Program Files\Parallax Inc\SX-Key v3.2.3\Templates

    Now, if you have a very large interrupt -- which must come ahead of the of the subroutine and function declarations -- you may end up having the interrupt jump to another section of of the listing so that the jump table is properly located. In my experience (and I write a LOT of SX/B programs) this doesn't happen very often so don't be concerned about it now.

    You seem frustrated with SX/B and that there's not as much information for it as there is for the BS2. To be candid, SX/B is not as easy as PBASIC and never will be, and the SX is not as big of seller as the BS2 so Parallax can only devote so many resources to it. The benefit to sticking it out, though, is that you will gain tremendous horsepower using the SX and will be able to do things that you never could with a BS2 module. Stick with it, it's worth the effort. Just Friday I got a call form a big amusement park in my area (the one run by a rodent) that loves the SX and they asked me to write them another SX/B program for a show. In addition to using the EFX-TEK Prop-SX in several areas of the park I have designed custom devices for them; all are SX-based and programmed in SX/B (with a bit of Assembly mixed in, but this is one of the strengths of SX/B).

    A note about my template: I prefer to declare IO pins before constants; some programmers go the other way so that they can use constants in their IO pin definitions. If you prefer the latter, simply move the section headers. I created this template to keep my thoughts organized so that I always put the right code in the right place -- this lets me focus on the solution to the problem. I think it will help you, too. Good luck and have fun with your SX/B projects.

    Post Edited (JonnyMac) : 8/31/2008 3:39:42 PM GMT
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-08-31 15:44
    I found out about this "the hard way", too.

    Sparks-R-Fun made the following observation:

    "The label after the PROGRAM directive designates the location where program execution should begin. In your case you have called this label starter: which is fine. However, I suspect the location of starter: is not fine since you are trying to declare subroutines after the start of your program. I suspect that this causes the SX to actually attempt to run those subroutines in the order listed!"
Sign In or Register to comment.