The CPU (Central Processing Unit) is the mind of a computer and the main connection between the software and hardware. Allows software to run on hardware.
But how does it work in depth? And how do you connect programs to certain computer hardware?
This article aims to help you understand this connection by explaining in depth how a processor works. This topic is generally only familiar to those with an academic background in computer hardware design.
Often, many computer science graduates have never taken an advanced digital logic course. Therefore, even the most experienced programmers may not understand how a processor actually processes information.
Although we will not design logic gates from transistors or processor components from logic gates, we will cover key concepts necessary to understand how a processor processes data created by a program written in a programming language.
Analogy: Introduction to What Makes CPUs Work
To fully understand how a computer works, let’s imagine a city as a real-life setting. We will compare the computer elements with certain parts of this city.
This way, you will have a clearer vision of the different components of the processor and their importance. Next, we will look at each of the elements in depth.
Memory Hubs: Understanding RAM and ROM
RAM (random access memory) is like an urban public library: it stores books and information that people can borrow and return as needed.
In a computer, RAM loads data and instructions from the computer’s memory that the processor needs to process the data.
The ROM (read-only memory) is like a city historical archive: it only stores documents that will never change and will never be loaned to the public.
Data Paths – Navigating the CPU Data Path
The CPU data path corresponds to the city’s road network. CPU buses and data route registers make up the city’s road network.
Just as roads facilitate the movement of cars and people, the processor’s data path ensures that data flows efficiently through the processor.
Traffic controllers: the role of state machines in processors
State machines act as traffic control systems.
The traffic control system manages the flow of vehicles, and the state machines manage the flow of data according to the instructions provided by the CPU.
Daily routines: explanation of the recovery-performance cycle
The research and execution cycle constitutes the daily movement of the city’s inhabitants.
Every day, people decide where they are going, arrive, complete their tasks, and return home. This process always repeats itself.
Likewise, the processor retrieves instructions, decodes them, and executes them in a repetitive cycle.
The Rulebook: Decoding Instruction Set Architecture (ISA)
The architecture of the instruction set is like the law of urban transportation.
The Urban Transportation Law establishes what is legal to do in the city when it comes to transporting people.
Instruction set architecture is the set of rules and instructions that the processor can execute.
The Memory Hubs: Understanding RAM and ROM
RAM stands for chance access memory and can be used to read and write data.
The processor first transfers data from the computer’s memory to RAM to avoid long waiting times.
It then uses the data in RAM to complete the instructions.
They are used in computers and many electronic devices due to the volatility of memory. This means that the data is only there when the computer is on, making it ideal for temporary storage while the device is running.
ROM stands for read-only memory. Data was only added during the computer’s manufacturing.
They are widely used in device firmware, BIOS, and small embedded systems.
In fact, ROM is a non-volatile memory. This means that it remains in memory when the device is turned off, making it very important for permanent data storage.
The Roadways of Data: Navigating the CPU Data Path
The processor data path is a complex digital circuit made up of many components that work together, such as:
Arithmetic Logic Unit (ALU): Performs arithmetic and logical operations on the data portion of the processor.
Registers: Small, fast storage locations for temporary data retrieved from RAM.
Bus: Data, control, and address buses are cables used in the processor’s data path to transfer information.
Although processors have changed a lot since the Intel 8008, these are some of the components that still serve as the basis for all processors.
Thanks to them, it is possible to let the data flow but not control the actual flow. This is the job of the processor control unit, created in the Intel 8008 as state machines.
Traffic Controllers: The Role of State Machines in CPUs
A state machine is a system that goes from one state to another to perform tasks.
They are made up of a series of states and transitions. They were used in Intel 8008 to create the control unit due to its structure and efficient way of handling the sequence of operations necessary to process instructions.
Each of the states can activate one or more processor components to process a given assembly instruction.
In this way, certain parts of the CPU’s data path are activated for an instruction to be executed.
Furthermore, through these state machines, the CPU is complete and can execute all the instructions desired by the user in a continuous loop called the fetch-execute cycle.
Daily Routines: The Fetch-Execute Cycle Explained
The processor state machine controls how the processor’s data path works together to execute a given instruction.
Today, each computer receives millions of instructions per second. In this way, state machines act as a loop to fetch instructions and execute them.
This process is known as the fetch-execute cycle, in which the processor fetches and executes the instructions:
Fetch: The processor fetches the instruction from memory.
Decode: The retrieved instruction is decoded to determine the required action.
Execute: The decoded instruction is executed using the appropriate CPU components.
Rewrite: The result of the execution is written back to memory or a register.
During the recovery stage, the control unit asks the RAM to give the next instruction to the CPU.
In the decoding phase, the CPU interprets the instruction, and in the execution phase, it operates. Then, the writeback step ensures that the result is stored correctly.
This cycle continues as long as the PC is turned on. In this way, modern processors process billions of instructions per second.
But What About Data from the Keyboard or Mouse?
This data does not come from RAM but is processed through a mechanism called interrupts. When the processor executes instructions, it can detect when data is coming from devices.
If this happens, the CPU stops its current task and gives priority to peripheral instructions. The CPU then resumes its previous tasks.
There are many ways to handle interruptions. The most common are:
Polled Interrupts: The processor periodically checks to see if an interrupt has occurred.
Vector Interrupts: The interrupt device directs the processor to the appropriate interrupt service routine.
Priority Interrupts: Interrupts are assigned different priority levels, ensuring that critical tasks are handled first.
In this way, thanks to these mechanisms, the CPU maintains its performance while interacting with the peripherals.
The Rulebook: Decoding the Instruction Set Architecture (ISA)
With the control unit, full CPU, and RAM, it is possible to execute many instructions.
But what instructions can be executed on a given CPU? And how much? This is what the Instruction Set Architecture (ISA) solves.
The ISA defines a set of instructions that a given processor can execute. This is what allows programmers to understand what a processor can and cannot do without having to understand all the digital logic hardware it contains.
In this way, it acts as an interface between the software and the hardware.
Key aspects of the ISA:
Types of instructions: These include arithmetic, logic, control, and data transfer instructions.
She was addressing modes: methods for specifying instruction operands.
Registers: A set of registers available for use by instructions.
Common ISAs:
x86: Widely used in desktop processors and servers.
ARM: Dominant in mobile and embedded devices due to its energy efficiency.
RISC-V: An open ISA standard designed for a wide range of applications.
Each processor usually has its version of the instruction set architecture. The instruction set architecture is often defined with assembly programming languages.
Each processor usually has its version of the instruction set architecture. The instruction set architecture is often defined with assembly programming languages.
That is why there are so many versions of the assembly programming language.
Since each processor has its hardware specifications, each will have components similar to other processors and, therefore, similar assembly programming languages associated with them.
The choice of ISA affects the design, performance, and compatibility of the processor with the software.
For example, the complexity of x86 enables powerful desktop applications, while the simplicity of ARM favors low-power mobile devices.
From Programming Languages to Machine Code
Although each processor has its assembly language, managing assembly code and writing assembly code to create large programs can be complex.
This is very complicated and can lead to wasting time fixing things and details instead of, in a faster and easier way, managing the development of a program and actually developing it.
To solve this problem, many assembly-based programming languages have been created. We write the code in programming languages , and then it is converted to assembly language.
This way, instead of spending time on details, it is possible to focus on more important things like system development and algorithm design.
This is the process by which most programming languages convert their code to assembly:
Convert the code to assembly code using a compiler or interpreter.
The assembly code is then converted to raw machine code and executed by the processor.
A specific loop is completed in the CPU state machine.
Then, the CPU recovers and executes the next instruction.
C Programming Language
Assembler created the C programming language in the early 1970s. It was designed to provide a higher-level language for efficient system-level programming, also allowing hardware manipulation.
With a compiler, C code is converted to assembly and then processed by the entire CPU.
Thanks to this conversion, when writing programs in the C programming language, we can solve many problems more efficiently, such as:
- Memory management errors
- Buffer overflows
- Manual optimization problems
Nowadays, even for simpler tasks, assembly code converted from the C compiler is much more efficient and reliable than assembly code written by a human.
Python Programming Language
The Python programming language was created from C in the late 1980s.
Its goal was to provide a high-level, easy-to-use programming language through an emphasis on readability and simplicity, allowing rapid application development.
In Python, an interpreter converts Python code to bytecode line by line.
This bytecode is converted to machine code in the CPU and processed in the fetch-execute cycle.
This way, people can program more easily and focus on more important programs, such as:
- Artificial intelligence models
- Web applications
- Data analysis
- Scientific calculation
However, the challenge with processors in all programming languages is that they process data sequentially.
City Challenges: Addressing CPU Problems
The traditional single-core processor processes data sequentially, instruction by instruction. This becomes a limitation if we have many instructions to process.
This is what GPUs (Graphics Processing Units) came to correct. Thanks to GPUs, we can process instructions in parallel, thus significantly reducing calculation time.
With these parallel processing capabilities, much faster computing and greater efficiency can be achieved in a wide range of applications.
Conclusion: Better Control Units and Data Parts
In addition to the fact that modern processors are multi-core, advances in control units and data paths play a vital role in improving processor performance.
Control units are usually designed using firmware or hardwired control units.
Microprogramming provides greater flexibility and easier updates to control logic, while hardwired control units provide faster performance by directly implementing control signals.
Another significant advance is the exploration of new materials for logic gate transistors.
Instead of relying solely on silicon, researchers are studying alternative materials to create faster, more efficient processors.
As technology continues to advance, understanding these fundamental concepts will continue to be essential for both enthusiasts and professionals in the field.
Staying abreast of these developments ensures continuous innovation and improvement in processor design and functionality.