# 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 "if data looks like 01001010 10101010... send instruction 10010010... to CPU that tells computer to scroll down". 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?

## 2 Answers

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 "commands". 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 "0001" means we want to sum the two numbers, while "0010" might mean multiplication and "0011" is division. This ALU does this magic!

This "magic" 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$$ "normal" inputs, and one "special" 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 "special" 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.

The microprocessor has to read an I/O port that is connected to the mouse or track pad. The information is probably using more than one byte (8 bit) or one 'word' (32 bit), so the processor either read the same port many times or has other way to get the position of your fingers on the device. The information must be saved in RAM because the internal memory in the processor is too precious to waste with that kind of information. Now, you mention that, on your Mac computer, touching that device trigger printing something of the screen. So, as the processor finished to read the data from the trackpad, the next operation performed by the operating system or the app is to call a function which uses a input parameter the new position if your finger on the trackpad. The function compare with the previous position to decide which pixels to erase and which pixels to add on the screen. When ready, the function call a function to print. That function look at the X/Y position to make sure it is visible on the window. It also check if other window are sitting on top. If all conditions are good, the processor must send so many write commands to the video card controller to specify every pixels to repaint. The video controller save that information to its own video RAM, then do nothing else. Meanwhile, an independent group of digital circuit is reading asynchronously the video RAM to refresh the LCD display. Eventually, after one full scan vertically, the new position of your finger becomes visible on the monitor.