How Does A CPU Run Code in Computer?

How Does A CPU Run Code in Computer?

The CPU is the "brain" of the computer, responsible for executing instructions in the programs, processing data and performing various tasks. So the running process of the program is to execute each instruction step by step, and instructions refer to the operating commands that control the computer program through specific codes. And one question, how does the CPU run the code?


von Neumann Architecture

According to the definition of the von Neumann model, the simplest basic structure of a computer consists of five parts: controller, arithmetic logic unit, memory, input, and output. In modern computers, the controller and arithmetic logic unit are embedded in the CPU, while memory usually refers to the main memory, and input and output usually refer to the keyboard and display. So you can probably say that the arithmetic unit and controller are integrated together and collectively referred to as the Central Processing Unit (CPU).


CPU in Computer

Functions of CPU

1. Program control

The CPU controls the execution order of the program by executing instructions, which is really important.

2. Operation control

The realization of an instruction function requires several operation signals to complete. The CPU generates an operation signal for each instruction and sends it to different components to control the corresponding components to operate according to the functional requirements of the instruction.

3. Time control

The CPU controls various operations in time, which is time control. The CPU must strictly control the entire execution process of each instruction, that is, the appearance time, duration and time sequence of the operation signal.

4. Data processing

The CPU processes the data by performing arithmetic operations and logical operations, and the results of data processing are used by people. Therefore, data processing is also the most fundamental task of the CPU.


What Makes Up the CPU?

The CPU is usually composed of a control unit, a arithmetic logic unit, and registers. From a software perspective, the CPU is a logic machine that executes various computer instructions codes. So what is it inside? How does it execute instructions? Logically, we can think that the CPU is actually composed of a bunch of registers. If we correspond to the hardware circuit, the CPU is a large and complex circuit, and the register is a simple circuit composed of multiple Flip-Flops or latches Latches.

CPU Registers

• Control unit: responsible for controlling the CPU according to instructions, such as reading data from memory to registers, adding two data in registers, etc.

• Arithmetic logic unit: responsible for calculation.

• Registers: including general registers, program counters, and instruction registers.

• General registers: used to store data to be calculated.

• Program counter: used to store the memory address of the next instruction to be executed.

• Instruction register: used to store the instruction being executed.


Three Points You Should Know

1. Why does the CPU need registers when it has memory?

Because the speed at which the CPU reads data from memory is very different from the speed at which it reads data directly from registers. This is a hierarchical design based on the trade-offs between efficiency and cost.

2. What role does memory play?

When a program is running, the "program itself" and "program data" in the computer are stored in the memory, which is a continuous linear area. A basic memory storage unit is 1 byte (8 bits), and each byte corresponds to a memory address.

3. What role does the bus play?

The bus connects the CPU, memory, and other devices and is responsible for the communication between them. The bus can be divided into three types: address bus, data bus, and control bus.

• Address bus, used to specify the memory address that the CPU will operate.

• Data bus, used to read and write data in the memory.

• Control bus, used to send and receive signals, such as interrupts, device resets, etc. The CPU will naturally respond after receiving the signal, and the control bus is also needed at this time.

When the CPU wants to read or write memory data, it first specifies the address of the memory through the address bus, then controls the read or write command through the control bus, and finally transmits the data through the data bus.


CPU Bit Size

How is data transmitted through the bit line? What impact does the bit size have on efficiency?

• In fact, it is through the operation voltage, low voltage represents 0, and high voltage represents 1.

• If a signal like "high-low-high" is constructed, the corresponding binary data is "101", and the decimal represents "5".

• If there is only one line, the binary data "101" needs to be transmitted 3 times, which is usually unacceptable efficiency.

So what can we do? Just use multiple lines to transmit at the same time.

• The CPU [data bus] with 32 bits wide is usually 32 lines, which means that 4 bytes of data can be read from the memory at a time.

• The CPU [address bus] with 32 bits wide is usually 32 or 36 lines, which means that the maximum memory addressing capacity of the CPU is 2^32=4G or 2^40.

CPUs are usually 32-bit or 64-bit, what is the difference between them?

• The bit here refers to the "bit width", that is, the amount of data that the CPU can calculate in one operation.

Taking a 32-bit CPU as an example, it can calculate 4 bytes at a time, and the maximum integer that can be calculated = 2^32-1= 429496729. But this does not mean that a 32-bit computer can only perform calculations less than this number. For example, when using a 32-bit CPU to add two 64-bit numbers, the CPU will first split the original number into two high-order and low-order numbers, add the low-order numbers first, calculate the carry, and then add the high-order numbers, plus the carry of the low-order numbers, and finally splice them together to get the result.


Basic Process of Program Execution

A program is actually a series of instructions, so the running process of the program is to execute each instruction step by step, and the CPU is responsible for executing the instructions.

So how does the CPU execute the program? The process of CPU executing the program is as follows:

Step 1

• The CPU reads the value of the [Program Counter], which is the memory address of the next instruction;

• The CPU's [Control Unit] operates the [Address Bus] to specify the memory to be accessed, and notifies the memory device to prepare the data;

• After the data is ready, the instruction data is transmitted to the CPU through the [Data Bus];

• After the CPU receives the data from the memory, it stores the instruction data in the [Instruction Register].

Step 2

• The value of the [Program Counter] is incremented, indicating that it points to the next instruction. The size of this increment is determined by the bit width of the CPU. For example, for a 32-bit CPU, the instruction is 4 bytes and requires 4 memory addresses to store, so the value of the "Program Counter" will be incremented by 4.

Step 3

• The CPU analyzes the instruction in the [instruction register] to determine the type and parameters of the instruction;

• If it is a calculation type instruction, it will be handed over to the [logic operation unit] for operation;

• If it is a storage type instruction, it will be handed over to the [control unit] for execution.

Basic Process of Program Execution
Basic Process of Program Execution

To summarize, when a program is executed, the CPU reads the instruction to be executed from the memory into the instruction register according to the memory address in the program counter, and then starts to read the next instruction in sequence according to the instruction length. Moreover, the CPU reads the instruction from the program counter, executes it, and then goes to the next instruction. This process will continue to repeat until the program is executed. This continuous cycle is called the CPU instruction cycle.


Five Basic Instructions to Execute

Five Basic Instructions to Execute
Five Basic Instructions to Execute

MOV Instruction

Instruction fetching:

The program counter PC is loaded with the address 101 of the first instruction, and the content of PC is placed on the instruction address bus, the instruction is decoded and the read command is started. The MOV instruction is read from address 101 and loaded into the instruction register IR through the instruction bus IBus. The content of the program counter PC is increased by 1 and becomes 102, preparing for the next instruction. The opcode in the instruction register IR is decoded, and the CPU recognizes it as a MOV instruction, and the instruction fetch stage is completed.

Instruction Execution:

The operation controller OC sends a control signal to the general register, selects R1 (10) as the source register, and RO (00) as the target register. OC sends a control signal to the ALU, specifies the ALU to perform a transfer operation, opens the ALU output tri-state gate, and sends the ALU output (10) to the data bus DBus. There can only be one data on DBus at any time. The data on DBus is written into the data buffer register DR, and the data in DR is written into the target register RO. The content of RO changes from 00 to 10. At this point, the MOV instruction is executed.


LAD Instruction

Instruction Fetching:

The instruction fetching of the LAD instruction is exactly the same as that of the MOV instruction.

Instruction Execution:

OC issues a control command, opens the IR output tri-state gate, puts the direct address code 6 in the instruction on the data bus DBus, loads it into the address register AR, reads the number 100 in the number storage unit 6 to DBus, and loads it into the buffer register DR. The number 100 in DR is loaded into the general register R1, and the original value 10 in R1 is overwritten. At this point, the LAD instruction is executed.


ADD Instruction

Instruction Fetching:

The instruction fetching of the ADD instruction is the same as other instructions.

Instruction Execution:

The operation controller OC sends a control signal to the general register, selects R1 (100) as the source register, and R2 (20) as the target register. The ALU performs the addition operation on R1 and R2, opens the ALU output tri-state gate, puts the operation result 120 on the data bus DBus, and then enters the buffer register DR. The carry signal generated by the ALU is saved in the status word register PSW, and the value 120 in DR is loaded into R2, and the original number 20 in R2 is overwritten. At this point, the execution of the ADD instruction ends.


STO Instruction

Instruction Fetching:

The instruction fetching of the STO instruction is also the same as other instructions.

Instruction Execution:

The operation controller OC sends a control signal to the general register, and selects R3 (30) as the address of the data storage. The general register output tri-state gate is turned on, and the address 30 is placed on DBus and loaded into the address register AR, and the address is decoded. The operation controller OC sends a control signal to the general register, and selects R2 (120) as the write data of the data storage and puts it on DBus. The value 120 is written into the data storage unit 30, and the original data 40 is flushed. At this point, the execution of the STO instruction ends.

JMP Instruction

Instruction Fetching:

The instruction fetching cycle of JMP is the same as other instructions.

Instruction Execution:

OC issues a control command, opens the IR output tri-state gate, sends the address code 101 in IR to DBus, and enters the address code 101 on DBus into the program counter PC, and the original address 106 in PC is replaced. Therefore, the next instruction is not fetched from unit 106, but transferred to unit 101. At this point, the JMP instruction execution cycle ends.


Conclusion

Computer instructions are the language that the CPU can understand, also known as machine language. Different CPUs support different instruction sets, corresponding to various assembly languages and machine codes. The MIPS (Microprocessor without Interlocked Pipelined Stages) instruction set is a commonly used instruction set.

The process of executing instructions by the CPU includes decoding and executing the instructions. The CPU consists of a control unit, an arithmetic logic unit, and a data unit, which work together to execute instructions. Where the control unit is responsible for decoding instructions and generating control signals, the arithmetic logic unit handles computational operations, and the data unit is used to store data and computation results.

Data transfer between the CPU and main memory is carried out through the address bus and data bus. The address bus determines the range of memory locations that the CPU can address, while the data bus specifies the amount of data that can be transferred at a time.

To view or add a comment, sign in

More articles by KUKE ELECTRONICS LIMITED

Explore content categories