"James Courtier-Dutton" <[EMAIL PROTECTED]> wrote on 28.04.2008 
15:28:56:

> 2008/4/28 Kai Tietz <[EMAIL PROTECTED]>:
> > [EMAIL PROTECTED] wrote on 28.04.2008 13:11:39:
> >
> >
> >
> >  > I am trying to look at assembler code, and representing it as C 
code.
> >  >
> >  > For ia32, x86 platforms,
> >  > assembler like the following
> >  >
> >  > ADD eax,ebx;
> >  > JO integer_overflow_detected;
> >  >
> >  > How would I represent this in C?
> >  >
> >  > Kind Regards
> >  >
> >  > James
> >
> >  It would be something like this:
> >
> >  #define MSB (type)  ((1<<((sizeof(type)*8)-1))
> >  typedef unsigned int myscalar;
> >  ...
> >  {
> >   myscalar a,b,savea;
> >   ...
> >   savea = a;
> >   a+=b;
> >   if ( ((savea & MSB(myscalar)) & ((b & MSB(myscalar)) & ~(a &
> >  MSB(myscalar)))) ||
> >     ( ~(savea & MSB(myscalar)) & ~(b&MSB(myscalar)) & 
(a&MSB(myscalar))))
> >      /* overflow */
> >   ...
> >  }
> >
> >  For signed integers you can ease this as follow
> >
> >   savea = a;
> >   a+=b;
> >   if ( (savea<0 && b<0 && a>=0)) ||
> >     (savea>=0 && b>=0 && a<0))
> >      /* overflow */
> >
> 
> I am taking a wild guess here, but can I assume that the above will
> not compile back to something like:
> ADD eax,ebx;
> JO integer_overflow_detected;
This is a matter of the optimization. But I guess that gcc won't optimize 
this to the same instruction you wrote, too.
But you queried 'How would I represent this in C?' and the above code is 
the c representation of your assembler, sure.

> I think I will have to have some macro/function in C that does the 
following:
> add(int a,int b, int (&integer_overflow_detected));
> This will add a and b, and jump to the overflow handler if there is 
> an overflow.
> I can then implement CPU specific implementations for each target
> platform, and that would at least return to the same ASM code
> generated at compile.

May the better choice for your purpose.


Kai

|  (\_/)  This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.

Reply via email to