GOTO vs RETURN after a GOSUB


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637

    Default GOTO vs RETURN after a GOSUB

    Hi, I have the following code,

    Code:
    MainLoop:
    
    'DO SOME STUFF HERE.....
    
        FOR J = 0 TO 7
            GOSUB SearchForClockSignal
        NEXT J
    
    goto MainLoop
    
    SearchForClockSignal:
    
        USBSERVICE
        SERIN2 PORTE.3, 32, 3, CLOCKSEARCHFAILED, [wait(252), STR RFID_IN\6]
        USBSERVICE 
    
        IF RFID_IN[0] = 252 THEN GOTO MAINLOOP  
    
        SquaresArray[0] = 255
        SquaresArray[1] = 202                       
        SquaresArray[2] = RFID_IN[0]                        
        SquaresArray[3] = RFID_IN[1]  
        SquaresArray[4] = RFID_IN[2]                        
        SquaresArray[5] = RFID_IN[3]
        SquaresArray[6] = RFID_IN[4]                        
        SquaresArray[7] = RFID_IN[5] 
        
        if PPB_DeviceState = 32 then   'ONLY WHEN THERE IS A USB CONNECTION PRESENT     
            GOSUB SendUSBData
        endif
        
        GOTO MAINLOOP   'CLOCK SERIAL COMMUNICATION WAS RECEIVED
        
    CLOCKSEARCHFAILED:
    
    RETURN
    The program above will run for about 20 seconds and then it will stop working. Now, if I replaced the code in red "GOTO MAINLOOP" above by RETURN, then the code works just fine. Now, I understand that every GOSUB waits for a RETURN, but since I had the option "STACK OVERFLAW" disabled I thought that I could get away with it. Can somebody explains to me what is going on with this issue?

    Thanks,

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

    Anonymous

  2. #2
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    Better question is why you would use GOSUB without RETURN? That just doesn't have any sense.
    If stack is full then PBP can't call internal subroutines, and it crash...
    You just disabled reset on overflow. That fuse doesn't expand stack to infinity and beyond.

    Better solution to get same result
    Code:
    MainLoop:
     GOSUB SearchForClockSignal
    goto MainLoop
    
    SearchForClockSignal:
       FOR J = 0 TO 7
        USBSERVICE
        SERIN2 PORTE.3, 32, 3, CLOCKSEARCHFAILED, [wait(252), STR RFID_IN\6]
        USBSERVICE 
    
        IF RFID_IN[0] = 252 THEN RETURN 'GOTO MAINLOOP  
    
        SquaresArray[0] = 255
        SquaresArray[1] = 202                       
        SquaresArray[2] = RFID_IN[0]                        
        SquaresArray[3] = RFID_IN[1]  
        SquaresArray[4] = RFID_IN[2]                        
        SquaresArray[5] = RFID_IN[3]
        SquaresArray[6] = RFID_IN[4]                        
        SquaresArray[7] = RFID_IN[5] 
        
        if PPB_DeviceState = 32 then   'ONLY WHEN THERE IS A USB CONNECTION PRESENT     
            GOSUB SendUSBData
        endif
        
        RETURN   'CLOCK SERIAL COMMUNICATION WAS RECEIVED
        
    CLOCKSEARCHFAILED:
        NEXT J
    RETURN

  3. #3
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    It's probably overflowing the stack and halting.
    Dave Purola,
    N8NTA
    EN82fn

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    This should work.

    Code:
    MainLoop:
    
    'DO SOME STUFF HERE.....
    
        FOR J = 0 TO 7
            GOTO SearchForClockSignal
        NEXT J
    
    goto MainLoop
    Last edited by sayzer; - 19th April 2018 at 05:41.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    Quote Originally Posted by pedja089 View Post
    Better question is why you would use GOSUB without RETURN?
    Yes, I'm asking myself the same question . Yes, your code looks better than mine. The program is probably overflowing the stack and halting like Dave said. Will the program restart from the beginning if the stack overflow is enabled?
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  6. #6
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    Yes it should create an internal reset condition.
    Dave Purola,
    N8NTA
    EN82fn

  7. #7
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    Yes. It have almost same effect as MCLR pulled low, then high. Only 2 bits are different, if I remember correctly.

  8. #8
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    Thank you guys.

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

    Anonymous

  9. #9
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    If you want to be a cowboy for some reason you could have used GOTO instead of GOSUB.
    Then you could GOTO back as well (instead of RETURN).

    The difference between GOTO and GOSUB is that GOSUB loads the stack with the location it was called from so that a RETURN can go back there, but GOTO doesn’t need to do that.

    BUT! using a GOTO to go somewhere, and another GOTO to go back, just means that neither GOTO had to exist, and you could have just dropped the routine right in where the GOTO was.

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,793


    Did you find this post helpful? Yes | No

    Default Re: GOTO vs RETURN after a GOSUB

    Also, using GOSUB reduces code size as this Sub can be called from various points of the program to do the same job, maybe with some parameters passing to it.

    Keeps things structured.

    Ioannis

Similar Threads

  1. Is a 'GOTO' Or a 'GoSub'?
    By FromTheCockpit in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 2nd August 2013, 00:27
  2. gosub - return
    By l_gaminde in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 11th August 2010, 11:21
  3. GOTO main or RETURN - Question
    By studysession in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 31st January 2009, 17:13
  4. Return VS Goto?
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th January 2008, 01:13
  5. If ... Then Gosub <> If ... Then Goto?
    By oldtoddler in forum General
    Replies: 6
    Last Post: - 27th February 2006, 12:52

Members who have read this thread : 2

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