ZBasic Language Reference
100
ZBasic Microcontrollers
in bar()
returning from bar()
returning from foo()
normal return from foo()
end test
Note that a call to LongJmp() generally should not utilize a value of zero as the second parameter.
Doing so will make it appear as though the original SetJmp() call is returning.
The jump buffer may also be a local variable if desired. The example code below is a modified version of
the previous example showing how the jump buffer is passed down the hierarchy as a parameter. This
technique may be used to create generalized subroutines that might return to one of several places
depending on how it was called.
Sub Main()
Dim jmpBuf1(1 to System.JumpBufSize) as Byte
debug.print "start test"
' initialize the jump buffer
Select Case SetJmp(jmpBuf1)
Case 0
' control came back from SetJmp()
debug.print "calling foo()"
Call foo(jmpBuf1)
debug.print "normal return from foo()"
Case 1
' control came back from LongJmp()
debug.print "SetJmp() returned 1 via LongJmp()"
End Select
debug.print "end test"
End Sub
Sub foo(ByRef jb() as Byte)
debug.print "in foo()"
Call bar(jb)
debug.print "returning from foo()"
End Sub
Sub bar(ByRef jb() as Byte)
debug.print "in bar()"
Call LongJmp(jb, 1)
debug.print "returning from bar()"
End Sub
3.33 Run Time Stack Checking
On VM mode devices (e.g. the ZX-24a), the control program on the ZX implements optional run-time
stack overflow detection. You can enable and disable the checking at any time using the System Library
subroutine StackCheck(). There is a small performance penalty for the stack check although the
checking is only done after operations that add data to the stack.
When stack checking is enabled the stack pointer is compared against the end of stack value in the
current tasks Task Control Block less the current setting of Register.StackMargin. If the stack
pointer exceeds the limit a stack fault is generated and the processor is reset. When the processor
begins running again Register.ResetFlags will indicate that a WatchDog reset occurred and
Register.FaultType will have the value 1 indicating a stack fault condition occurred. Also,
Register.FaultData will contain the address of the Task Control Block of the task that was running
when the stack fault was detected. Register.FaultData2 will contain the address of the instruction
that was executing at the time of the fault. Note that Register.FaultType is a persistent value. If you
|