How is the stack pointer changed in this program with call and ret

半腔热情 提交于 2019-12-02 05:20:21

How does the parent function know how to rebuild itself ? Are the necessary steps built into call and ret?

Before calling a function, current status of registers are saved, as well as the return address. call instruction jumps to particular address, where the called function begins. The return address is pushed onto stack. When called function returns, ret instruction pops previously pushed return address and goes to that location.

Then the rsp is always moved to rbp

rbp is previously pushed onto stack to be able to restore rbp's value from caller's function. Then, rsp is moved to rbp to create a new stack frame for callee function. The new base pointer has been set up. So currently, rbp and rsp points to the same addresses. If there are other push instructions, esp is automatically adjusted. When function is done, the pop ebp instruction restores previously pushed stack base pointer address.

Push and Pop modify the stack pointer - SP.

Call pushes FLAGS - status register as well as the RA - return address. Ret pops the FLAGS pops and jumps to the return address.

As rkhb said, the need to keep certain registers as they are comes from the calling conventions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!