Re: 'C' Operators precedence

Matthew Kirkwood (weejock@ferret.lmh.ox.ac.uk)
Wed, 20 May 1998 18:58:57 +0100 (BST)


On Wed, 20 May 1998, Richard B. Johnson wrote:

> Look damnit..........
> Script started on Wed May 20 12:00:36 1998
> # cat aaa.c
> #include <stdio.h>
>
> static int a, b, c, d, e;
> static int f()
> {
> return a;
> }
> int main()
> {
> b = 1;
> c = 2;
> d = 3;
> e = 4;
> printf("%d\n", b + c + d + d + f());
> return 0;
>
> }
>
> # gcc -S -o aaa -Wall -pedantic -ansi aaa.c
> # cat aaa
> .file "aaa.c"
> .version "01.01"
[snip]
> main:
> pushl %ebp
> movl %esp,%ebp
> movl $1,b <---- variables initialized
> movl $2,c
> movl $3,d
> movl $4,e
> call f <------- function called first
> movl %eax,%eax
> movl b,%edx <------- now other variables evaluated.
> addl c,%edx
> movl %edx,%ecx
> addl d,%ecx
> movl %ecx,%edx
> addl d,%edx
> addl %edx,%eax
> pushl %eax
[snip]

Script started on Wed May 20 18:53:48 1998
$ cat aaa.c
[as before, except]
printf("%d\n", f() + b + c + d + d);
^ note changed order here.
$ gcc -S -o aaa -Wall -pedantic -ansi aaa.c
$ cat aaa
.file "aaa.c"
.version "01.01"
[more snip]
main:
pushl %ebp
movl %esp,%ebp
movl $1,b
movl $2,c
movl $3,d
movl $4,e
call f <-- function called first.
movl %eax,%eax
movl %eax,%edx <-- then other vars used
addl b,%edx
movl %edx,%eax
addl c,%eax
movl %eax,%edx
addl d,%edx
movl %edx,%eax
addl d,%eax
pushl %eax

And this _without_ optimisation. gcc is right to do this; if a function
clobbers registers, then let it do so when the variables don't contain
anything interersting anyway.

Your point was..?

Matthew.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu