PDA

View Full Version : How can I avoid stack corruption



BrianT
- 29th August 2013, 05:58
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

HenrikOlsson
- 29th August 2013, 06:11
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:



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.

BrianT
- 30th August 2013, 02:45
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