Shop OBEX P1 Docs P2 Docs Learn Events
GOSUB nesting limit — Parallax Forums

GOSUB nesting limit

Greg_ve9gfiGreg_ve9gfi Posts: 2
edited 2009-01-18 23:50 in BASIC Stamp
Hi Folks,

I recently wrote·a program that failed after I added just one additional GOSUB statement in a part of the program that was already in another GOSUB routine.

The program would reboot,·and I spent some time trying to debug the code before I realized that I was at the limit of the number of nested loops the BS2 can handle.

Is there any quick and dirty method to print out or otherwise determine the depth of the·nested loops as a program is running?

Thanks,

Greg

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-01-16 00:31
    Every time that you call a GOSUB, increment a variable and print out the number of spaces given by that variable, followed by | .When you return from that, decrement the variable. You'll end up with something like:

    |
      |
         |
      |
         |
           |
              |
           |
         |
       |
    
    
  • Beau SchwabeBeau Schwabe Posts: 6,563
    edited 2009-01-16 06:11
    Greg_ve9gfi,

    For the BS1 the total number of GOSUBs per program are 16.
    For the BS2 the total number of GOSUBs per program are 256.

    For both the BS1 and BS2, the total number of nested GOSUBs per program are 4.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • SRLMSRLM Posts: 5,045
    edited 2009-01-16 06:38
    I just had a thought: recursion is possible with the BS2? I know that the traditional, stack based form of recursion won't work beyond the four limit, but what about a specialized form of recursion called tail end? This is where all the work is done on the way down the recursion road, and on the way back the only thing that happens is that a variable is returned up the stack. So, could this be simulated by something like the following. Correct?

    Main:
    n = 100

    Recursion:
    n = n-1
    IF n == 0 THEN GOTO Main
    ELSE GOTO Recursion
  • Adrian SchneiderAdrian Schneider Posts: 92
    edited 2009-01-16 11:25
    SRLM: this is no recursion, just an ordinary iteration equivalent to:

    do
      n := 100
      do n := n - 1
        ; ...
      until n = 0
    while true
    
    
  • SRLMSRLM Posts: 5,045
    edited 2009-01-16 15:24
    Recursion can always be translated to iteration, and vice versa. The code is recursive because it calls it self, not because of what it does.
  • kelvin jameskelvin james Posts: 531
    edited 2009-01-16 18:07
    Using DEBUG statements within the program is a good way to watch what is happening as it is running. Debugs could be put in each subroutine to show which one is running at a specific time, eg. DEBUG " gosub1 run ", and so on. When the program is working properly to your satisfaction, the debug stuff should be removed, as it slows the execution considerably.

    **
  • Greg_ve9gfiGreg_ve9gfi Posts: 2
    edited 2009-01-18 23:50
    thanks for the feedback - much appreciated!
Sign In or Register to comment.