Analysis of SS, SP, BP Register _ assembly

Source: Internet
Author: User

SS, SP, BP three registers


SS: The segment address of the storage stack;
SP: Stack registers SP (stack pointer) The offset address of the stack;

BP: Cardinal Pointer register BP (base pointer) is a register, its use is a little special, and the stack pointer SP joint use, as SP calibration used, only in the search stack of data and the use of individual addressing methods to use
For example, a lot of data or addresses are pressed into the stack, you definitely want to access the data or address through the SP, but the SP to point to the top of the stack, is not random change, then you need to use BP, the value of the SP to pass to BP, through BP to find the data or address on the stack. Generally, in addition to saving data, can be used as a pointer register for memory addressing, at which point the default matching segment register is the ss-stack segment register. BP is 16-bit, and the 16-bit expansion is EBP for 32-bit programming environments. The general high-level language of the parameter transfer, etc., is converted to the assembly often by the bp/ EBP to be responsible for addressing \ processing.

SP,BP is typically used with the segment register SS to determine the address of a cell in the stack register, the SP is used to indicate the offset of the top of the stack, and BP can be used as a base address in the stack area to determine the operand address on the stack.

(The following is the same as in the Win32 Assembly)
BP is the base address register, typically used in functions to save the SP's top base address when entering the function
Each time a child function is called, the system saves the two pointers at the beginning and restores the SP and BP values at the end of the function. Like the following:
When a function enters:
Push BP//save BP pointer
MOV bp,sp//The SP pointer is passed to BP, at which point the BP points to the base address of the SP.
At this point, if the function has parameters, [bp + 2*4] is the first parameter of the child function, [bp+3*4] is the second parameter of the child function, and so on, how many parameters [Bp+ (n-1) *4].
.....
.....
At the end of the function:
MOV sp,bp//Send the original SP pointer back to SP
Pop BP//restore the original BP value.
RET//exit child function


http://my.oschina.net/orion/blog/15879
The following is the assembly code for calling function test (int p1,int p2) by calling convention __stdcall
; Assume that the stack pointer esp is NN before executing the function
Push P2 parameter 2 in stack, ESP = 4h, esp = nn-4h
Push p1 parameter 1 in stack, esp = 4h, esp = nn-8h
Call Test: Press the return address ESP = 4h, esp = nn-0ch (Note that the command will push the return address onto the stack)
;//Enter function
{
Push ebp; protect previous EBP pointers, ebp into stacks, esp-=4h, ESP = nn-10h
MOV ebp, esp; set the EBP pointer to the top of the stack nn-10h
mov eax, DWORD ptr [ebp+0ch]; ebp+0ch for nn-4h, which is the location of parameter 2 here we can see the effect of BP.
mov ebx, DWORD ptr [ebp+08h]; ebp+08h for nn-8h, which is the location of parameter 1 here we can see the effect of BP.
UB ESP, 8; Local variables occupy space esp-=8, esp = nn-18h (large address at bottom of stack)

                                                       ; Here is the application space for the local variable.
...
add    ESP, 8                                    Free local variables, esp+=8, ESP = nn-10h

                                                         ;(Assuming that EBP is unchanged in the instructions above, direct MOV ESP, EBP can reach the stack balance,

                                                       ; In fact it is often used in this way)
pop    ebp                                        stack, restore EBP, esp+=4, ESP = nn-0ch
ret    8                                              ret return, pop-up return address, esp+=4, esp=nn-08h,

; Add operand 8 to the balance stack, esp+=8,esp=nn, and restore the stack before entering the function

; Why is it 8? Because the test child function has two parameters, 8 is two parameters corresponding to the stack when the SP decreased by 8

}
The original ESP is always pointing to the top of the stack pointer, and EBP just access to the top of the stack pointer, to facilitate the operation of the stack, such as getting function parameters, local variables, etc.

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.