Stack and stack frame

Stack and stack frame

Stack

Stack (stack), also known as the stack or stacking , is one of the most important and most basic data structures in computer science, it (Out First In Last, according to FILO LIFO ) principle to store data.
Related concepts of stack:

  1. Top and bottom of the stack: the end that allows element insertion and deletion is called the top of the stack, and the other end is called the bottom of the stack.
  2. Pushing the stack: The insertion operation of the stack is called pushing into the stack, also known as pushing and pushing into the stack.
  3. Bouncing stack: the delete operation of the stack, also known as stack popping.

The following is a schematic diagram of the stack. It can be clearly seen from the figure that whether data is inserted or deleted is performed at the top of the stack, and there is the FILO principle. You can see that if you want to take out the value of B You must first remove the C above B. To retrieve the value of C, you must retrieve the value above C, and so on .
Insert picture description here
Technically, the stack is an area of ​​memory pointed to by a pointer in the CPU register. The "some pointer" mentioned here is usually located in the ESP register / RSP register of the x86 / x64 platform and the SP register of the ARM platform.

The most common instructions for operating the stack are PUSH (push stack) and POP (bounce stack). PUSH instruction value will ESP / RSP / SP register performs a subtraction operation, so that by subtracting 4 (32), or 8 (64), and said register operand in the write pointer points to the memory.

POP instruction PUSH instruction is the reverse operation: it reads the start pointer stack memory data for backup (usually written in the other registers), then the stack pointer value plus 4 or 8.

The following diagram illustrates the x86 platform under the pushinstructions and popcommands, instructions push Z, first ESP的值-4, the value of Z is then written to the new memory referred to in ESP; an instruction pop eax, first the Z return value in EAX register , then ESP+4.
Instruction POP EBX, first store the top element of the stack into EBX, then ESP+4 .
Insert picture description here
The following is an example. First, push eax (add the value of the EAX register to the stack). At this time, the value of eax is 0x115fcc0, the value of ESP is 0x115fc68, and the value of the top of the stack is 0x75936359.
Insert picture description here
After the push eax is executed, the result is as follows In the figure, the value of ESP at this time is 0x115fc64 (the original value of ESP is -4. Note that most stacks grow inversely, that is, grow toward low addresses). The value at the top of the stack is 0x115fcc0 (the value of EAX)
Insert picture description here
when the pop ebx instruction When the execution is completed, the value of ebx is 0x115fcc0 (popped from the top of the stack), and the value of ESP is 0x115fc68 (the value of ESP in the previous step +4). At this time, the previous data 0x115fcc0 is still in memory (the address is 0x115fc64) Place), but this value is no longer part of the stack, because ESP points to the top of the stack.

The role of the stack in the process is as follows:

  • Temporarily save local variables within the function.
  • Pass parameters when calling the function.
  • Save the address returned by the function.

Stack frame

The stack frame is also called the process activity record, and is a data structure used by the compiler to implement procedure / function calls. In short, stack frame is a means to use EBP (frame pointer, please note that it is not ESP) register to access local variables, parameters, function return address, etc.

;栈帧结构
PUSH EBP			;函数开始(使用EBP前先把已有值保存到栈中)
MOV EBP, ESP		;保存当前ESP到EBP...					;函数体
					;无论ESP值如何变化,EBP都保持不变,可以安全访问函数的局部变量、参数
					
MOV ESP, EBP		;将函数的起始地址返回到ESP中
POP EBP				;函数返回前弹出保存在栈中的值
RETN				;函数终止

Each function call maintains an independent stack frame on the call stack. Each independent stack frame generally includes:

  • Function return address and parameters
  • Temporary variables: including non-static local variables of functions and other temporary variables automatically generated by the compiler
  • Function call context

A stack extending from higher to lower addresses , stack frame with a function of both ESP and EBP registers scoping. EBP points to the bottom of the current stack frame, and ESP always points to the top of the stack frame.

The EBP register is also called the frame pointer (Frame Pointer)

The ESP register is also called the stack pointer (Stack Pointer)

A very common example of activity records is shown in the figure.
Insert picture description here
From: https://blog.csdn.net/Casuall/article/details/99284422

Published 8 original articles · Likes4 · Visits 290

Guess you like

Origin blog.csdn.net/qq_45521281/article/details/105365121