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

Show parent comment

18

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

Every 1 and 0 in a computer is just a voltage on a wire at roughly 0v to 5v, it's never actually 0 or 5 exactly, but it's close enough. There's a threshold voltage of 3v or so and if a wire is higher than the threshold, it's a 1, and lower is a 0. Also known as a "HIGH" or a "LOW" wire in electrical engineering.

This is the exact point where you cross over from our noisy day to day world, into the digital world of computing. All about high and low voltage.

As a specific example, the "Bus" that allows the CPU to talk to the RAM is basically nothing more than a big row of wires bundled together. The number 172 (10101100 in binary) on the bus is simply the 3rd, 4th, 6th, and 8th wires having a high voltage, and the rest a low voltage. (Binary number read from right to left)