[Tutor] Re: Why x+=y instead of x=x+y?

lumbricus@gmx.net lumbricus@gmx.net
Wed, 24 Jul 2002 12:59:51 +0200 (MEST)


> > | It comes from C which was initially implemented on a 
> > | computer which had a different set of assembler instructions 
> > | for generic addition and 'self addition' )
> > 
> > I think it is also related to the complexity of the compiler.  I
> > expect that any modern compiler will turn "x+=1" into INC X.  
> 
> If optimisation is turned off I sincerely hope it doesn't!

Why not?

$ gcc --version
egcs-2.91.66
$ uname
Linux
$ cat inc.c
#include <stdio.h>
int main (void) {
        int i=0;
        i++;
        i+=1;
        i=i+1;
        return 0;
}

$ cc -S inc.c
$ cat inc.s
	[ snip ]
        incl -4(%ebp)
        incl -4(%ebp)
        incl -4(%ebp)
	[ snip ]
$ 

HAND and Greetings, J"o!

-- 


-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net