analysis code(value passing, pointer(address) passing)

来源:互联网 发布:mac 桌面上多了个磁盘 编辑:程序博客网 时间:2024/06/09 22:22

1. none change

1.1)

int a = 9;

int add1(int a,int b)
{
    a = a + 1;
}

push ebp
      mov  ebp,esp
      mov  eax,dword ptr ss:[ebp+8]
      add  eax,1

      mov dword prt ss:[ebp+8], eax

here only change temp stack's [ebp+8] location's value.

after invoke the function add1, the stack will be recovery, and the [ebp+8] location's value will has none meaning.

So the a still is not changed, it is 9.

 
 1.2)

void cstart(int a,int b,int c)
{
 a=1;
 c=3;
 b=2;
}

call .**********(该函数的地址)           ;esp=0x00011800

push ebp                                  ;esp=0x000117fc

mov ebp,esp                              ;esp=0x000117f8,ebp=0x000117f8

mov dword ptr ss:[ebp+0x8], 0x00000001    ;esp=0x000117f8,ebp=0x000117f8

mov dword ptr ss:[ebp+0xc], 0x00000003   ;esp=0x000117f8,ebp=0x000117f8

mov dword ptr ss:[ebp+0x10], 0x00000002    ;esp=0x000117f8,ebp=0x000117f8

pop ebp

ret
汇编调用c函数时 <wbr>堆栈的变化的详细分析

 

2 pointer to value.

a has changed.

int i = 9;

int *a = &i;

int add1(int *a,int b)
{
    *a = *a + 1;
}

As it change the temp stack's [ebp+8] location's value(actually value address) """"ssss pointed value.

So this pointer is not change(in stack and exit stack it's the same).

alrough the pointer is same after exit invoking function,

But it's pointed value is followed to change as the temp stack's value's pointed NEW value.

 

原创粉丝点击