jumping out of a gosub reminder


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: jumping out of a gosub reminder

    Here is a simple exercise to review the Stack:
    Code:
    10:  Main:
    20:  DO
    30:   IF PORTB.1 = 1 THEN
    40:      GOSUB CheckIn
    '  Stack Contains 40
    50:    ENDIF
    60:  LOOP
    
    70:  CheckIn:
    80:    IF PORTB.2 = 1 THEN
    90:      GOTO Verify
    ' Stack Still Contains 40
    100:  ENDIF
    110:  RETURN
    
    120:  Verify:
    130:    IF PORTB.1 = 1 THEN
    140:      RETURN
    '  This RETURN jumps to the address in the Stack + 1 (address), so the point of RETURN = 50
    150:    ELSEIF PORTB.2 = 1 THEN
    160:      GOTO NameCalling
    '  Stack Still Contains 40
    170:    ENDIF
    180:  RETURN

  2. #2
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: jumping out of a gosub reminder

    thanks mike

    i am going though this code slowly to find any goto's that have found their way into sub's , fixed about 10 or so that if the error condition happend wold have acted on the goto , and left the stack with the address sitting on there from the gosub

  3. #3
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: jumping out of a gosub reminder

    I commonly use GOTO to jump from one Sub to another when there is nothing left to do in the first Sub. This keeps the Stack from building up, and reduces execution time. Working with the Nextion TFT screens, sending UART commands must terminate with $FF $FF $FF, which I often add in a subroutine. Since that is the last part of the operation, I GOTO FfFfFf: from the last part of the Send_Data: routine, then RETURN from FfFfFf:. It saves a couple jumps, which may involve ASM BANKSELs.

    Know that you are doing it intentionally, know where your Stack is pointing so you know where you ultimately RETURN to, and it can work to your advantage.

  4. #4
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default Re: jumping out of a gosub reminder

    I had similar ideas, but give up on them. I use something like this:
    Code:
    Gosub xx : If err=1 then goto yy
    
    XX:
    if something to triger going to err sub then
    err=1 ' goto in different sub after retun
    else
    err=0 ' return and continue normally
    endif
    But if you must have gosub, without return, you can look up in datasheet of PIC, POP command. It should pop up stack, and free one space.

    Quote Originally Posted by Datasheet
    The TOS value is pulled off the return
    stack and is discarded. The TOS value
    then becomes the previous value that
    was pushed onto the return stack.
    This instruction is provided to enable
    the user to properly manage the return
    stack to incorporate a software stack
    If I understand POP and PUSH commands, you could do something like this

    If err=1 THEN
    @ POP
    GOTO ErrHandler
    ENDIF
    Last edited by pedja089; - 2nd February 2020 at 22:15.

  5. #5
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: jumping out of a gosub reminder

    Don't use both. What was printed in the above post will GOTO a new address, then erase the RETURN address. This will generate a Stack Underflow condition and the PIC will "exhibit unpredictable behavior". Just use GOTO and leave the Stack alone.

  6. #6
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default Re: jumping out of a gosub reminder

    But if I understand correctly he want to goto and never return.
    So you need to pop stack to prevent overflow.

  7. #7
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: jumping out of a gosub reminder

    Then yes, POP the Stack. Not sure when such an approach might be considered good programming practice, though.

Similar Threads

  1. programming jumping game
    By bokken in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 17th February 2012, 06:40
  2. jumping out of subroutine with goto
    By helloo in forum General
    Replies: 4
    Last Post: - 3rd January 2012, 10:37
  3. OWIN not jumping to label
    By MOUNTAIN747 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th March 2011, 15:30
  4. A reminder for all Forum Users
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 40
    Last Post: - 6th December 2008, 18:56
  5. Pot reading jumping like crazy!!!
    By champion in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 20th November 2006, 21:24

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