Help with GOSUB


Closed Thread
Results 1 to 17 of 17

Thread: Help with GOSUB

Hybrid View

  1. #1
    Join Date
    Oct 2014
    Location
    Lagos Nigeria
    Posts
    10


    Did you find this post helpful? Yes | No

    Thumbs up Re: Help with GOSUB

    One thing i have learnt about pbp;

    NEVER use GOSUB without a matching RETURN;
    NEVER use RETURN without a matching GOSUB

    but when using ASM, i seem to get away with using goto with return and sometimes i can call a sub that terminates with a goto... don't know why it worked but then maybe pbp GOSUB is very much different from asm CALL

    stanon1

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Help with GOSUB

    Whenever you GOSUB somewhere the adress after from where you GOUSBed is pushed onto the stack (stored in memory). If another GOSUB is executed the adress after THAT gosub is pushed onto the stack and the first adress stored is "pushed down". When a RETURN is executed the adress at the top of the stack is popped (retreived) and the execution jumps to that adress. This is how it works in any language, not just PBP.

    If you GOSUB somewhere you must go back with a RETURN.
    You can not RETURN from somwhere to where you did not GOSUB.

    This too is NOT unique to PBP.

    Now, it's perfectly fine to use GOTO from a subroutine to which you jumped with a GOSUB as long as you eventually end up at a "matching" RETURN. Example:
    Code:
    Main:
    Toggle PortB.0
    Pause 100
    GOSUB CheckSwitch
    Goto Main
    
    CheckSwitch:
    If PortA.0 = 1 THEN
    GOTO DoThis
    ELSE
    GOTO DoThat
    ENDIF
    RETURN
    
    DoThis:
    ' Whatever
    RETURN
    
    DoThat:
    ' Whatever
    RETURN
    In the above example the RETURN instryction at the end of DoThis and DoThat "matches" the GOSUB in the Main loop, Everything is fine. However, if you in somewhere in the main loop (or whatever) did a GOTO DoThat things would go bad because there's a "non matching" RETURN at the end. When the execution hits that RETURN it will pop "something" of off the stack and go there - not what you want.

    Again, this is NOT unique to PBP in any way.

    /Henrik.

Similar Threads

  1. gosub - return
    By l_gaminde in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 11th August 2010, 11:21
  2. On...gosub
    By circuitpro in forum PBP Wish List
    Replies: 5
    Last Post: - 7th December 2009, 22:40
  3. If ... Then Gosub <> If ... Then Goto?
    By oldtoddler in forum General
    Replies: 6
    Last Post: - 27th February 2006, 12:52
  4. gosub and if-then working together?
    By bartman in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 23rd November 2005, 14:54
  5. GoSub Problem
    By ghoot in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th May 2004, 16:15

Members who have read this thread : 0

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