Skip to content
← Back to Community
How a CPU works.
Profile icon
JustAWalrus

How a CPU works.

Hello.

So recently I found out that most people involved or not with programming have no idea how a CPU works.

This bothered me.

And I didn't like that.

So I decided to find why this was the case. And the more I though about it I realized that most people never have to deal with a CPU in programming at all.

For most of you, programming is about variables, classes, functions, ect. And all of that is too high level to ever need to worry about a CPU.

The only reason you would need to is out of curiosity.

And I'm here to help you sate that curiosity.

What is a CPU?

I'll make this one short due to the fact that most of you probably know what a CPU does at a higher level already.

In short, a CPU or Central Processing Unit is what does all the actual computation in your computer

It can also interface with the other components in your computer.

What is inside a CPU?

At a very low level a CPU is just a ALU, and registers.

aaaaa

The ALU

The ALU or the Arithmetic and Logic Unit is where all the Arithmetic and Logic happens.

Inside the ALU is what is known as logic gates.

Gate map

These are little electrical circuits that when given any input return (out the other end) a output via electrical wire.

Lets look at an example.

The AND Gate

The and gate only returns a 1 (or an electrical current) when all of the inputs are 1 (or on).

Back to the ALU.

The ALU consists of these gates and they can perform logic.

When these gates are put together you can make some cool things.

Such as adders.

The registers

A Computer needs to store things its working with.

This can be done via RAM.

Or a few registers inside the actual CPU.

These are just little circuits that can store values.

The fetch and execute cycle.

The CPU needs stuff to do.

So it has instructions in RAM.

These instructions can tell the CPU to do certain things.

But how does it get these?

It uses a cycle.

Firstly it fetches the binary instruction from RAM.

Then it decodes it.

Then it executes whatever the instruction said to do.

Conclusion.

Congrats! We have learned about a simple CPU!

It can execute instructions that are in RAM and store values in internal registers!

I hope you enjoyed.

Upvoting is caring :) - @Bookie0 2020

Voters
Profile icon
aaalyzee
Profile icon
panzer-chan
Profile icon
Alaxalandre
Profile icon
the-o
Profile icon
saarthak1607
Profile icon
KYLADUNNE
Profile icon
Loshad
Profile icon
z0r
Profile icon
RicksNext
Profile icon
JTpotato
Comments
hotnewtop
Profile icon
AmazingMech2418

Nice! Though, I was kind of hoping that it would explain more of like how the ALU combines multiple logic gates to do stuff and run the programs...

Profile icon
JustAWalrus

A tutorial on that is coming up :) @AmazingMech2418

Profile icon
JustAWalrus
Profile icon
HahaYes

hmmm decently nice tutorial, you should've included GPU, more intresting but whatever

Profile icon
JustAWalrus

The GPU is usually not worked with directly. The BIOS handles most of the GPU stuff so talking about it really doesn't help anybody. @HahaYes

Profile icon
HahaYes

@Wuru yeah BIOS kinda sucks

Profile icon
HahaYes

@Wuru should we team up to make better tutorials?

Profile icon
JustAWalrus

Your choice, though I am not very good with teams. I do however need some help with the Mega Web-Dev Course if you were interested. @HahaYes

Profile icon
HahaYes

@Wuru ah okay. We'll team up?

Profile icon
JustAWalrus
Profile icon
rediar

Nice informative tutorial! Maybe tutorial on other pieces of hardware, and how they work next?

Profile icon
JustAWalrus

If this gets enough upvotes yes. @rediar

Profile icon
EpicGamer007

@Wuru, It's got 40. Go for it!

Profile icon
JustAWalrus

On what though? And maybe this future tutorial could be our collab? @AbhayBhat

Profile icon
JustAWalrus
Profile icon
JustAWalrus
Profile icon
AtriDey

@Wuru I want a tutorial on CUDA just to be mean.

Profile icon
xxpertHacker

Most don't need to know about the CPU because programming often refers to software programming, the hardware often times doesn't matter.

Now, I personally want to learn about compilation for hardware, I heard it takes forever.

Profile icon
glitchish

This is nice, I just am not about how i feel about

"Upvoting is caring"

Profile icon
Sholohz

Thx so much i really needed that lolo.

Profile icon
[deleted]

Cool tutorial! But I think not many people here on Repl.it know about a CPU or how it works is because they mostly focus on software, not hardware.

Profile icon
kuzz

cool

Profile icon
k9chelsea2

cool, I already knew all this but wasn't 100% sure what everything meant you really helped clear that up. thx lol

Profile icon
Neurolizer

😊 comprehensive and interesting learning. And I guess the number of cycles a CPU does, is called Hz?

Profile icon
AtriDey

@Neurolizer Hz is a unit of how many times something happens per second. For example, your monitor is probably displaying frames at 60 Hz.

Your CPU does its work in what are essentially blocks called clocks (very simplified so take it with a grain of salt). In this clock, it carries out instructions. For example, a 3rd gen Ryzen chip carries out about 3.2 instructions per clock.

I established earlier that Hz is simply unit of measure. Manufacturers measure the speed at which clocks are executed in Hz. For CPUs specifically, this is called clock speed. Modern CPUs can actually vary the clock speed. For example, my CPU runs about 0.7 GHz (700,000,000 Hz) on idle, and is currently running at 2.1 GHz due to repl.it and pesky old Windows Defender, and in a game while adequately cooled can run, realistically, at 3.9 GHz. For the record, thats 3.9 billion Hz. And that's not even the top end.

Profile icon
arcade331

Thank you. Now I can imagine how the CPU works.

Profile icon
schaumont

Thank you.

Profile icon
AshishSarkar

Nice tutorial I upvoted man

Profile icon
[deleted]

thank you for this!

Profile icon
lichr

Thank you very much for posting this tutorial!

Profile icon
firefish

I love the fact that you post a tutorial every single day. EVERY SINGLE DAY. That's so outrageous, you cycle-farming spammer.

Profile icon
DynamicSquid

@johnstev111 but he's actually helping people with these tutorials

Profile icon
JustAWalrus

Dude, stop being childish. I make full courses now and not multiple-parters. I've already responded to your demands and improved. These tutorials teach people and help them learn. Unlike some project you may or may not make that people may look at for a second and never use again. Some of this stuff I teach people, for example Clean Code, can stick with them for a long time unlike some repl. Please stop holding a grudge against me for no good reason. All you are doing is proving yourself to be immature. By posting one every single day I am just helping people learn more often. Cycles don't really matter to me, it's just a number. Doesn't prove anything and or help you in the long run. So next time, consider what you're saying before you make a comment. @johnstev111

Profile icon
firefish

@Wuru The multi-part tutorial is the problem. It's cycle s q u e e z i n g, and @Hahayes has admitted that nobody should do it.

Profile icon
firefish
Profile icon
JustAWalrus

I used to do it. You really should research your stuff before jumping onto a bandwagon, kid. My stuff is now one-off.

I make full courses now and not multiple-parters.

I could've milked this and made it into a series for an epoch. Alas, I didn't. Please think before you act, johnstev. @johnstev111

Profile icon
AtriDey

@Wuru Is he jealous that he can't oversimplify a comparatively basic concept? Or is he just jealous that he doesn't get useless internet points?

Profile icon
DungeonMaster00

this tutorial is good.

cant wait for the web dev course

Profile icon
CodingGoose

Nice tutorial. I only knew what CPU meant before reading this tutorial :)

Profile icon
potatojs

great tutorial!
but i have a smaaaaal remarke about the AND gate, you sayed 'both' but in reality the AND gate can have any number of inputs, same as the OR gate.
continue the great work:)

Profile icon
JustAWalrus

Fixed :) @potatojs

Profile icon
potatojs

yey!
@Wuru

Profile icon
Jakman

As a side. Write a tutorial explaining how to write a language in itself like how C wrote the stdlib in itself.. I want to see just how good of programmers are on this site.

Profile icon
JustAWalrus

What good would that do? @Jakman

Profile icon
JustAWalrus

Also I don't write programming languages. I write operating systems so I am not the best source on that. @Jakman

Profile icon
Jakman

@Wuru ok. So C was written in ASM and itself. This was done by using the abilities given from ASM and using those abilities to write C in itself. Kind of how the python standard lib was written in itself mostly

Profile icon
DynamicSquid

@Jakman That has always been a goal of mine, to create a language good enough so that you can use it to create your language

Profile icon
Jakman

@DynamicSquid i can already see you making Night libraries in Night after giving it some more control in Cpp

Profile icon
DynamicSquid

@Jakman Yup! That's my plan! I just gotta finish some bugs I have with functions, then I can start work on predefined functions, and eventually entire libraries

Profile icon
Jakman

@DynamicSquid sounds great!. I could help with some C libs like most languages have if you would like for me to help.

Profile icon
DynamicSquid

@Jakman it's still going to be a while until I start actually making the library, but I might need your help cause I've done nothing like it before. thanks for offering btw!

Profile icon
firefish

@Wuru Yes, a tutorial on bootstrapping would be wonderful.

Profile icon
Bookie0

Um why do you keep mentioning me lol you don’t have to say that all the time

Profile icon
JustAWalrus
Profile icon
Bookie0

Okie dokie @Wuru