setjmp Function (ROM Call 0x266)

setjmp.h

short setjmp (void *j_buf);

Sets up for nonlocal goto.

setjmp captures the complete task state in j_buf and returns 0. j_buf is usually a buffer of type JMP_BUF. A later call to longjmp with j_buf restores the captured task state and returns in such a way that setjmp appears to have returned with the value val. A task state consists of address registers A2-A7, data registers D2-D7, and the program counter.

setjmp must be called before longjmp. The routine that calls setjmp and sets up j_buf must still be active and cannot have returned before the longjmp is called. If it has returned, the results are unpredictable (and usually results with a crash). setjmp is useful for dealing with errors and exceptions encountered in a low-level subroutine of a program. setjmp returns 0 when it is initially called. If the return is from a call to longjmp, setjmp returns a nonzero value.

Note: The saved task state is not complete enough that setjmp can be used to implement coroutines (i.e. multitasking). Namely, registers A0, A1, D0, D1 and SR are not included into the task state.