A bit off topic; setjmp/longjmp

Discussion in 'C Programming' started by Jrferguson, Oct 29, 2004.

  1. Jrferguson

    Jrferguson Guest

    I have a C program that I am trying to port to a Motorola 68k based system. It
    makes use of setjmp and longjmp which are not supported by my C compiler. I
    understand the general principle behind setjmp/longjmp, but I am somewhat lost
    in the detail of how to actually implement this in assembler. (My compiler
    supports in-line assembler which I hope will prove usefull). I will be very
    gratefull for any help or pointers with this problem. I am conversant with the
    x86 architecture, so an example from one of these platforms would probably be
    illuminating as well.
    John Ferguson
     
    Jrferguson, Oct 29, 2004
    #1
    1. Advertising

  2. Jrferguson wrote:

    > I have a C program that I am trying to port to a Motorola 68k based system.
    > It makes use of setjmp and longjmp which are not supported by my C compiler.


    Why don't you just get a C compiler that supports setjmp and longjmp?
     
    E. Robert Tisdale, Oct 29, 2004
    #2
    1. Advertising

  3. Jrferguson

    Eric Sosman Guest

    Jrferguson wrote:
    > I have a C program that I am trying to port to a Motorola 68k based system. It
    > makes use of setjmp and longjmp which are not supported by my C compiler. I
    > understand the general principle behind setjmp/longjmp, but I am somewhat lost
    > in the detail of how to actually implement this in assembler. (My compiler
    > supports in-line assembler which I hope will prove usefull). I will be very
    > gratefull for any help or pointers with this problem. I am conversant with the
    > x86 architecture, so an example from one of these platforms would probably be
    > illuminating as well.


    Are you sure setjmp() and longjmp() are not provided?
    They are required to be present (and to work as advertised)
    in any Standard-conforming hosted implementation.

    A conforming "freestanding" implementation can omit most
    of the Standard library facilities, including setjmp() and
    longjmp(). If your implementation does in fact omit them,
    the job of coming up with substitutes is likely to be more
    than a little difficult. Not only must setjmp() and longjmp()
    have intimate knowledge of the way the compiler generates code,
    but the compiler must often be made aware that setjmp() and
    longjmp() are "special" and violate the ordinary flow of
    control. If setjmp() and longjmp() are absent, I'd suggest
    you look long and hard for a suitable implementation that
    provides them before undertaking to add them yourself. The
    widely-available gcc compiler supports various M68K-based
    systems, and it might well be easier to switch than fight.

    Unfortunately, we here in comp.lang.c can't help you much
    if you are eventually forced to roll your own. The tricks
    you'll need to use will be highly specific to your machine
    and to your compiler, and will have little or nothing to do
    with the C language.

    One thing we *might* be able to help with is rearranging
    the code so it doesn't require setjmp() and longjmp() in the
    first place. That's not always practical, but in cases where
    it happens to be so the techniques you'll use will be part of
    the C language and hence topical here.

    Good luck: I have a feeling you'll need it ...

    --
     
    Eric Sosman, Oct 29, 2004
    #3
  4. Jrferguson

    jacob navia Guest

    Jrferguson wrote:
    > I have a C program that I am trying to port to a Motorola 68k based system. It
    > makes use of setjmp and longjmp which are not supported by my C compiler. I
    > understand the general principle behind setjmp/longjmp, but I am somewhat lost
    > in the detail of how to actually implement this in assembler. (My compiler
    > supports in-line assembler which I hope will prove usefull). I will be very
    > gratefull for any help or pointers with this problem. I am conversant with the
    > x86 architecture, so an example from one of these platforms would probably be
    > illuminating as well.
    > John Ferguson
    >

    Having done that for the x86 architecture the principle is as follows:
    1: You need a buffer large enough to hold all registers.
    2: setjump saves all registers into that buffer
    3: longjmp restores all registers from the buffer

    Now, you should take care with the return and the result.
    In setjmp, save the return address in the buffer. Return zero.
    In longjmp, after restoring all registers (without restoring the
    instruction pointer of course) set the result register to 1,
    and jump to the saved return address instead of returning from the
    procedure.

    Looks simple but it isn't.

    Good luck.

    Jacob
     
    jacob navia, Oct 29, 2004
    #4
  5. Jrferguson

    Dan Pop Guest

    In <clu7iv$fsd$> Eric Sosman <> writes:

    >control. If setjmp() and longjmp() are absent, I'd suggest
    >you look long and hard for a suitable implementation that
    >provides them before undertaking to add them yourself. The
    >widely-available gcc compiler supports various M68K-based
    >systems, and it might well be easier to switch than fight.


    I have news for you: gcc is *not* a complete implementation; it is missing
    *exactly* the part the OP needs: the standard C library. No point in
    directing the OP to another compiler without what he needs.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Nov 2, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Baruchel

    How dirty is setjmp+fopen+longjmp ?

    Thomas Baruchel, Oct 2, 2003, in forum: C Programming
    Replies:
    2
    Views:
    464
    Eric Sosman
    Oct 2, 2003
  2. Mantorok Redgormor

    setjmp, longjmp

    Mantorok Redgormor, Nov 12, 2003, in forum: C Programming
    Replies:
    2
    Views:
    440
    Ian Woods
    Nov 12, 2003
  3. someone
    Replies:
    5
    Views:
    3,501
    SM Ryan
    May 1, 2004
  4. Michael B Allen

    Is setjmp/longjmp ok?

    Michael B Allen, May 1, 2004, in forum: C Programming
    Replies:
    11
    Views:
    2,271
    -wombat-
    May 4, 2004
  5. Ravi Uday

    setjmp/longjmp

    Ravi Uday, Aug 3, 2004, in forum: C Programming
    Replies:
    2
    Views:
    554
    Dave Vandervies
    Aug 6, 2004
Loading...

Share This Page