Setjmp Library
The Setjmp library contains functions and types definitions for bypassing the normal function call and return discipline.
Library Routines
Setjmp
Prototype |
function setjmp(var env : array[4] of word) : integer; |
---|---|
Returns |
|
Description |
This function saves calling position for a later use by longjmp. Parameters :
|
Requires |
Nothing. |
Example |
var buf : array[4] of word; ... Setjmp(buf); |
Longjmp
Prototype |
procedure longjmp(var env : array[4] of word; val : integer); |
---|---|
Returns |
Nothing. |
Description |
Restores calling environment saved in the Parameters :
|
Requires |
Invocation of |
Example |
var buf : array[4] of word; ... Longjmp(buf, 2); |
Library Example
Example demonstrates function cross calling using setjmp and longjmp functions. When called, Setjmp() saves its calling environment in its buf argument for later use by the Longjmp(). Longjmp(), on the other hand, restores the environment saved by the most recent invocation of the Setjmp() with the corresponding buf argument.
program Setjmp; var buf : array[4] of word ; // Note : Program flow diagrams are indexed according // to the sequence of execution procedure func33(); // 2<------------| begin // | Delay_ms(1000); // | // | nop; // | longjmp(buf, 2); // 3---------------->| nop; // | | // | | end; // | | // | | procedure func(); // 1<--------| | | begin // | | | PORTB := 3; // | | | if (setjmp(buf) = 2) then // 3<----------------| PORTB := 1 // 4-->| | | else // | | | func33(); // 2------------>| // | | // 4<--| | end; // 5----->| | // | | begin // | | ADPCFG := 0xFFFF; // | | // | | PORTB := 0; // | | TRISB := 0; // | | // | | nop; // | | // | | func(); // 1-------->| // | nop; // 5<-----| Delay_ms(1000); PORTB := 0xFFFF; end.
What do you think about this topic ? Send us feedback!