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
  • 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
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 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
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.

Copy Code To ClipboardCopy Code To Clipboard
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.
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