Setjmp Library
The Setjmp library contains functions and types definitions for bypassing the normal function call and return discipline.
Library Routines
Setjmp
Prototype |
sub function setjmp(dim byref env as word[4]) as integer |
---|---|
Returns |
|
Description |
This function saves calling position for a later use by longjmp. Parameters :
|
Requires |
Nothing. |
Example |
dim buf as word[4] ... Setjmp(buf) |
Longjmp
Prototype |
sub procedure longjmp(dim byref env as word[4], dim val as integer) |
---|---|
Returns |
Nothing. |
Description |
Restores calling environment saved in the Parameters :
|
Requires |
Invocation of |
Example |
dim buf as word[4] ... 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 dim buf as word[4] ' Note: Program flow diagrams are indexed according ' to the sequence of execution sub procedure func33() ' 2<------------| Delay_ms(1000) ' | ' | nop ' | longjmp(buf, 2) ' 3---------------->| nop ' | | ' | | end sub ' | | ' | | sub procedure func() ' 1<--------| | | PORTB = 3 ' | | | if (setjmp(buf) = 2) then ' 3<----------------| PORTB = 1 ' 4-->| | | else ' | | | func33() ' 2------------>| end if ' | | ' 4<--| | end sub ' 5----->| | ' | | main: ' | | 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!