Have you ever been asked? One time I was asked to look confused.

The CPU (central processing unit), also known as the microprocessor, is the heart and/or brain of a computer. In this article, let’s take a closer look at the heart of the computer to help us write computer programs efficiently.

The following is the translation:

“Tools are usually simpler than machines and are usually used by hand, while machines are usually powered by animals or steam power.”

— Charles Babbage

A computer is a machine, driven mainly by electricity, but its flexibility and programmability help achieve its simplicity as a tool.

The CPU is the heart and/or brain of the computer. Responsible for executing the instructions provided to them. Its main task is to perform arithmetic and logical operations and harmonize instructions. Before diving into the main parts of this article, let’s take a look at the main components of the CPU and what they do.

1. The two main components of the CPU

Control unit – CU

The control unit (CU) is the part of the CPU that helps coordinate the execution of instructions. It tells the CPU what to do. Its main job is to help activate the wiring that connects the CPU to various other parts of the computer, including the ALU, on command. The control unit is the first part of the CPU to receive processing instructions.

There are two types of control units:

  • Hardwired control unit.
  • Microprogrammable (microprogrammable) control units.

A hardwired control unit is a piece of hardware that needs to be changed in order to modify the way it works, while a micro programmable control unit can be programmed to change the way it works. Hardwired control units are faster at processing instructions, while microprogrammable control units are more flexible.

Arithmetic logic unit – ALU

The Arithmetic Logic Unit (ALU), as its name implies, is responsible for all arithmetic and logical operations. The arithmetic logical unit performs addition, subtraction, and so on. An arithmetic logic unit consists of a logic circuit or gate that performs these operations.

Most logic gates take two inputs and produce one output.

The following is an example of a half-adder circuit that takes two inputs and outputs the result. Here A and B are inputs, S is output, and C is carry.

2. Storage – Registers and memory

The main task of the CPU is to execute the instructions provided to it. In most cases, it needs data in order to process these instructions. Some data is intermediate data, some is input, and some is output. These data, together with instructions, are stored in the following memory:

register

A register is a group of small places where data can be stored. Registers are a combination of Latches. A latch, also known as a flip-flop, is a combination of logic gates that store 1 bit of information.

The latch has an input line, a write and input line, and an output line. We can enable write lines to make changes to stored data. When the write line is disabled, the output always remains the same.

The CPU stores output data in registers. Because they are intermediate, they are sent slowly to main memory (RAM). This data is sent to other registers connected by the bus. Registers can store instructions, output data, storage addresses, or any type of data.

Memory (RAM)

Ram-random access memory is a collection of registers that are arranged and compressed together in an optimized way to store more data. Random access memory (RAM) is volatile and the data it stores is lost when we turn off the power. Since RAM is a collection of registers used to read/write data, RAM can be used to store 8-bit address inputs, data inputs used to store actual data, and final read-write enablement codes that work as latches.

3. What is a directive

Instructions are the least granular operations a computer can perform. The CPU can handle various types of instructions

Instruction types include:

  • Instructions for arithmetic operations such as addition and subtraction
  • Logical operation instructions, such as logical AND (AND), logical OR (OR), AND logical non (NOT) operations
  • Data transfer instructions, such as data movement, input, output, loading, and storage
  • Program control instructions, such as conditional/unconditional transfer instructions (if… Goto, GOTO), rotor instructions and transfer instructions (call, return)
  • A Halt instruction that informs the CPU that the program has ended

Instructions are provided to the computer either directly in assembly language, generated by the compiler, or interpreted in some high-level language.

These instructions are hardwired inside the CPU. Arithmetic and logical operation instructions are included in the ALU (Arithmetic logic unit), while procedural control instructions are managed by the CU (control unit).

Usually a computer can execute one instruction in a clock cycle, but modern computers can execute more than one instruction at a time.

A set of instructions that a computer can execute is called an instruction set.

4. CPU clock

Clock cycle

The speed of a computer is measured by its Clock cycle. It is the number of clock cycles per second that a computer works. The single clock period is very small, about 250*10^(-12) seconds. The higher the number of clock cycles per second, the faster the processor.

The CPU clock rate is measured in the GHz (gigahertz). 1Ghz is equal to 10 ⁹Hz. One Hertz means one second. So one gigahertz means 10 ⁹ clock cycles per second.

The smaller the clock cycle, the more instructions the CPU can execute. The clock period is the reciprocal of the clock frequency, while CPU time is the number of clock cycles/clock frequency

This means that in order to improve (reduce) CPU time, we can reduce the number of clock cycles required by instructions by increasing the clock frequency, or optimizing the instructions we provide to the CPU. Some processors offer the ability to increase the clock rate, but since this is a physical change, overheating and even smoke/fire can occur.

5. How is the instruction carried out

Instructions are stored sequentially on random-access memory (RAM). For a hypothetical CPU instruction, it consists of an OP code and a memory or register address.

The control unit (CU) has two registers: an instruction register (IR) for loading the instruction opcodes, and an instruction address register for loading the address of the instruction currently being executed. There are other registers in the CPU that store the value stored in the address of the last four bits of an instruction.

Let’s take a set of instructions that add two numbers. Here are the instructions and their instructions:

Step 1 – LOAD_A 8:

The instruction is initially stored in RAM, such as the instruction <1100 1000>. Its first four bits are opcodes. This determines what this instruction is going to do. The instruction is then read into the instruction register (IR) of the control unit. The instruction is decoded to load_A, which means it needs to load data in address 1000, the last four bits of the instruction stored in register A.

Step 2 – LOAD_B 2:

Similarly, it loads the data in memory address 2 (0010) into register B of the CPU.

Step 3 – ADD B A

The next instruction is to add the two numbers. Here, the control unit (CU) tells the arithmetic logic Unit (ALU) to perform the addition operation and save the result back to register A.

Step 4 – STORE_A 23

Save the result back into register A.

This is a very simple set of instructions that add two numbers.

We have now succeeded in getting the sum of the two numbers!

The bus

All data between the CPU, registers, memory and IO devices is transferred through the bus. To save the sum of the two numbers just added to memory, the CPU puts the memory address into the address bus, puts the resulting result (and number) into the data bus, and then enables the correct signal in the control bus. In this way, the data is saved to memory with the help of the bus.

The cache

The CPU also has a mechanism for prefetching instructions into its cache. We know that a processor can complete millions of instructions in a second. This means that it takes more time to fetch instructions from memory (RAM) than it takes to execute them. So the CPU prefetches some instructions and data into its cache to speed up execution.

If the data in the cache is different from the data in operational memory, the data is marked as dirty bits.

Instruction pipeline

Modern CPU uses instruction pipeline technology to realize the parallelization of finger taking (FI), decoding (DI) and execution (EI) in instruction execution. When an instruction completes “fingering” and enters “decoding”, the next instruction can carry out “fingering”, thus improving the execution efficiency of the instruction.

However, this technique can cause problems when one instruction has a dependency on another. Thus, pipelining allows the processor to execute instructions in a different order that do not depend on each other.

Multicore computer

It basically has different cpus, but some shared resources like caches and so on.

6, performance,

The performance of a CPU depends on its execution time. Performance =1/ Execution time

Suppose a program takes 20 milliseconds to execute. CPU performance was 1/20=0.05ms. Relative performance = Execution time 1/ Execution time 2

Factors that affect CPU performance are instruction execution time and CPU clock speed (clock frequency). Therefore, to improve the performance of the program, we either increase the CLOCK speed (clock frequency) of the CPU or decrease the number of instructions in the program. Processors are limited in speed, and modern multicore computers can support millions of instructions per second. However, if we write a program with too many instructions, it will degrade overall performance.

The Big O notation calculation method can be used to determine how the CPU’s performance will be affected given the input.

A lot of optimization has been done in the CPU to maximize CPU speed. When we write any program, we need to consider how to improve the performance of the computer program by minimizing the number of instructions we provide to the CPU.

Original text: milapneupane.com.np/2019/07/06/… Author: Milap Neupane Translator: Su Benru, Editor: Tu Min Producer: CSDN

Recent hot articles recommended:

1.1,000+ Java Interview Questions and Answers (2021)

2. I finally got the IntelliJ IDEA activation code thanks to the open source project. How sweet!

3. Ali Mock is officially open source, killing all Mock tools on the market!

4.Spring Cloud 2020.0.0 is officially released, a new and disruptive version!

5. “Java Development Manual (Songshan version)” the latest release, quick download!

Feel good, don’t forget to click on + forward oh!