How do computers *really* work? (at the most basic level) While learning about computers I will read about RAM and Storage and the CPU, and while these explain the architecture of a computer and how parts of a computer work together, I still don't understand the fundamental workings of a computer program.
Let me take an example.
I'm on the web on a mac right now. When I push two fingers away from me on the trackpad, I understand that the trackpad is made up of a capacitive touch screen that, when your finger is placed on it, draws current and the trackpad detects that and converts it into data that explains the movement of my fingers up the screen.
However, when you have this data in binary form, or machine code, it goes through logic gates and other things to make it in scroll down on the page. But what is actually happening here? How would the computer have physical things that operate differently based on the input? What operations are carried out on the machine code that says &quot;if data looks like 01001010 10101010... send instruction 10010010... to CPU that tells computer to scroll down&quot;. Surely if you had some logic gates that turned input x into output y, yes they would give different outputs based on the input, but unless you somehow redirected the current to the right set of logic gates every time the action was carried out, how would it go to the correct one? To me this step is still magic. Especially with computers now that have no moving parts it seems even more alien.
Could someone explain this to me?
This is a complex matter. To begin with, when you move your mouse, there is so much going on at once, that I cannot even begin to explain everything here. So, I will focus on the most fundamental thing: How does the computer actually compute?
As you are probably familiar with, computers have memory called RAM, and a CPU that is able to execute &quot;commands&quot;. Commands, are essentially some form of data, that allows the computer to decide which logic gates to pass the input to.
At its core, this is the work of the ALU in the CPU. ALU stands for Arithmetic Logic Unit, and it had three inputs, contrary to what you may think. The first two inputs are normal inputs for the operations, for example: two numbers $a,b$ that we want to sum up. The third input, decides what operation to do on them. For example, you can encode that &quot;0001&quot; means we want to sum the two numbers, while &quot;0010&quot; might mean multiplication and &quot;0011&quot; is division. This ALU does this magic!
This &quot;magic&quot; that decides based on the third input which operation to do, is done via applying a multiplexer circuit to take the output from the correct series of logic gates in the ALU.
Basically, an $n$-to-$1$ multiplexer is given $n$ &quot;normal&quot; inputs, and one &quot;special&quot; input that decides which of the $n$ inputs to output. Yes, it simply outputs one of its inputs without changing anything. Basically, if we think of its inputs as an list $inp$, and the &quot;special&quot; input is called $s$, then the output is $inp[s]$.
The ALU takes this, and gives to it as the $inp$ the list of all possible operations on the two numbers $a,b$, and thats how the ALU knows which operation to apply when.