This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Some confuse about the pass of the arguments by gcc
- From: åè <yxy dot 716 at gmail dot com>
- To: James Courtier-Dutton <james dot dutton at gmail dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Wed, 22 Feb 2012 21:34:07 +0800
- Subject: Re: Some confuse about the pass of the arguments by gcc
- Authentication-results: mr.google.com; spf=pass (google.com: domain of yxy.716@gmail.com designates 10.204.174.13 as permitted sender) smtp.mail=yxy.716@gmail.com; dkim=pass header.i=yxy.716@gmail.com
- References: <CA+fE_MS_mk81miNq-qe5UenzgfqRKb+mDrXpjt0OfP0q2uifOQ@mail.gmail.com> <CAAMvbhEfabd2-TwWkfW88iX=h8y_FXstdA9friSk+WLnUvFejw@mail.gmail.com>
2012/2/22 James Courtier-Dutton <james.dutton@gmail.com>:
> The order that function parameters are evaluated is undefined. Therefore it
> is wise to ensure that no matter what order they are evaluated, the result
> should be the same. It is the ++ that breaks it in this case. Didn't you get
> a compiler warning?
Yes you are right. gcc -Wall indeed get the warning.
operation on 'a' may be undefined [-Wsequence-point]
Thanks for you reminder.
Let me know ,If we want the result we want ,we should do the precision
logic design .
> On Feb 21, 2012 3:19 PM, "åè" <yxy.716@gmail.com> wrote:
>>
>> I do a experiments to check how gcc pass the arguments.
>> here is the code
>>
>> #include <stdio.h>
>> int main(int argc , char *argv[]){
>> int a=3;
>> int b=3;
>> int c=3;
>> printf("%d %d\n",++a+c,a+c);
>> printf("%d %d\n",++b,b);
>> return 0;
>> }
>>
>> the anwer is
>>
>> 8 7
>> 4 4
>>
>> the piece of assembly language: gcc 4.6.2
>>
>> movl $3, 28(%esp)
>> movl $3, 24(%esp)
>> movl $3, 20(%esp)
>> movl 20(%esp), %eax
>> movl 28(%esp), %edx
>> leal (%edx,%eax), %ecx
>> addl $1, 28(%esp)
>> movl 20(%esp), %eax
>> movl 28(%esp), %edx
>> addl %eax, %edx
>> movl $.LC0, %eax
>> movl %ecx, 8(%esp)
>> movl %edx, 4(%esp)
>> movl %eax, (%esp)
>> call printf
>> addl $1, 24(%esp)
>> movl $.LC0, %eax
>> movl 24(%esp), %edx
>> movl %edx, 8(%esp)
>> movl 24(%esp), %edx
>> movl %edx, 4(%esp)
>> movl %eax, (%esp)
>> call printf
>>
>> In the first case , gcc first compute the a+c to %ecx ,and pass it
>> stack , the compute ++a+c to %edx ,so the answer is 8 7
>>
>> In the second case , why it didn't do the same thing like
>> compute b=3 and pass it to stack ,then compute ++b and pass it to
>> stack .to the result 4 3. However it first
>> addl $1, 24(%esp) ==> b+1 I think it compute the expression
>> b+1. the pass it to stack . the b which now is 4 and was passed to
>> stack.
>>
>> I was wondering why gcc handle the same mode in two ways.
>>
>> Is there some errors I had made ?
>> In my opinion ,for the reason of the concept of consistent ,it may be
>> 8 8
>> 4 4
>>
>> thank you advance