Three registers: SS, SP, and BP

Source: Internet
Author: User

Three registers: SS, SP, and BP


SS: the segment address of the stack;
SP: Stack register Sp (Stack pointer) stores the stack offset address;

BP: base pointer register BP (base pointer) is a register. It is used in conjunction with the stack pointer SP for SP calibration, it can be used only when data in the stack is searched and individual addressing methods are used.
For example, if a stack contains a lot of data or addresses, you must use SP to access the data or addresses. But SP must point to the top of the stack and cannot be changed randomly, at this time, you need to use BP to pass the sp value to BP and use BP to find the data or address in the stack. generally, in addition to data storage, it can be used as a pointer register for memory addressing. In this case, it is used with the SS-stack segment register by default. BP is 16-bit, and the 16-bit extension is EBP, which is used in a 32-bit programming environment. generally, parameters in advanced languages are transferred. After being converted to assembly, BP/EBP is often responsible for addressing and processing.

SP and BP are generally used together with the segment register ss to determine the address of a unit in the stack register. SP is used to indicate the offset address at the top of the stack,BP can be used as a base address in the stack area to determine the operand address in the stack.

(Below is like in Win32 compilation)
BP is the base address register, which is generally used in the function to save the stack base address of the SP that enters the function.
Each time a sub-function is called, the system will save the two pointers at the beginning and restore the SP and Bp values at the end of the function. As shown below:
When the function enters:
Push BP // Save the BP pointer
MoV bp, SP // pass the SP pointer to BP. At this time, BP points to the base address of SP.
// If this function has parameters, [bp + 2*4] is the first parameter of the subfunction, [bp + 3*4] is the second parameter of the subfunction, and so on. The number of parameters is [bp + (n-1) * 4].
.....
.....
When the function ends:
MoV sp, BP // returns the original SP pointer to SP
Pop BP // restore the original BP value.
RET // exit the subfunction

Http://hi.baidu.com/donghongchen/blog/item/486ac300e96dc4027bec2c80.html


Http://my.oschina.net/orion/blog/15879
The following is the assembly code for calling the test (INT P1, int P2) function according to the call Convention _ stdcall.
Suppose that the pre-function Stack pointer ESP is NN
Push P2; parameter 2 into the stack, ESP-= 4 h, esp = nn-4 h
Push P1; parameter 1 into the stack, ESP-= 4 h, esp = nn-8 h
Call test; push the return address ESP-= 4 h, esp = nn-0ch (note that the call command will push the return address to the stack)
; // Enter the Function
{
Push EBP to protect the previous EBP pointer, EBP into the stack, ESP-= 4 h, esp = nn-10 h
MoV EBP, esp; Set EBP pointer to stack top NN-10h
MoV eax, dword ptr [EBP + 0ch]; EBP + 0ch for the NN-4h, that is, the location of parameter 2 here we can see the role of BP
MoV EBX, dword ptr [EBP + 08 h]; EBP + 08h for the NN-8h, that is, the position of parameter 1 here we can see the role of BP
Ub esp, 8; space occupied by local variables ESP-= 8, esp = NN-18h (The address at the bottom of the stack is large.)

.
...
Add ESP, 8; release local variables, esp + = 8, esp = NN-10h

(Assuming that the EBP has not changed in the preceding command, mov ESP and EBP can achieve stack balance,

In fact, this is also often used)
Pop EBP; out stack, recovery EBP, esp + = 4, esp = NN-0Ch
RET 8; RET return, the return address is displayed, esp + = 4, esp = NN-08h,

The operand 8 is the balanced stack, esp + = 8, esp = nn, And the stack before the function is restored.

; Why is it 8? Because the test sub-function has two parameters, 8 corresponds to two parameters, and SP is reduced by 8 when two parameters are imported into the stack.

}
In the past, esp was a pointer always pointing to the top of the stack, while EBP only accessed the top pointer of the stack at a certain time point to facilitate stack operations, such as obtaining function parameters and local variables.

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.