r/computerscience • • Oct 18 '24

how exactly does a CPU "run" code

1st year electronics eng. student here. i know almost nothing about CS but i find hardware and computer architecture to be a fascinating subject. my question is (regarding both the hardware and the more "abstract" logic parts) ¿how exactly does a CPU "run" code?

I know that inside the CPU there is an ALU (which performs logic and arithmetic), registers (which store temporary data while the ALU works) and a control unit which allows the user to control what the CPU does.

Now from what I know, the CPU is the "brain" of the computer, it is the one that "thinks" and "does things" while the rest of the hardware are just input/output devices.

my question (now more appropiately phrased) is: if the ALU does only arithmetic and Boolean algebra ¿how exactly is it capable of doing everything it does?

say , for example, that i want to delete a file, so i go to it, double click and delete. ¿how can the ALU give the order to delete that file if all it does is "math and logic"?

deleting a file is a very specific and relatively complex task, you have to search for the addres where the file and its info is located and empty it and show it in some way so the user knows it's deleted (that would be, send some output).

TL;DR: How can a device that only does, very roughly speaking, "math and logic" receive, decode and perform an instruction which is clearly more complicated than "math and logic"?

163 Upvotes

151 comments sorted by

View all comments

0

u/[deleted] Oct 18 '24

[deleted] — view deleted comment

53

u/BobbyThrowaway6969 Oct 18 '24 edited Oct 20 '24

To add, there is no "delete file" instruction. Deleting a file may have thousands of simple CPU instructions, but you don't notice it because a modern CPU runs through a billion tens of billions of them each second.

So a CPU might be pretty simple, like placing a lego brick is simple, but place enough of them and you have a lego city.

22

u/hpela_ Oct 18 '24 edited Dec 05 '24

glorious compare roof airport sort employ correct disarm noxious depend

This post was mass deleted and anonymized with Redact

17

u/BobbyThrowaway6969 Oct 18 '24

Oh absolutely. Modern CPUs are insanely complex.

→ More replies (16)

4

u/Ghosttwo Oct 18 '24

There's actually a separate computer in the hardrive that turns simpler directives from the CPU into the various block-level instructions used to perform the operation and manage empty space.

Given the standard definition of a 'computer' (i/o, memory, datapath, control, etc), I figure my phone has no less than seven discrete sub-computers in it, each communicating with its neighbors and slaved to one or the other. There's the main CPU itself, the display driver, the SIM card, the wifi module, the GPS chip, the camera unit, as well as the GSM module that does the long range stuff. Maybe even a few extra ones for stuff like JTAG, battery charging, NFC decoder, finger print reader, etc.

Each chip will have it's own little bit of ram, firmware, all the bells and whistles.

1

u/GwynnethIDFK Oct 19 '24

Hell some modern x86 cores will have smaller ARM or RISC-V cores in them for doing some operations. It's kinda insane.

2

u/Badtimewithscar Oct 18 '24

Correction, you can buy 3ghz CPUs

1

u/BobbyThrowaway6969 Oct 18 '24 edited Oct 18 '24

I factored that in haha. Each instruction takes on average 4 or so clock cycles.

2

u/Badtimewithscar Oct 18 '24

Ah thx my bad, sorry

Brains working on caffeine and photosynthesis rn

→ More replies (2)

1

u/d0x360 Nov 11 '24

I bought a 6 core i7 5820k in 2014ish.  Boost clock wss like 3.6ghz but I got that puppy cranked to 5.4 ghz max but dialed back and settled at 4.8ghz because I'm not insane and used custom loop water not ln2 

6

u/DesiBail Oct 18 '24

The real answer is instructions. Your CPU receives various instructions that it then uses to give control signals to various components like the ALU. The way that it performs the instructions is physically etched into the silicon.

Who controls the circuits which release these instructions

2

u/Orangutanion Oct 18 '24

In many cases the ROM. The instruction picks a portion of the ROM and then the control signals for the various elements in the circuit are all in there.

1

u/DesiBail Oct 18 '24

In many cases the ROM. The instruction picks a portion of the ROM and then the control signals for the various elements in the circuit are all in there.

Question for non electronics guys is who controls the controllers and so on. How is data in storage turned into instructions (who decides which port pin gets the on current and which does not)

2

u/pjc50 Oct 18 '24

I think what you need here is "instruction decode" possibly via a "microcode table". You can imagine writing down a very large table with numbers (opcodes) on the left and "turn on the ALU for one cycle then turn on the data lines to memory for one cycle" etc on the right.

1

u/Orangutanion Oct 18 '24

The first thing to understand is that the hard drive and the motherboard are separate devices. There is basically a serial connection scheme that can load stuff in storage into RAM. Precompiled instructions in storage get put into the RAM serially, and then the CPU reads instructions from RAM. This is heavily simplified and isn't taking into account the cache, which essentially acts as an extra smaller/faster buffer between RAM and CPU.

2

u/[deleted] Oct 20 '24

the concept of bootstrapping is really useful here. Super small and simple program spawns a slightly larger one and that continues recursively

→ More replies (6)

3

u/CowBoyDanIndie Oct 18 '24 edited Oct 18 '24

Not a great description, the cpu does not receive anything, it is not passive. When a cpu powers on, it initializes to read its first instruction from a specific memory address, so it sets the memory controller to read from that address, it reads the bits and executes that instruction. On a regular computer, that first instruction is usually from the bios. For a microcontroller it might actually be the first instruction you flashed onto its eeprom. On the computer the bios provides the information about the actual hardware to the cpu, things like its io ports, disk controllers, etc, accessing the actual ram. (Ya the cpu can start and do stuff without main ram, mainly it can yell at you that there is no ram, it can’t do useful things like load a regular os, but it can run instructions). From The bios is learn where to look for its next instructions, the boot disk, load from lan, usb boot, etc. It reads the first part of the drive, gets a boot loader, and progresses from there.

Edit: in the microcontroller situation, its actually important to known what address the microcontroller looks for its first instruction, also, “memory addresses” are not necessarily actual memory, or even the same type of memory, a memory address could actually be reading pins from a board. Some cpus especially 8 bit can switch between reading rom and ram using the same addresses, flipping a bit logically controls whether its reading from one bus or another.

2

u/Redsss429 Oct 18 '24

With the physically etched instructions, how does the CPU "pick" the right instruction? If I pass something really simple which I'm assuming would be an etched instruction, like an AND, how does the CPU correctly route the electrical signal through to the circuitry for AND?

5

u/Current_Band_2835 Oct 18 '24 edited Oct 19 '24

Each instruction has a section that is the operation code. The operation code corresponds to which “wires” are given voltage. This combination of active wires will go through a circuit that links them to another circuit that performs the operation. 

 So, for instance if your operation code is 5 (00…101) which adds, the rightmost and third to right most wire are given voltage. This goes through a circuit of and, or, not gates to result in only the fifth output wire getting voltage. This fifth output wire links to the ADD circuit, which is made up of and, or, not gates. The numbers being added are other parts of the original instruction. 

 That’s the basic idea anyway

1

u/Orangutanion Oct 18 '24

One of the most crucial circuits in a CPU is a multiplexor. Say you have two inputs, a and b. You want one of those two inputs to be routed to an output. You have a control signal s that chooses between them. Then the output is:

    out = (s)? b : a;

If s is 0, output a. Else if s is 1, output b. What this means is that the CPU can calculate both VALUEX AND VALUEY and VALUEX + VALUEY, then s decides which one the CPU wants to use via the multiplexor.

1

u/Perfect-Campaign9551 Oct 18 '24

Decoding circuitry. Quite simple really, it just looks for a specific set of inputs to be high and then sets a specific output high when it see that pattern.

1

u/Badtimewithscar Oct 18 '24

The instruction sent will be used, I made my own cpu in minecraft and what I did was pretty much just take a bit from the instructions, and if it was on I'd send some other data to something, but if it was off that part did nothing

1

u/CowBoyDanIndie Oct 18 '24

Not a great description, the cpu does not receive anything, it is not passive. When a cpu powers on, it initializes to read its first instruction from a specific memory address, so it sets the memory controller to read from that address, it reads the bits and executes that instruction. On a regular computer, that first instruction is usually from the bios. For a microcontroller it might actually be the first instruction you flashed onto its eeprom. On the computer the bios provides the information about the actual hardware to the cpu, things like its io ports, disk controllers, etc, accessing the actual ram. (Ya the cpu can start and do stuff without main ram, mainly it can yell at you that there is no ram, it can’t do useful things like load a regular os, but it can run instructions). From The bios is learn where to look for its next instructions, the boot disk, load from lan, usb boot, etc. It reads the first part of the drive, gets a boot loader, and progresses from there.