Stack operations and stack Frames

Source: Internet
Author: User

StructuredProgramThe most basic unit of is "function" or "process ". At the compilation layer, commands that support these concepts, such as stack operations and stack frames, are also available.

First of all here to "Open the Door of assembly" that blog to add a point is: assembly language is related to machines, everything here is based on the IA-32 machine platform.

1. Addressing Mode
We already know that there is a way in the operand representation to indicate the content of the memory address. In GNU assembly, there are multiple methods to indicate the memory address. These methods are collectively referred to as "addressing methods ". The general addressing format is "IMM (EB, EI, S)" [1]. Explanation: The expression is calculated in the form of IMM + R [Eb] + R [ei] * s. What is the result of this string? Is a memory address. The operation command uses the memory address calculated by this operand expression to access the memory.

Several common and special forms evolved from common forms are as follows:
1) Imm-pay attention to the difference with $ Imm. The latter is the immediate number, while the former is a memory address carried in the form of immediate number. This method is called absolute addressing;
2) (Ex)-pay attention to the difference with ex. The latter is the register content, while the former is a memory address carried in the form of register content. This method is called indirect addressing;
3) Imm (EB)-indicates that the memory address is Imm + R [Eb];
4) (EB, EI)-the result is that the memory address is R [Eb] + R [ei];
5) Imm (EB, EI)-indicates that the memory address is Imm + R [Eb] + R [ei].

2. Register usage
In the "open assembly door", it was mentioned that although the specificity of registers has been reduced, some registers still have their own special applications. GNU has developed a register usage rule for us, which stipulates that "% eax, % ECx, and % edX are stored by the caller, % EBX, % Ebi, and % ESI are protected by callers, while % ESP and % EBP are dedicated to stack operations ".

3. Stack operations
Stack is actually a dedicated memory area. Each process address space has its own stack zone. Everyone on Earth knows that there are two stack operations: Push and pop. The corresponding GNU Assembly defines "pushl s" and "popl D" respectively to complete the stack pressure and output operations. Each operation involves two steps: Mobile stack top pointer and data transmission.
Pushl S <=> r [% esp] <-- R [% esp]-4; m [R [% esp] <-- S
Popl d <=> d <-- M [R [% esp]; R [% esp] <-- R [% esp] + 4

4. stack frame Formation
When it comes to function or process calling, you cannot leave the stack operation. Every function or process call is inseparable from the concept of "stack frame. Stack is used to pass parameters and save returned results, while stack frames are mapped to a specific process. Stack frames are identified by % EBP. Let's take a look at an example to see what is in the stack frame?
Void callee (int x, int y ){
X = 1;
Y = 2;
}

Void caller (int m, int N ){
Callee (m, n );
}

the Code is:
_ callee:
pushl % EBP // Save the caller's stack frame address
movl % ESP, % EBP // initialize the callee stack frame address
movl $1, 8 (% EBP) // obtain the parameter x Information
movl $2, 12 (% EBP) // obtain the parameter Y Information
popl % EBP
RET
......
......
_ Caller:
pushl % EBP // Save the caller's stack frame address
movl % ESP, % EBP // initialize the caller stack frame address
subl $8, % ESP
movl 12 (% EBP), % eax
movl % eax, 4 (% ESP)
movl 8 (% EBP), % eax
Movl % eax, (% ESP)
call _ callee
Leave
RET
check callee's assembly code: after entering callee, first save the stack frame address of the caller, and then read the parameter information in the caller stack frame for calculation. It can be seen that the stack frame in a process includes at least the starting address of the previous stack frame, and then some parameter information, according to CS. according to the app, stack frames may save some local variables or temporary changes before storing parameter information. The process return address is recorded at the end of the stack frame of each process. This return address is automatically added when the call is executed. Callee obtains parameter information through % EBP +/-offset. You can use the following figure to summarize the stack frame (starting from: byte referred to by % EBP --> terminating: byte of the returned address):

++
|
+ ---------- +
| Old % EBP | <--- % EBP
+ ---------- +
| Local variable |
+ ---------- +
| Parameter n |
+ ---------- +
| Parameter... |
+ ---------- +
| Parameter 1 |
+ ---------- +
| Return address |
+ ---------- +
|... |
| <-- % ESP

[NOTE 1]
The Representation Method in CS. app is used here. EB indicates the base address register, EI indicates the address change register, and s indicates the scaling factor. We use R to reference the value of a register, and m to reference a memory address.

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.