Let's talk about stack. What is it? We have already talked about it. You won't forget it. It's a piece of memory. If you forget, I --!
I want to talk about the stack. I want to talk about some compilation knowledge. I want to know more about these dishes, yido doesn't press it down. If you will be woodworking, electrician, steel engineer, and welder, you will not do welders in the summer, for the other three, you may have to consider which one you want to give more money and which one you want to do. This means that if you are younger, you should learn more crafts.
Just introduce some of the commands we use. We don't care too much about the details. We just need to talk about its functions.
The first mov a and B is to give the value of B to A. Easy, So Easy! For example, mov eax, 10 is EAX = 0X10
The second sub a, B is to reduce the value of a B, such as SUB EAX, 10, is EAX = EAX-0X10
The third lea a and B is to give the address of B to A. For example, LEA EDI, val is to give the first address of val to EDI.
The fourth rep stos dword ptr [edi] is to write the values in EAX to the address pointed to by EDI cyclically. The value of ECX is-1, ECX = 0 ends the loop, EDI value + 4
The fifth push a is to first ESP-4, and then write A to the position pointed to by ESP, this process is called pressure stack or into the stack
The sixth pop a is to first write the value pointed to by ESP to A, and then add the ESP value to 4. This process is called stack play or stack exit.
The seventh call fun is to CALL a function. During this operation, you must first press the EIP value into the stack.
The eighth RET is the return command, which is used to play the data in the stack to the EIP.
Well, it's almost enough, so there may be a few things you don't understand, such as EAX, ECX, ESP, what does EIP mean and what is it used? They are registers, which can be assumed to be the memory of the CPU. In fact, there are many registers. We use these registers. You can click the Register button in the debugging window to bring up the Register window.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/19264B396-0.jpg "title =" 3.jpg" alt = "142854161.jpg"/>
Let's give a brief introduction: EAX is mainly used for accumulation and use, for common computing
For EBX, ESI, and EDI storage function environments, ESI and EDI are also used for string commands.
ECX is mainly used for cyclic counting.
I also forgot what it is for, generic
The EIP is important and always points to the command to be executed, which is automatically processed by the system.
ESP always points to the top of the stack
EBP base address pointer register EFL flag register
Well, let's introduce so much. I used to know these things very well. After a long time, I forgot --! You don't need to know the other registers below. It involves some other instruction sets, such as MMX, SSE, and so on. ST0-ST7 is the register used for floating point operations, we generally do not have much to do with it.
After talking about that, let's talk about the data structure of the stack. the stack is a piece of memory, and we think of them as four consecutive Memory Spaces. That is to say, if our stack is 1 kb, we can regard it as an integer space of 256 4B. Since it is a memory block, there are high and low addresses. We assume that the maximum address is 0X00131288, ESP points to here at the beginning, so what is the meaning of the stack, that is, the first ESP-4, then ESP = 0X00131284, the data to be written into the stack is then written to the four addresses 0X00131284 85 86 87, and then ESP-4 = 0X00131280 is re-written into the stack, then the data to be written into the stack will be written to the four addresses 0X00131280 81 82 83. Now, the stack is stretched from the large address to the small address, for example, pop eax writes data from the four addresses 80 81 82 83 to EAX, and then ESP + 4 = 0X00131284, well, this is the stack. You need to read this section several times, specify an ESP value at will, and then use several push pop commands for experiments.
Well, let's take a look at the function: the code is as follows:
#include <stdio.h>void swap(int a,int b){ int tem = a; a = b; b = tem;}int main(){ int a =1; int b =2; swap(a,b); return 0;}
The code is very simple. Because this section is hard to understand or cannot be expressed in words, I plan to use a video to describe it.
As follows:
This video
Now, let's talk about it. I hope it will help you !.... You must practice it. After understanding this, you will understand why our SWAP function has not exchanged values. Let's modify the code a little and see what the effect is.
#include <stdio.h>void swap(int *pa,int *pb){ int tem = *pa; *pa = *pb; *pb = tem;}int main(){ int a =1; int b =2; swap(&a,&b); return 0;}
This Code requires your own research. The process is the same as in my video. If you can understand it yourself, you will understand it. If you don't understand it, then you need to read it several times from 01 to 06 ..... --! 88
This article from the "Qian song" blog, please be sure to keep this source http://qianqianquege.blog.51cto.com/8004200/1304824