How does a program run in the CPU (I)

Publisher:trendsetter10Latest update time:2022-04-27 Source: eefocusKeywords:Program Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The difference between CPU and MCU

When it comes to embedded development, the concept we come into contact with in daily life is MCU. The difference between MCU and CPU is that MCU integrates on-chip peripheral devices, while CPU does not. A simple example is that MCU integrates Flash and RAM in the chip to store programs and data. The corresponding embodiment in our personal PC is the hard disk and memory stick. Therefore, the difference between the two is only whether the peripheral devices are integrated or not. The most essential working principle of the two is interoperable.


Components of a CPU

The CPU is an electronic component composed of many transistors. From a functional point of view, the CPU consists of four parts: registers, controllers, arithmetic units, and clocks. Their respective functions are as follows:


Register: can be used to temporarily store instructions, data and other processing objects.

Controller: responsible for reading instructions from memory into registers and controlling the entire system based on the execution results of the instructions.

Arithmetic unit: The arithmetic unit is responsible for calculating the data sent to the arithmetic unit.

Clock: Responsible for sending the clock signal that starts the CPU timing. The CPU can only work under the drive of the clock signal.

How programs are executed by the CPU

When we are developing embedded software, a common language is C language, which is also called high-level language. Before the high-level language we write actually enters the CPU for execution, there are still many steps to complete. A rough division is that it needs to go through three steps: compile -> link -> generate executable files. A more detailed division is the process shown in the figure below:

Compile and link process

According to the above flowchart, we can know that before the program enters the CPU for execution, it will get an executable program, and this executable file contains a collection of instructions and data. So the program we write is essentially in the form of instructions + data. Since there are instructions and data that can be recognized by the CPU, the written program can also run in the CPU.


CPU workflow

Before explaining in detail how the CPU works, let's first look at a CPU workflow diagram, as shown below:


insert image description here

In the picture shown above, we can see the following information: instruction memory and data memory outside the dotted box, instruction register, control unit, register group, logic operation unit and status register inside the dotted box. Next, we will explain how each of them operates and how they coordinate with each other to complete a program.


Logical operation unit and register group

Let's start with the logical operation unit. As the name suggests, its function is to perform operations. Since it is an operation, it requires operands and instructions, as shown in the following figure:

Logic Operation Unit

The OP shown in the above figure represents the operation to be performed by the instruction. It can also be seen from the figure that the operands of the logic operation unit come from the register group. Of course, the operands of the logic operation unit do not always come from the register group. Only when performing simple operations will the operands be obtained from the register group for operation. After the operation, there must be a result, so the logic operation unit will output two data, one is the result after the operation, and the other is the flag bit. The flag bit here represents whether the result of the operation after the two operands are operated is 0, whether there is a carry, whether it is a negative number, or whether there is an overflow.

On the other hand, in addition to providing operands to the logic operation unit, the register group also serves as a storage function for the operation results. The schematic diagram is as follows:

insert image description here

That is to say, in addition to providing operands to the logic operation unit, the register group also stores the operation results of the logic operation unit.


Logic unit and data memory

As mentioned above, when performing simple operations, the logic operation unit will obtain operands from the register group. What should we do when performing complex operations? At this time, the data memory will play its role. The schematic diagram is as follows:

insert image description here

From the above picture, we can see that the operands of the logic operation unit come from the data memory and the register group. When fetching operands from the data memory, it should be noted that the data memory is not part of the CPU, so the speed of accessing the memory is much slower than the speed of accessing the register.

When explaining the register group, we said that the register group can provide operands and can also store the operation results of the logic operation unit. The same is true for the data memory, which can also store the operation results of the logic operation unit. The schematic diagram is as follows:

insert image description here

The register group and the data memory can both store the operation results of the logic operation unit, so what is the difference between the two? The most popular explanation I have heard is: storing the operation results of the logic operation unit in the register group is like when we do math fill-in-the-blank questions in an exam, we will get some intermediate results, that is, these results are not to be filled in the answer on the test paper, but as the conditions for the next step of operation, then we first record this result on the draft paper, this draft paper is equivalent to our register group, when we calculate the final answer, then fill this answer on our test paper, the test paper is equivalent to our data memory here. So, in a word, that is, the register group is used to store intermediate results, and the data memory is used to store the final result.


control unit

We said above that the operand source of the logic operation unit may be the register group or the data memory, and the storage location of the operation result may be the register group or the data memory. So when the actual program is running, which method should be used? At this time, the control unit is needed to coordinate. The schematic diagram is as follows:

insert image description here

From the above picture, we can see that the lines from the control unit downwards point to each switch. The empty circle here represents a switch node, which means where the controller controls the switch to close, thereby determining whether to obtain the operand from the register or the data memory.

Looking at the part above the control unit in the figure, we can see that the data source of the data control unit is the instruction register, so the control unit receives instructions. Then we can summarize the functions of the controller as follows:


Instruction parsing: Analyze what operations the instruction needs to perform

Determine the data flow: determine the source of the operands used by the instruction and the destination of the results

Get Instructions

As mentioned above, the control unit obtains instructions from the instruction register. So where do the instructions in the instruction register come from? Let's go back to the diagram we gave at the beginning:

insert image description here

From the figure, we can see that instructions are obtained from the instruction memory. At the same time, if we want the program to run correctly, we need to make the instructions run in order, rather than sending them to the instruction register randomly. So how can the CPU ensure that the instructions can be run in order? Here we need to use the CPU's PC pointer register. The function of the PC pointer register is to store the address of the next instruction to be executed. After the address is taken out, the PC pointer register will be updated to point to the next instruction to be executed. Therefore, the existence of the PC pointer register ensures the orderly execution of instructions.


Summarize

Above, we have separately explained the components involved in the work of the CPU. Now we will integrate them. The CPU's workflow is to store the address of the next instruction to be executed in the PC pointer register, so that the instructions in the instruction memory can enter the instruction register in order. Then, the control unit obtains the corresponding instruction from the instruction register, parses the instruction, determines the operation to be performed by the current instruction, and controls the source of the logic operation unit operand and the storage location of the logic operation unit operation result according to the content of the instruction analysis, thereby completing the operation required by the instruction to be performed by the CPU.

Keywords:Program Reference address:How does a program run in the CPU (I)

Previous article:How does a program run in the CPU (Part 2)
Next article:Summary of Assembly Language Phase 1

Recommended ReadingLatest update time:2024-11-22 21:15

Design of SMS Communication Terminal System Based on Embedded Wireless CPU
1 Introduction   At present, the multi-microcomputer system composed of single-chip microcomputer and PC through serial interface has been widely used in industrial control, environmental monitoring and other occasions. Most of these systems use RS-232, RS-485 or wired modem communication methods. Although they ar
[Microcontroller]
Design of SMS Communication Terminal System Based on Embedded Wireless CPU
The parent company of Russian CPU manufacturer Baikal is on the verge of bankruptcy: it will auction patents and is expected to transform the AI ​​field
According to news on August 30, foreign media CNews reported that the parent company of Russian CPU manufacturer Baikal is on the verge of bankruptcy and is currently preparing to auction its assets. Baikal is part of T-Platforms, a Russian company specializing in exascale computing. According to reports, T-Platforms
[Semiconductor design/manufacturing]
The parent company of Russian CPU manufacturer Baikal is on the verge of bankruptcy: it will auction patents and is expected to transform the AI ​​field
Realization of Data Communication between Dual CPUs
1 Introduction A digital signal processor (DSP) is a microprocessor suitable for implementing various digital signal processing operations. It has the following main structural features: (1) It adopts an improved Harvard structure with independent program bus and data bus, which can access instruction and data space
[Microcontroller]
Realization of Data Communication between Dual CPUs
Time error analysis of 8051 microcontroller timer overflow interrupt and CPU response interrupt
1 Preface There are generally several timers inside the microcontroller. For example, the 8051 microcontroller has timer 0 and timer 1 inside. When the timer count overflows, an interrupt request is issued to the CPU. When the CPU is executing an instruction or an interrupt service routine, it often delays responding
[Microcontroller]
Domestic independent architecture Loongson CPU supports open "Hongmeng"
Loongson Technology and Runhe Software jointly announced that after joint efforts, the Loongson 2K0500 development board and OpenHarmony operating system have completed adaptation verification, and the LoongArch dragon architecture platform has formed preliminary support for OpenHarmony. The ecosystem of the Internet
[Embedded]
Domestic independent architecture Loongson CPU supports open
High-speed real-time control system composed of DSP and 80C196 dual CPU
DSP is also called digital signal processor. TMS320C31 is the third DSP chip of TI. Its basic structure includes: (1) bus structure that separates program memory from data space, which can address and access the two independent memories, program memory and data memory, independently; (2) pipeline structure. Taking t
[Embedded]
MediaTek launches Dimensity 9300+, with more powerful AI features and higher-frequency CPU
The new flagship smartphone SoC uses MediaTek’s “all-big-core” architecture, taking a non-traditional approach to on-device generative AI and gaming. MediaTek has unveiled the Dimensity 9300+ SoC, the company’s latest processor for high-end mobile applications. With today’s consumer demand for mobile gami
[Mobile phone portable]
MediaTek launches Dimensity 9300+, with more powerful AI features and higher-frequency CPU
How to access CPU registers from a driver?
I was porting 2440 and happened to look at the 2.6.32 code. I found the following structure definition in mach-smdk2440.c of smdk2440: static struct map_desc smdk2440_iodesc __initdata = { /* ISA IO Space map (memory space selected by A24) */ { .virtual = (u32)S3C24XX_VA_ISA_WORD, .pfn
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号