Setjmp Library

This library contains functions and types definitions for bypassing the normal function call and return discipline. The type declared is jmp_buf which is an array type suitable for holding the information needed to restore a calling environment.

Type declaration is contained in sejmp16.h and setjmp18.h header files for PIC16 and PIC18 family mcus respectively. These headers can be found in the include folder of the compiler. The implementation of this library is different for PIC16 and PIC18 family mcus. For PIC16 family Setjmp and Longjmp are implemented as macros defined in setjmp16.h header file and for PIC18 family as functions defined in setjmp library file.

  Note : Due to PIC16 family specific of not being able to read/write stack pointer, the program execution after Longjmp ivocation occurs depends on the stack content. That is why, for PIC16 family only, implementation of Setjmp and Longjmp functions is not ANSI C standard compliant.

Library Routines



int setjmp(jmp_buf env);


if the return is from direct invocation it returns 0
if the return is from a call to the longjmp it returns nonzero value


This function saves calling position in jmp_buf for later use by longjmp. The parameter env: array of type (jmp_buf) suitible for holding the information needed for restoring calling environment.






void longjmp(jmp_buf env, int val);


longjmp causes setjmp to return val, if val is 0 it will return 1.


Restores calling environment saved in jmp_buf by most recent invocation of setjmp macro. If there has been no such invocation, or function conatinig the invocation of setjmp has terminated in the interim, the behaviour is undefined.Parameter env: array of type (jmp_buf) holding the information saved by corresponding setjmp invocation, val: char value, that will return corresponding setjmp.


Invocation of Longjmp must occur before return from the function in which Setjmp was called encounters.

longjmp(buf, 2);

Library Example

Example demonstrates function cross calling using setjmp and longjmp functions. When called, Setjmp() saves its calling environment in its jmp_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 jmp_buf argument. The given example is for P16.

Copy Code To ClipboardCopy Code To Clipboard
#include <setjmp16.h>

jmp_buf buf;             // Note: Program flow diagrams are indexed according
                         //       to the sequence of execution

void func33(){           //  2<----------|
  Delay_ms(1000);        //              |
                         //              |
  asm nop;               //              |
  longjmp(buf, 2);       //  3-------------->|
  asm nop;               //              |   |
                         //              |   |
}                        //              |   |
                         //              |   |
void func(){             //  1<------|   |   |
                         //          |   |   |
  PORTB = 3;             //          |   |   |
  if (setjmp(buf) == 2)  //  3<--------------|
    PORTB = 1;           //  4-->|   |   |
  else                   //      |   |   |
    func33();            //  2---------->|
                         //      |   |
  asm nop;               //  4<--|   |
}                        //  5-------|------->   depends on stack content
                         //          |
void main() {            //          |
                         //          |
  PORTB = 0;             //          |
  TRISB = 0;             //          |
                         //          |
  asm nop;               //          |
                         //          |
  func();                //  1------>|
  asm nop;               //
  PORTB = 0xFF;
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