How can I avoid stack corruption


Closed Thread
Results 1 to 3 of 3
  1. #1

    Default How can I avoid stack corruption

    I need a fast subroutine to transmit a character, increment the CheckSum and check if a Reset "R" command has come in.
    If the "R" is found I want to immediately jump back to the start of the sending program. This means the code exits the subroutine via a GOTO instead of the normal RETURN. How can I get around this?

    SendThis:
    'Sends the next TxChar, increments CheckSum, looks for "R" command
    hserout [txchar]
    checksum = checksum + txchar
    hserin 0, norxdata, [rxchar] 'note - 0 is a valid timeout - yeah!
    if rxchar = "R" then searchpoint
    'Reset & back to the SearchPoint to look for the next GO command.
    'Does this structure have a stack corruption problem? RETURN not executed.
    'R should work within a character time.
    NoRxData:
    return

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


    Did you find this post helpful? Yes | No

    Default Re: How can I avoid stack corruption

    Hi,
    Use a flag, when you receive your 'R' command you set the flag and then RETURN. Check the flag and GOTO SearchPoint if set. In SearchPoint clear the flag.
    Perhaps something like this:

    Code:
    Abort VAR BIT
    
    Main:
      '...do whatever....
      GOSUB SendThis
      If Abort = 1 THEN SearchPoint
    
    SearchPoint:
      Abort = 0   'Clear flag
      '...do whatever...
    
    Goto Main
    
    SendThis:
      'Sends the next TxChar, increments CheckSum, looks for "R" command
      hserout [txchar]
      checksum = checksum + txchar
      hserin 0, norxdata, [rxchar] 'note - 0 is a valid timeout - yeah!
    
      if rxchar = "R" then Abort = 1   ' Set flag to indicate receives reset command
    
      'Reset & back to the SearchPoint to look for the next GO command.
      'Does this structure have a stack corruption problem? RETURN not executed.
      'R should work within a character time.
    NoRxData: 
      return
    It'll add a couple of instruction cycles but I'm pretty sure it'll be fast enough, give it a try.

    /Henrik.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: How can I avoid stack corruption

    Thanks Henrik.
    Your approach works but I call the 'SendThis' subroutine over 2000 times per loop, from different parts of the code, so I was trying to avoid all the additional "If Abort = 1 then SearchPoint" lines.

    Thanks for your help.
    Cheers
    Brian

Similar Threads

  1. USB data corruption
    By Charlie in forum USB
    Replies: 7
    Last Post: - 23rd March 2012, 12:34
  2. Beginner's stack of FAQ... to solve stack of facts?
    By mister_e in forum FAQ - Frequently Asked Questions
    Replies: 1
    Last Post: - 26th June 2011, 19:52
  3. How to avoid DIV32 command?
    By pxidr84 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 16th May 2011, 06:11
  4. Stack Overflow - Stack Underflow
    By massive attack in forum mel PIC BASIC
    Replies: 4
    Last Post: - 1st September 2010, 13:39
  5. How to avoid corrupting an EEPROM Write
    By jessey in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th July 2005, 08:34

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