What makes computer code ‘work’

798 viewsEngineeringOther

By this I mean, when you write code, what exactly gives that the power to do anything is it more code? 0’s and 1’s? more so, what gives that thing the power to do anything? At some stage I can only deduce what must just be magic making it work because it’ll boil down to something metal and plastic ‘talking to’ an electric current, which doesn’t make sense at all

In: Engineering

17 Answers

Anonymous 0 Comments

Everything inside the computer really comes down to very, very tiny electrical switches.

The most simple thing we do with those switches is called “a logic gate”. They have “inputs” and “outputs”, and do very specific things based on whether each input has or does not have electricity applied to it.

A very simple one is an AND gate. It has two inputs and one output. The rules are:

* If there is no electricity at either input, it will make sure its output is “off”.
* If there is only electricity at one input, it will make sure its output is “off”.
* If there is electricity at BOTH inputs, it will make sure its output is “on”.

We learned to build useful machines we call “circuits” out of these “gates”. For example, an “adder” is a circuit that adds numbers. Here’s how that works:

* First, we decide how to use patterns of “on” and “off” on inputs to represent numbers.
* The “adder” has many inputs. Usually at least 8 inputs for one number and 8 inputs for the other number.
* You set up the first 8 input switches to represent the first number, then the second 8 input switches to represent the 2nd number.
* Inside the “adder”, lots of AND gates and other gates are wired together in a way that the 8 outputs flip their switches to represent the number that is the result.

So if you squint, this is just like a gate works: you set up the “input” switches and get a useful “output” switch.

Computers also have “memory”, which is like a switch that can tell you if it’s on or off. So to store the number from our adder, we have to find 8 switches in memory and say, “Please flip these 8 switches to the same on/off status as these 8 output switches.” Then, later, if we want that number again, we can say, “Please connect these 8 switches to these input switches so they’ll have the same values.”

How does memory work? Gates and circuits! It has inputs to tell it, “What switches would you like me to tell you about?”. After you set up those switches, they are connected to a lot of other gates that flip their own output switches until eventually it connects the correct memory switch to an output switch. Think of it like a very complicated set of train tracks, where you want the train from a particular track to follow a very specific path to get to a very specific track on the “output”. You’d have to study the tracks and figure out how to rearrange all the switches to make that happen. That’s what’s going on inside memory and the CPU.

So when you turn a computer on, usually its components spend a little bit of time turning every switch off or on according to how the factory says they should “start”.

A CPU is… a circuit! A very complicated one. The input is “an instruction”, but all that means is we decided if we set up its input switches to be a certain number, that means “I want you to add the two numbers I set up your other input pins to represent.” When that finishes, the next instruction is probably a set of numbers that means, “I want you to store the result of the last thing you did in the memory location with the address I set up your other inputs to represent.”

Those instructions, and the memory addresses/numbers each instruction needs for inputs, are what is in a program. So when the CPU starts, usually it’s configured to look in a hard-coded factory location for a program at the start. So it connects the switches in memory at that location to its input switches. Then that memory gets treated like an instruction. Then it connects the switches in memory at the *next* location to its input switches. THAT memory gets treated as an instruction. This keeps going.

So the information about say, your OS is somewhere on your disk at that factory-specified location. The CPU knows to copy instructions from that part of the disk into memory right away. Once the OS is running, it has its own rules about loading programs, and when you ask it to run something, it controls telling the CPU which memory locations to connect to inputs for its instructions.

So it’s like a very, very complicated switchboard, where the machine itself is capable of flipping its own switches. Punchcards were a very direct analogy: the patterns of holes on a card were aligned with a set of switches, and if the paper had a hole the switch would go “down” and that set up patterns for the inputs of the circuits inside the computer. Later we figured out how to make the switches smaller, and how to use other kinds of switches to flip each other.

You are viewing 1 out of 17 answers, click here to view all answers.