Assembly language (7)-stack

0. What is a stack?

Stack data structure (stack data structure) of the same principles and the plate stack: add a new value to the top of the stack, remove the top of the stack value is also removed. The stack is also called the LIFO structure (Last-In First-Out), the reason is that the last value entered into the stack is also the first value out of the stack. (The data structure will be discussed)

1. Stack operation principle

The runtime stack is a memory array, and the CPU directly manages it with the ESP (extended stack pointer) register, which is called the stack pointer register. In 32-bit mode, the ESP register stores the 32-bit offset of a certain position on the stack. ESP is basically not directly controlled by the programmer. On the contrary, it is modified indirectly by instructions such as CALL, RET, PUSH, and POP. ESP always points to the last value added or pushed to the top of the stack. The runtime stack works at the system level and handles subroutine calls. Stack ADT is a programming structure used to implement algorithms based on last-in first-out operations.

2. Stack instructions

(1) PUSH instruction

The PUSH instruction first reduces the value of ESP, and then copies the source operand to the stack.

(2) POP instruction

The POP instruction first copies the contents of the stack element pointed to by ESP to a 16-bit or 32-bit destination operand, and then increases the value of ESP.

(3) PUSHFD and POPFD instructions

The PUSHFD instruction pushes the contents of the 32-bit EFLAGS register onto the stack, while the POPFD instruction pops the contents of the top cell of the stack to the EFLAGS register. The MOV instruction cannot be used to copy the contents of the flag register to a variable. Therefore, PUSHFD may be the best way to save the flag bit. Sometimes it is very useful to save a copy of the flag register so that the original value of the flag register can be restored later.

(4) PUSHAD, PUSHA, POPAD and POPA

The PUSHAD instruction pushes all 32-bit general-purpose registers onto the stack in the order of EAX, ECX, EDX, EBX, ESP (value before PUSHAD), EBP, ESI, and EDI.
 

The POPAD instruction pops the same register from the stack in reverse order. Similarly, the PUSHA instruction sequentially (AX, CX, DX, BX, SP, BP, SI, and DI) pushes 16-bit general-purpose registers onto the stack.
 

The POPA instruction pops the same register from the stack in reverse order. In 16-bit mode, only PUSHA and POPA instructions can be used.

 

Guess you like

Origin blog.csdn.net/qq_35789421/article/details/113738602