jumping out of subroutine with goto


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2008
    Posts
    15

    Default jumping out of subroutine with goto

    Hey @ all,
    I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?

    Example:

    Code:
    main:
       GOSUB label
    goto main
    
    label:
       GOTO somwhereelse
    RETURN
    
    somwhereelse:
       DOSOMETHING HERE
    GOTO main
    I know from other BASIC dialects that one should'nt do this, because it messes up the pointers. But what happens in PBP eg. the PIC itself?

    Is there a better solution for this problem?

    Greetings, helloo

  2. #2
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default Re: jumping out of subroutine with goto

    I recently had a project I was working on that would work fine for a while, then reboot and work fine for a while, then completely lock up. After a power cycle this would repeat. The project had buttons, and I was polling the buttons in a subroutine, if a button press was detected I jumped somewhere else using a goto. I soon realized there was a pattern and it was a certain number of button presses, I forget exactly how many. I moved my button polling routine to the main loop instead of a subroutine and it fixed the problem. I assume a stack was overflowing causing the problems because of using a goto inside of a subroutine.
    Shawn

  3. #3
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default Re: jumping out of subroutine with goto

    Quote Originally Posted by helloo View Post
    Hey @ all,
    I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?

    Example:

    Code:
    main:
       GOSUB label
    goto main
    
    label:
       GOTO somwhereelse
    RETURN
    
    somwhereelse:
       DOSOMETHING HERE
    GOTO main
    I know from other BASIC dialects that one should'nt do this, because it messes up the pointers. But what happens in PBP eg. the PIC itself?

    Is there a better solution for this problem?

    Greetings, helloo
    Try:
    Code:
    main:    
      GOSUB label
      IF bit1 = 1 then
        GOTO somwhereelse
      Endif 
      goto main  
    
    label:    
         'GOTO somwhereelse
         F x > 3 then
            bit1 = 1 'set a flag
         Endif
         RETURN
         ...  
     RETURN 
    
     somwhereelse:
         DOSOMETHING HERE 
     GOTO main

    Norm

  4. #4
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    644


    Did you find this post helpful? Yes | No

    Default Re: jumping out of subroutine with goto

    Quote Originally Posted by helloo View Post
    Hey @ all,
    I wonder what happens within the µC when I write code that jumps out of a subroutine with the goto command?

    Example:

    Code:
    main:
       GOSUB label
    goto main
    
    label:
       GOTO somwhereelse
    RETURN
    
    somwhereelse:
       DOSOMETHING HERE
    GOTO main
    I know from other BASIC dialects that one should'nt do this, because it messes up the pointers. But what happens in PBP eg. the PIC itself?

    Is there a better solution for this problem?

    Greetings, helloo
    Or, you can try this,


    Code:
    main:
       GOSUB label
    goto main
    
    label:
       GOSUB somwhereelse
    RETURN
    
    somwhereelse:
       DOSOMETHING HERE
    RETURN
    
    Use GOSUB and RETURN instead of GOTO.

    Robert
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  5. #5
    Join Date
    Feb 2008
    Posts
    15


    Did you find this post helpful? Yes | No

    Default Re: jumping out of subroutine with goto

    Thanks for all. That is a solution to the problem. Thank you.

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