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
  • 0 if the return is from direct invocation
  • nonzero value if the return is from a call to longjmp (this value will be set by the longjmp routine)
Description

This function saves calling position for a later use by longjmp.

Parameters :

  • env: buffer suitable for holding information needed for restoring calling environment

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 env buffer by the most recent invocation of setjmp. If there has been no such invocation, or the function containing the invocation of setjmp has terminated in the interim, the behavior is undefined.

Parameters :

  • env: buffer holding the information saved by the corresponding setjmp invocation
  • val: value to be returned by the corresponding setjmp function

Requires

Invocation of longjmp must occur before return from the function in which setjmp was called encounters.

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.

Copy Code To ClipboardCopy Code To Clipboard
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.
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code