stack changes when calling function in c

C code:

Assembler code:


The important extraction that can make usknow about stack is:

first of all, in function main, beforecalling function f, the instruction subtracts esp by 8 because there are 2parameters need to be passed into function f. when the two parameters arepassed into stack function f is called which will push the return address intostack.

Then in function f, the first thing need tobe done is save the previous stack base pointer by pushing it into stack andthen set the current stack pointer as base stack pointer of current function. Thefunction f uses epb to retrieve parameters passed into function f in the main.

The code declares two variables in the functionf which locates in the stack. We can see that the esp is subtracted by 16 toallocate a block of stack memory for storing variables. Here we just allocatetwo variables but the instruction allocates 16 bytes for us. It seems that 16bytes are the minimum size of stack memory that can be allocated in the stack.

Before return from function f the savedprevious stack base pointer need to be set back by poping the stack(popl %ebp).Last instruction ret in the function f will pop the return address saved in thestack and jump to that address.

So the major structure of the stack for theabove program is as follows:


From the code we can use that ebp is usedto retrieve variable in the stack. It severs as a base pointer as its nameindicates.

In a nutshell, when a function is called in c, first of all, the parameters are passed into stack, then function return address, previous stack frame address, and other variables declared inside the function called.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值