[Android Pro] ESP and EBP stack top hands and stack bottom pointers

Source: Internet
Author: User

cp:http://blog.csdn.net/hutao1101175783/article/details/40128587

(1) ESP: Stack pointer register (extended stack pointer), which holds a pointer that always points to the top of the stack frame at the top of the system stack.

(2) EBP: Base point pointer Register (extended base pointer), which holds a pointer that always points to the bottom of the top stack frame of the system stack.

"This highlights: learn a few common register names, remember that EAX is generally used to save the function's return value, remember that ESP is the stack-top pointer register, EBP is the stack-bottom pointer register. 】
The pointer in the ESP will always point to the new location, so the address data in the ESP is dynamic.

-----

cp:http://blog.csdn.net/yeruby/article/details/39780943

ESP is a stack pointer, is determined by the CPU mechanism, push, pop instructions will automatically adjust the value of ESP;

EBP just accesses a certain moment of ESP, this moment is entered into a function, the CPU will be the value of the ESP assigned to EBP, at this time, the stack can be manipulated through EBP, such as the acquisition of function parameters, local variables, in fact, the use of ESP can also;

Since the use of ESP is also possible, then why should you set up EBP?

The answer is for the convenience of programmers.

Because ESP changes continuously while the function is running, it is convenient for programmers to access parameters and local variables by saving an ESP into a function, and it is also convenient for the debugger to parse the stack condition during function calls. As I said earlier, this EBP is not a must-have, you have to use ESP to access function parameters and local variables is also feasible, but this will be troublesome.

Understand ESP and EBP through a program:

Main () {

Before executing test

print (int p1,int p2);

After you execute test

}

Analyze the invocation principle of the above program, assuming that the esp=q is executed before print:

Push P2; function parameters P2 into the stack, esp=q-4h

Push P1; Function parameters P1 into the stack, esp=q-8h

Call print; function returns the address into the stack, esp=q-0ch

Now go inside print and do some preparation:

Push EBP; Protect the previous EBP pointer, ebp into the stack, esp=q-10h

MOV esp,ebp; Setting EBP equals the current ESP

At this point, ebp+0ch=q-4h, the location of P2

Similarly, the location of ebp+08h=q-8h, or P1

Here are some of the actions in print:

Sub esp,20h; Set the 10H size of the local variable space, esp=q-20h

// ... ...

A range of operations

// ... ...

Add esp,20h; Releasing local variable space, esp=q-10h

Pop ebp; Out of the stack, restoring the value of the original EBP, esp=q-0ch

RET 8; RET returns, pops up the return address of the previous stack, esp=q-08h, followed by the operand 8H to balance the stack

After that, the popup function parameter, esp=q, resumes the stack before the print function is executed;

As shown, note that the stack's growth direction in memory is reversed:

Execute push p2; before, esp=q;

Execute push p2; in the process, esp-=4h,p2 into the stack;

Execute push p2; esp=q-4h;

[Android Pro] ESP and EBP stack top hands and stack bottom pointers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.