How a CPU Works?

The central processing unit or CPU is like the engine in your car or brain in your skull. It is like a fancy calculator used to run the applications on your computer.

When you write software in a language like JavaScript or python you’re actually writing a set of instructions that will be executed as machine code by a CPU, which itself is a carefully crafted piece of metal and silicon that contains billions of tiny transistors or on/off switches that represent ones and zeros.

To perform mathematical calculations a CPU will combine multiple transistors together to form logic gates. For example, AND will take two binary inputs and validate that both are true to produce a true output.

It only takes a few basic logic gates to solve highly complex computational problems. Modern chips contain billions of transistors and they can be flipped on and off billions of times per second.

The state of the CPU is synchronized by an oscillator known as the Clock Generator. In general the more times the clock can pulse per second the faster the CPU can compute and is normally measured in gigahertz.

Gamers will sometimes overclock their CPUs to gain more performance at the cost of higher temperatures and a lower life expectancy.

Instruction Cycle

In order to run applications it interacts with the system Memory or RAM in a series of four steps known as the machine cycle or instruction cycle. Step one is the Fetch Phase.

Think of a software program as a set of instructions stored in the RAM. The CPU has registers to temporarily store the Address and Memory that it wants to interact with.

The program counter starts at 0 and copies that address to the Memory Address Register, then the control unit sends out a signal to copy the data from that address to the Instruction Register at which point it needs to figure out how to use this instruction in the decode phase.

The control unit parses the actual bits in the instruction. Most importantly the opcode contains the instruction like add or subtract and the Operand is the address in memory to perform that operation on.

The final execute stage takes the decoded information and passes it as electrical signals to the relevant parts of the CPU.

The Arithmetic Logic Unit or ALU can perform math on the data and then store the results in RAM to change the state of the program.

The cycle is repeated billions of times per second and modern chips utilize multiple CPU cores to run multiple computations in parallel. That’s how a CPU works under the hood but there’s a lot more that you should know about processor architecture if you’re a developer.

I love gaming and I have been playing games since the late 90s. I am very passionate about gaming and I always find the time to play different games. I started this website to take my hobby to the next level and share it with the world.

GPCD