The mechanism by which programs store data

The mechanism by which programs store data

The running of the program depends on the CPU, and we know that the CPU is only responsible for computing, and does not have intelligence itself. Every step of the execution of the CPU requires us to tell it what to do, enter an instruction, it runs once, then stops and waits for the next instruction. These instructions are all binary and are called opcodes. For humans, binary programs are unreadable, and it is impossible to see what the machine is doing. But we all know that we write code and write it directly in binary. Now most people write programs in high-level programming languages. These high-level programming languages ​​can be translated into operations through the action of a compiler or interpreter. code.
Let's look at registers first. The CPU itself is only responsible for computing, not for storing data. The data is generally stored in the memory, and the CPU reads and writes the data in the memory when it is used. However, the operation speed of the CPU is much higher than the read and write speed of the memory. In order to avoid being slowed down, the CPU has its own cache. Basically, the CPU cache can be thought of as memory with faster read and write speeds. However, the CPU cache is still not fast enough. In addition, the address of the data in the cache is not fixed, and the CPU needs to address every read and write, which will slow down the speed. Therefore, in addition to the cache, the CPU also has its own registers to store the most commonly used data. That is to say, the most frequently read and written data (such as loop variables) will be placed in the registers, and the CPU will first read and write the registers, and then the registers will exchange data with the memory. Registers do not rely on addresses to distinguish data, but on names. Each register has its own name. We tell the CPU which register to go to to get the data, which is the fastest.
The early CPU had only 8 registers, and each had a different purpose, but now there are more than 100 registers, and all of them have become general-purpose registers without special purpose. We often see names such as 32-bit CPU and 64-bit CPU, which actually refer to the size of the register. Registers can only store a small amount of data. Most of the time, the CPU needs to direct the registers to exchange data directly with the memory. Therefore, in addition to registers, it is also necessary to understand how memory stores data.
There are two models of memory, Heap and Stack. When a program runs, the operating system allocates a section of memory to store the program and data generated by the operation. This memory has a start address and an end address, the start address is the smaller address, and the end address is the larger address. During the running of the program, for dynamic memory occupation requests (such as creating new objects or using the malloc command), the system will draw a part of the pre-allocated memory to the user. This memory area divided by the user's active application is called the heap (Heap), which grows from the starting low-order address to the high-order address. An important feature of the heap is that it does not disappear automatically, but needs to be manually released by the user or recycled by the garbage collection mechanism.
Another type of memory occupation is called the stack (Stack). In short, the stack is the temporarily occupied memory area due to the operation of the function. When the main function main runs, the system will create a frame in the memory, and all the internal variables in the main function will be stored in this frame. Take up more space. If the function is called in the main function, when the program reads this line, the system will create another frame to store the internal variables in the function, so in general, there will be as many frames as there are layers in the call stack. When the calling function ends, its frame will be recycled, and the system will return to the point where the calling function was interrupted to continue execution. Through this mechanism, the function is called layer by layer, and each layer can use its own local variables. All frames are stored in the stack, and they are superimposed layer by layer. After each function ends, a frame is released, and when all functions are executed, the entire stack is released. The stack starts from the end address of the memory area and is allocated from the high-order address to the low-order address.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326659144&siteId=291194637