This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Code representations
- From: Kai Tietz <Kai dot Tietz at onevision dot com>
- To: "James Courtier-Dutton" <james dot dutton at gmail dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Mon, 28 Apr 2008 13:45:26 +0200
- Subject: Re: Code representations
gcc-owner@gcc.gnu.org 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.