How does coding physically work?

110 viewsOtherTechnology

Like how exactly can a bunch of letters, numbers, and punctuation symbols make your computer do all kinds of things? Plus what happens inside the computer when it executes the code?

In: Technology

22 Answers

Anonymous 0 Comments

When a person designs something like a CPU, they very deliberately arrange all the transistors do do very specific things. You have those that are responsible for doing math, those that specifically read and write memory and so on. It gets super complicated, but enough said that for very specific inputs, the circuit will perform very specific operations.

How this physically operates is rather complicated, but it comes down to using transistors letting a signal through or not, which translates to a 1 or a 0. By arranging those transistors appropriately (into Logic Gates), you can design a circuit that puts them in a specific order based on an input. And 1s and 0s in a specific order are a number. So by shuffling them around in the circuit in a controlled way, you can do math.

Then the designers tell everyone exactly what those inputs and operations are.

So someone then designs a program to translate a human-readable programming language (what you see programmers using) into those inputs (machine code). That program is the Compiler.

Anonymous 0 Comments

The CPU silicon understands things called “instructions” each instruction describes one thing to do, “move this here” “add these 2 values” etc. To understand how the silicon itself handles this, will take me hours of typing, so look up how an adder works on YouTube to get a basic idea for the rest of this.

Here is some code, this is what the computer actually deals with. From left to right we have

1. An instruction address: this is the location in memory that this instruction is
2. A function offset: Not really important here, it tells me how far into the function this instruction is.
3. “Machine language”: This is what the CPU actually reads and executes
4. The human readable instruction: I cant read machine language so this tells me what this does.

0x401000 <+0>:  b8 01 00 00 00                 mov    eax, 0x1
0x401005 <+5>:  ba 0d 00 00 00                 mov    edx, 0xd
0x40100a <+10>: bf 01 00 00 00                 mov    edi, 0x1
0x40100f <+15>: 48 be 00 20 40 00 00 00 00 00  movabs rsi, 0x402000
0x401019 <+25>: 0f 05                          syscall
0x40101b <+27>: b8 3c 00 00 00                 mov    eax, 0x3c
0x401020 <+32>: ba 00 00 00 00                 mov    edx, 0x0
0x401025 <+37>: 0f 05                          syscall
0x401027:       00 00                          add    byte ptr [rax], al
0x401029:       00 00                          add    byte ptr [rax], al
0x40102b:       00 00                          add    byte ptr [rax], al

Lets break this down. Memory (RAM) is broken down into bytes, each byte has an address, addresses start form zero and count up with one byte at each address. The CPU starts at the top of this and reads down, we have an instruction pointer that points to our starting point at the top `0x401000` the CPU executes this instruction moving the number `0x0` into `eax`. `eax` is a piece of memory on the CPU that can be accessed *very* quickly.

So how do we actually get this? Well a programming language like C is compiled, which means that the “bunch of letters, numbers, and punctuation symbols” is read by the compiler which will then spit out a file containing this stuff. Some languages work a bit differently where its interpreted, languages like Java, Python, and BASIC, where another piece of software will read `print(“Hello, world!”)` and the interpreter can read this an break it down to figure out what its being asked to do.

Anonymous 0 Comments

Not exactly ELI5 but if you’re really interested here are some great learning resources for the topic.

https://nandgame.com/

It’s an online game/challenge that guides you through how simple switches can do things like add numbers and perform other instructions. 
If you follow it you’ll get to building your own computer from scratch. This is pretty accessible and gets you to the “how do switches do X?” answers.
By the way, it’s free.

If you’re more interested in how programs get translated to ones and zeros you could check out https://craftinginterpreters.com/ (offers free web version). 
This guides you through writing your own language interpreter and compiler. 
It’s more advanced than nandgame and being able to program is a prerequisite, some rudimentary Python knowledge should be enough to get at least halfway through the book.

If you really want the full experience of designing your own computer and writing a compiler for it check out https://www.nand2tetris.org/
It’s quite similar to nandgame (I think it even inspired nandgame) but imo it feels a bit more fast paced and serious.
The great thing about this course is that it meshes in with the great book “elements of computing systems” (google is your friend).
Nandgame is like playing with legos, nand2tetris is more like taking a course on the subject.

More on the ELI5 level. Have a look at people building computers in Minecraft.
One notable example is this youtuber who is really going through the fundamentals https://youtu.be/osFa7nwHHz4?si=LN5oe_0s4XQfc6Jq

Anonymous 0 Comments

Imagine you are in your kindergarden group. There is this one weirdo kid who controls the candy machine. No one understand or cant command him/her to give you the Candy, except Randy. So to get sweet & sour yellow lemonade candy, you write that excatly down to a paper (you write the code). Then you give it to Randy (compiler) who starts speaking weirdos language (machine instruction). Weirdo then pushes button on and off and finally returns the exact candy back to Randy, who then gives it back to you.

More topics you need to ask from your kindergarden teacher: Compiler, Machine Instructions, Transistors

Anonymous 0 Comments

Okay, let me try to break this down into multiple layers:

**Hardware:**

Inside your computer is a bunch of interconnected “wires” and “gates”. I don’t think I need to give an explanation of a “wire”– But I think gates do deserve an explanation. Basically some wires are attached to these gates, and whether the wire has electricity running through it or not, the gate will “react” to that combination of electrical input, and either pass electricity along, or stop the passing of electricity.

Going even more basic, how could this work? One example is an “AND” gate, often represented like [this](https://www.electricaltechnology.org/wp-content/uploads/2018/02/AND-gate.png). What it does is that if both of the 2 wires going into it, are “turned on”, e.g. has an electrical signal going through them, it will output a signal. But if only one or none of the wires attached to it has an electrical signal, it won’t output anything.

Now I don’t know the exact fancy mechanisms of how these microscopic things work, but lets just imagine how we could make such a switch. You could either put in some type of resistance that can only be overcome when you have the combined amount of electricity running through both wires, or a switch that can only get turned on when both wires are providing power, the exact method is probably way past ELI5, but you can imagine something like this.

Now there is a handful of different kinds of gates, which react differently to each set of inputs. There’s forexample also a “NOT” Gate, and basically what it does, is reverse the signal. So you put in electricity into a wire, and the NOT gate stops that signal. Then when you turn the electricity off the wire, the NOT gate will start outputting a signal.

If you wanna look more into this, there is also XOR, NOR, OR and many other gates, that can take different set of inputs, and will “react” differently to them, and pass along an output depending on the circumstance.

Combining the gates in creative ways, you can start putting in a certain combination of input, and get a set of output, that can be useful.

**Language:**

Another element in understanding this puzzle, is also to understand that nothing and everything can have “meaning”, depending on how its “interpreted” by an observer. Forexample, as a european person who knows nothing about asian languages, I can look at chinese symbols, and its completely meaningless to me, because I don’t have any model to interpret and put meaning to those symbols. On the other hand, the arbitrary lines I’m typing out here, I have full intention of the meaning behind them, and since you asked the question in this language, I assume that we have some shared set of interpretation of these symbols, so by using them, I can transfer some meaning from my mind to yours.

However we do this, doesn’t really matter. And it doesn’t have to be symbols! We also talk, which is another way of communicating. There’s hand signals that deaf people use. And there’s even some super simple ways to do it, like morse code, where you just basically have 2 “symbols”, a long and a short beep. 2-symbol communication is something we can label as “binary”. Now if we look at the electrical wire, they kinda also have a binary option, either there’s electricity running through them or not.

We can forexample assign a number value to a bunch of wires, so forexample, the first wires value is 1, the second wire is 2, the third wire is 4 and so on. This would be the binary number system, so if I wanna write the number 6, and I have 4 wires, I would write it like this: 0110.

**Processing:**

Now combining the 2 elements above, we can built a set of wires and gates, where when we input the number 4 twice, which we could write as 0100 + 0100 we get the output 1000, which is 8, and we suddenly have addition! To see some more concrete examples of how to put the gates together to see this, you can do a simple google search, but I will also provide a link [here](https://www.electronics-tutorials.ws/combination/comb_7.html) to make it easy!

As you can see, we can use some quite simple steps, and a layer of interpretation, to make something useful. Now as with morse code, this binary set of 1’s and 0’s or ON/OFF wires, doesn’t have to be mapped to specifically numbers, we can also map them on to other symbols, words, actions, because its all just a matter of interpretation.

Anonymous 0 Comments

i tend to mentally picture computers as a massive printing press that churns out, or sometimes operates on, millions of Lite-brite sheets (if you remember those)

some sheets stand for instructions or operations or functions; a single specific task. like add, subtract, or move/copy some Lite-brite sheets from here to there. Then other sheets stand for values, input, output, that we humans are interested in.

by themselves, individual pegs in the Lite-brite sheet do not make much sense. but step back and look at the sheet as a whole and it makes a picture, or a symbol.

and the symbols only have meaning because we humans collectively agreed somehow on specific combinations of pegs representing an idea everyone understands. like an alphabet for any of the many human languages used throughout the history of mankind.

so combinations of Lite-brite pegs make sense to us humans even though the machine, the computer, doesn’t really know or care about the meaning. or in a similar way, individual lightbulbs in a ticker display like the one in Times Square in NYC that displays scrolling messages like stock values or “Victory In Europe!”

making those Lite-brite sheets, or screwing in individual lightbulbs in a scrolling ticker, that gets real tedious real quick. so we humans make up new words that represent predefined configurations of pegs on a Lite-brite sheets.

Those made up words start to become a language of their own, a programming language, if you will.

And a special machine at the heart of this massive monstrosity has a few hundred specific predefined Lite-brite sheets it is built to look for. literally hardwired into the electronic circuit (i.e. hardware)

That is the central processing unit, or CPU.

Just like a printing press operates in a predetermined sequence of steps to create one page of today’s newspaper, the CPU has a similar set of predetermined steps that make up one cycle of operation

and since we’re talking about an electronic machine instead of a bigger more physical machine, we can execute many many more cycles each second.

put enough cycles together with enough Lite-brite sheets with the right set of CPU operations – add, subtract, move/copy, etc. – and you start to be able to do process lots of information very quickly

so our made up programming languages, which are basically pre made Lite-bright sheets, tell that CPU machine to perform the right specific operations on values we feed it – in the form of other Lite-brite sheets – in the right order and suddenly this big old Rube Goldberg machine starts doing things that make sense to us humans

Instead of Lite-brite sheets, call them punchcards, and you have the makings of some of the original computer systems. What gets called a programming language is just shorthand for the more complicated specific predefined sequence of holes in the punchcard (or pegs in the Lite-brite sheet)

and to make it so humans can read the results easier, the results are printed on something like a piece of paper, or a ticker display, or a monitor/screen, using symbols humans understand (easier than on off bits of the machine) like letters and numbers of a human language’s alphabet

crank out enough Lite-brite sheets per second and one can achieve something of a flip book effect!

sorry to ramble off the top of my head in the wee hours of the morning typing on my phone with my fat thumbs but hopefully that starts to help explain some of the basic ideas of how a computer and programming languages work at a ELI5 level without confusing things too much

or, at the very least, gives you a bit of a chuckle 🤭 (either way, it’s time for a nap for me)

best of luck on your adventures in understanding computers. have fun with it!

ETA: if you haven’t heard of Lite-brites, its an old toy that was a lot of fun to play with when i was a little kid. see https://en.m.wikipedia.org/wiki/Lite-Brite

Anonymous 0 Comments

A computer executes machine code. It is a binary 0 and 1s in a very precise format the central processing unit can run.
There is a software named compiler that translates the human-written code into machine code, so it can be executed.
Another way is having an interpreter, which is a software that can read code and execute it directly.

Anonymous 0 Comments

The computer has a microchip called CPU that can change data on the computer memories such as RAM memory, CPU cache memory, on graphics card memory, or on permanent storage devices. When the CPU executes machine code instructions, it basically just changes data on one of those devices as instructed by the machine codes. So typically, a modern CPU understands about 3684 different machine instructions that change the memory data in own ways. So how does the CPU understands how to execute an instruction is not because it can think, but because it’s manufactured with billions of tiny electrical switches called transistors on the CPU chip. So one machine code instruction will flip those transistors in its own unique way to change one of the memories. We can mix these 3684 different machine instructions from top to bottom to do many millions of different data modification on all sort of devices.

Here’s a short snippet of CPU machine code to display a text on screen.

mov eax, 4 ; Request CPU to write data.
mov ebx, 1 ; We want to write to stdout (stdout is RAM memory for program output)
mov ecx, ‘Hello’ ; The data we want to write out is: Hello.
mov edx, 5 ; Length of the text is exactly 5 bytes.
int 0x80 ; Tell CPU to start writing data now.

In reality, we don’t write these machine code directly like this, because it would take such a long time to do it, and we could make many mistakes, like if we incorrectly put length of text to 4 instead of 5, we just create a buffer overflow error here, and this simple mistake can allow bad guys called hackers to add their own code to our program here, so this may allow them to hijack our computer.

So we created high-level programming languages to compile human readable code such as C language into machine code. There are hundreds of programming languages for all sort of purposes. Desktop software are typically written in C/C++, C# and Java. Websites are typically written in PHP, Javascript, Python languages for example. In the end, these languages will compile or translate the human readable code into machine code that the CPU can execute.

Anonymous 0 Comments

Imagine you have an electrical socket (just like what you have on the wall), multiple electrical devices, and a bunch of cables.

If you connect an electrical water pump to the socket using a cable, the pump runs. If you disconnect the cable, the pump stops. Of course connecting and disconnecting cable is dangerous, so let us use electrical switches instead. The pump is hot, so let’s connect a fan to the pump’s cable. Now whenever we switch the pump on the fan runs and when we switch the pump off the fan stops. We have just made our very first program! This is equivalent to:

if pump == ‘running’ then
fan = ‘running’
else
fan = ‘stop’
end

Notice that we cannot easily change this “program” because the way it works is completely tied to the way we connect the cables (or wires). This is why we have the phrase “hard-wired”, e.g., “I am hard-wired to be clumsy” which means I am clumsy and cannot change easily.

Now, our “program” only has two states: both pump and fan are running or both of them are not running. To have more states, we can connect more cables and switches such that:

* one cable connect the pump and fan to the electric socket
* one cable connect only the pump to the electric socket
* one cable connect only the fan to the electric socket

Then our program can reach any possible states: both of them are running, both of them are not running, and only one of them is running. Which state it has depends on which switch we press. As you can see, even two devices need so many cable and switches. When you have more devices, you need to have more cables and switches.

Not too long ago, scientists and engineers invented a microtransistor, which you can think of as a very small electrical switch. So small in fact, that you can fit millions if not billions inside your computer. Just like in our example, these “switches” control “devices”, e.g. each individual “tiny lamp” on your screen, also known as a pixel. Hope that helps

Anonymous 0 Comments

Basically, the central part of this is something called a “bit”. Bit is a value that can be either 0 or 1. Nothing more, nothing less. Physically, 1 means that you pass an electric shock/signal to your microchip and 0 means that you do not pass electricity to it. If your microchip had a lamp on it then 0 would mean no light and 1 would mean light.

Now, if you take 8 lamps in a row, each with its own wire, then you could pass different combinations of electric signals to them. So, for example:

1111000 would light up the first 4 lamps : 💡💡💡💡⚫⚫⚫⚫
10101010 would light up every second lamp: 💡⚫💡⚫💡⚫💡⚫

and so on.

The 8 bits stacked together is called a “Byte”. Since each bit can be 0 or 1, and there are 8 bits, you can have 2^8 = 256 different combinations of zeros and ones in a Byte. This means that you can actually agree with all other people on what each such combination means. You can make up whatever you want. For example, Soviet Union and USA had different values for different Bytes, and right now we are just using one standard agreed upon by everyone in the world.

So, anyway, for example in the ASCII standard

the letter ‘A’ is represented by 01000001 or ⚫💡⚫⚫⚫⚫⚫💡
the letter ‘W’ is represented by 01010111 or ⚫💡⚫💡⚫💡💡💡

This means that whenever you type “AW” on your keyboard it will first send the electricity to the circuit with the pattern ⚫💡⚫⚫⚫⚫⚫💡 and then a second later it will send electricity with the pattern ⚫💡⚫💡⚫💡💡💡. Now the word processor sees the same circuits light up and recovers the letters A and W that you typed on your keyboard and shows them back in your screen.

You can also make calculations using the binary arithmetic. So if you want to compute 5+3 you would pass three things to the computer:

1) it chooses the circuit type – there are lamps that only do adding (adders) and some only do subtracting etc. Whenever you pass two Bytes it will always add or always subtract them no matter what.
2) first number in Byte format
3) second number in Byte format.

So, to sum 5+3 you would choose adder circuits/lamps and pass them:

5 + 3 = 00000101 + 00000011 and the adders would return 00001000, which is 8.

And interestingly enough, every single operation that you do on your computer from clicking a mouse to watching a video can be reduced to such Bytes (combinations of 8 lamps) that are somehow added/subtracted etc. And all of that is about opening and closing electric current that goes to a particular lamp very very very fast.