A bit off topic; setjmp/longjmp

J

Jrferguson

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
 
E

E. Robert Tisdale

Jrferguson said:
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

Eric Sosman

Jrferguson said:
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 ...
 
J

jacob navia

Jrferguson said:
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
 
D

Dan Pop

In said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top