This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Some confuse about the pass of the arguments by gcc
- From: åè <yxy dot 716 at gmail dot com>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 21 Feb 2012 23:18:41 +0800
- Subject: 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.205.132.138 as permitted sender) smtp.mail=yxy.716@gmail.com; dkim=pass header.i=yxy.716@gmail.com
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