[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 */
Regards,
i.A. Kai Tietz
| (\_/) This is Bunny. Copy and paste Bunny
| (='.'=) into your signature to help him gain
| (")_(") world domination.