Author: Dao Ge, 10+ years of embedded development veteran.
Public number: [IOT Internet of Things Town], focusing on: C/C++, Linux operating system, application design, Internet of Things, single-chip microcomputer and embedded development. Reply to the official account [Books] to get classic books in the Linux and embedded fields.
Reprint: Welcome to reprint the article, and the source must be indicated.
[Linux from scratch] What is it
Over the past two years, my focus has always been on the x86 Linux system, from the driver to the middle layer to the application layer development.
With the continuous expansion of the content, I feel that many basic things have almost forgotten before, such as the following table ("In-depth understanding of the LINUX kernel" chapter
This table describes
Data segment and code segment, a closer look at the relevant books will know what these descriptors mean, but:
Why are the Base addresses of these segments
Why are Limits
Why are their Type and priority DPL different?
If not right
What s worse is that with
Such a behemoth, how to start to really learn well
Even from the Linux version 0.11, many of the codes seem to be very laborious!
When sorting out some dusty books this weekend, I found several good books I have read before: Wang Shuang's "Assembly Language", Li Zhong's "From Real Mode to Protected Mode", Ma Zhaohui's "Assembly Language Programming", etc. Wait.
They are all very-very-old books. I turned it over again, and I really feel that the content is really good!
The description of some concepts, principles, and design ideas is clear and thorough.
Ever since, I had an idea: whether these books can be combined with
After thinking about it, there are probably the following ideas:
First determine the goal of the final goal: learning the Linux operating system;
These books are written in assembly language and relatively basic low-level knowledge. We will play down the assembly language part and focus on the principle part related to the Linux operating system;
Do not output articles strictly in accordance with the content and order of the book, but put the relevant parts of the content in several books together for study and discussion;
Some content can be compared and analyzed with the relevant parts in the Linux 2.6 version, so that when you learn the Linux kernel part in the future, you can find the underlying support;
In the end, I hope I can stick to this series myself, which can be considered as a sorting out for myself.
In a word: based on basic knowledge!
As the first chapter of the opening, this article will describe the execution steps of the following picture:
Old Intel 8086 processor
So, the question is, what is a 16-bit processor?
Some people confuse the number of bits in the processor with the number of address buses!
Each address line represents one
In other words: the maximum can be located
The arithmetic unit can process up to 16 bits of data at a time;
The maximum width of the register is 16 bits;
The path between the register and the arithmetic unit is 16 bits;
In other words: in
What is main memory?
The essence of a computer is the storage and processing of data, so where does the data involved in the calculation come from? That is a physical device called storage (Storage or Memory).
Broadly speaking, any device that can store data can be called a memory, such as a hard disk, a USB flash drive, and so on.
However, inside the computer, there is a special
The memory is organized in bytes, and the smallest unit of a single access is
Each storage unit, that is, a byte, corresponds to an address, as shown in the following figure:
The address of the first byte is 0000H, the address of the second byte is 0001H, and so on.
In this memory in the figure, the address of the largest storage unit is
There is an atomic operation issue to consider.
Why do atomic operations need to limit the type of variables to
Although the smallest unit of memory is a byte, after careful design and arrangement, different bits of
In other words, with only a single visit,
What is a register?
Inside the processor, a circuit called a register must be used to latch these data.
Therefore, registers are essentially a type of memory. It's just that they are located inside the processor,
The processor is always very busy. During its operation, all data can only temporarily exist in the register for a short time, and then be sent elsewhere. This is why it is called a "register".
As mentioned earlier, these registers are
mov AX, 5D means to send 005D to the AX register (16 bits) mov AL, 5D means to send 5D to the AL register (8 bits) Copy code
When we start an application, the code and data of this program are loaded into physical memory.
Determine the address of the storage unit (address information);
Device selection, read or write command (control information);
Data read or written (data information);
In the computer, there is a dedicated connection
Logically classified, including the following
Address bus: used to determine the address of the storage unit;
control bus: CPU controls the external period;
data bus: transfer data between the CPU and memory or other devices;
In the same way, the width of the 8086 data bus is
The control bus determines
How does the CPU address the memory?
Remember the table of paragraph descriptors at the beginning of the article?
The starting addresses of the code segment and data segment descriptors are
Let's take a look again
As mentioned earlier, memory is a linear storage device,
From the internal structure point of view, if the address is simply sent from the inside to the address bus, it can only be sent
So how can we make full use of
The address adder uses the following formula to "synthesize" to get a
Physical address = segment address x 16 + offset address
For example: the program we write is placed in a memory space after being loaded into the memory.
When the CPU executes these instructions, it puts
It can be seen from the above description: 8086 CPU seems to be because the register cannot directly output
In fact, the more essential reason is: 8086 CPU just wants to address the memory through the base address + offset (the base address here means the segment address is shifted 4 bits to the left).
That is, even
Think about it: we are
The base address + offset addressing mode provides the bottom layer support for relocation.
How do we control the CPU?
Of course, a set of instruction sets need to be defined in advance. In the instruction area of the memory, all stored in the instruction area must be legal instructions, otherwise the CPU will not recognize it.
Each instruction is indicated by some specific number (instruction code)
For example: script
(Actually, it says
Another point can be said in advance: everything in the memory is data. As for which part of the data is executed as an instruction, which part of the data is regarded as a "variable" operated by the instruction, this is entirely up to the designer of the operating system. Planned.
At the 8086 processor level, any memory area pointed to by CS:IP is executed as an instruction.
As can be seen from the above description:
To put it more bluntly: we can control by changing the contents of the CS and IP registers
As a qualified embedded developer, everyone has estimated that they have configured some registers in the microcontroller to achieve some function definitions and port reuse purposes. In fact, these operations can be regarded as our control of the CPU.
If you compare the CPU to a puppet, then the register is the rope that controls the puppet.
We are getting a new one
Scan all input ports and lock them in the input image area;
Execute an operation and control logic to obtain a series of output signals, which are latched into the output image area;
Refresh the signal in the output image area to the output port;
In a brand-new PLC, the calculation and control logic required in the second step may not exist.
So, just one
in order to
When this app is downloaded to
We can simply think: Ladder diagram is used to control the running time of the PLC.
In other words: as long as the memory layout of a program is clear enough, you can play with the CPU between the palms of the hand and let it execute any code.
CPU execution instruction flow
Now we have understood address translation, memory addressing, distance
Simply put: the instruction buffer is used to cache instructions read from the memory, and the control circuit is used to coordinate the use of resources such as buses by various devices.
For the picture below, it has a total of
Take the first instruction as an example, it goes through
Send the content of CS:IP to the address adder, and calculate the 20-bit physical address 20000H;
The control circuit sends the 20-bit address to the address bus;
The instruction B8 23 01 at unit 20000H in the memory is sent to the instruction buffer via the data bus;
The value of the instruction offset register IP must be increased by 3 to point to the next offset address waiting to be executed (because the instruction code B8 represents the length of the current instruction is 3 bytes);
Execute the instructions in the instruction buffer: send the value 0123H into the register AX;
The above is the most basic step of the execution of an instruction. Of course, the instruction execution flow of modern processors is much more complicated than here.
------ End ------
High-rise buildings rise from the ground!
This article only describes
In the next article, we will continue to take a closer look at the memory segmentation mechanism.