A simple approach to the process assembly stack frame pointer __ algorithm

Source: Internet
Author: User


a simple approach to the process assembly reprinted from the Troublemaker Boy while young, using code to achieve Dreams-daoluan.net not ignoring the compilation

than the high-level language of our daily contact, such as C language, C++,java, and so on, assembly language is closer to the machine, its common operation is simple to put a numerical (immediate number, register number or memory data) loaded into the register, it is so that the assembly to complete a program task, the process will be more obscure The high-level language hides a lot of machine details (such as the initialization of the process (function) stack frame and the restoration of the stack frame at the end of the process), and the code is clear and understandable.

I really admire the 670 's those Daniel, how come over ... Worship and worship. Write an integer within 100 and, even if there is a full assembly of documents, this is enough to toss me for a while, it's disgusting. But understanding how the assembly behaves and some of its important details helps to understand how computer software and hardware work. I have a simple algorithm to understand the assembly. Process Assembly Prelude

The process can be understood as a function in C, when the caller (caller) invokes the callee (be caller), the system allocates space for the callee within the stack, which is called the stack frame. The structure of the stack is probably as follows:

The stack is a low address growth stack, similar to the stack structure in the data structure, has LIFO nature, register%ESP (stack pointer) Save the address of the top of the stack, register%EBP (* * pointer) Save the address of the frame pointer. When the program executes, the stack pointer can be moved to increase or decrease the stack space, while the frame pointer is fixed because most of the data stored in the stack is relative to the frame pointer (frame pointer + offset).

When the caller invokes another procedure: first, if the invoked procedure has parameters, the parameters are constructed in the stack frame of the call and are deposited into the caller's stack frame (so the above diagram argument n ...). Parameter 1, that's the reason); The address is returned to the stack. The return address is the instruction address that the caller should continue to execute after the invoked procedure has been executed; it belongs to the caller stack frame, forming the end of the caller's stack frame and entering the caller's stack frame, called the current stack frame. Save the caller's frame pointer so that the caller's stack can be retrieved later, and finally into the program execution, the general process will be sub 0xNh%esp to allocate the current stack size, to access the temporary variables ah, the value of the staging registers Ah, and so on. If the callee is going to call another procedure, return to the first step, and when the process ends, the stack pointer, the frame pointer is restored, often seen as follows in the disassembly: Meanwhile, the return address is restored to the PC. This is where the caller should continue to execute.

The above text can be more general, disassembly a process (function) will have the establishment (initialization), the main (execution), the end (return). Before it's easy to mix stacks and heaps (not the data structure inside), find a good article to share with everyone: stack and heap differences. It is said to have been turned countless times, the description is good. Process calls and returns are implemented in assembly language using call and RET (return), respectively. Call and RET are not very transparent, calling returns the address to the stack and jumps the PC to the starting address of the invoked process, and the RET, in turn, pops the return address from the stack and jumps to the PC.

Specifically look at the picture:

about assembly code format

The most common assembly code is ATT and Intel Assembler code format, ATT should be older, but it is the gcc,objdump default format. It is important to note that in the case of instructions with multiple operands, listing the order of operands is the opposite, so it is easy to confuse ideas. For example, to implement%ESP→%EAX, there are the following differences. #intel
MOV Eax,esp
#ATT
MOVL%esp,%eax

Because of the influence of books, I used to add "%" to the register, and I prefer the ATT format assembly code. Anti-assembly specific analysis

(Below the program stack diagram, I put the parameters into the stack I marked "parameter i=.") "That might be a little confusing if" parameter x=. "It would be better,:))

There is a simple program, regardless of what it realized what function, look down, there will definitely be a harvest. The C code given is: #include <iostream>
using namespace Std;

int fun (unsigned int x)
{
if (x = = 0)
return 0;
unsigned int nx = x>> 1;
int RV = Fun (NX);
Return (x & 0x01) +rv;
}

int main ()
{
unsigned int i = 12;
Fun (i);
return 0;

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.