It’s hard to answer this question without asking more questions. Simple as possible
We write code in a human readable language.
Compilers translate that into a thing that a computer can run
We wrote the compiler so that it could translate the code. Part of coding is we use libraries of pre written code so that we don’t have to rewrite everything ourselves and so we don’t have to know everything about everything
Some of those libraries are never changing and the same everywhere. Some of the libraries are specific to the computer but they have agreed to “names” and special “methods”. And the compiler puts those names and methods into the “compiled” code. It’s not really the compiler but the linker that does this. These are pulled in when the computer starts running your application
That’s how your application know specific things about your specific computer
A compiler just replaces words in programming language to words in machine language. For some programming languages it’s a bit more complicated, because the grammar is a bit different too, like if you wanted to translate English to Latin.
I suspect you also want to know how a computer then knows how to execute the machine code. I would compare this to a musical machine, like how a barrel organ “knows” which instruments to play without actually being able to read notes like a human. [Other example of musical machine](https://m.youtube.com/watch?v=IUwRW20agZ0) – very much in the ELI5 spirit, don’t you think?
“Reading” an instruction means that a certain circuit is closed that connects some input data from memory to the calculator and the result is connected to a certain memory location as well. Devices like keyboards and screens are just memory locations with numbers in them as well.
Transistors act as railway switches to guide to data along the correct path. Each instruction just sets up the switches in a certain way. One memory location is called the “program counter” and it determines where the *next instruction* should come from. It’s incremented by one (or four or eight or however much instructions are spaced apart) automatically unless it’s set by the current line if code. That’s how you do loops and function calls.
The calculator inside a computer works like the calculator in a simple electronic calculator. Are you interested in that as well?
**ELI5**
You have a wall full of [light switches](https://www.thespruce.com/thmb/BUwP79DQL5rJfQ_Ddz7oqqQ5h3Y=/2004×1127/smart/filters:no_upscale()/200526811-001-56a5a63e5f9b58b7d0ddd35e.jpg). Top to bottom, edge to edge, it’s just light switches arranged in a huge grid. Can you picture it? The whole wall is covered by them.
**PART I – Interpreting Switches**
Everyone has agreed that if switches 2, 544, 1097, and 45541 are turned on, that means the letter “A”.
Carry that idea to a larger scale. Which switches are on at the same time, “means” something. Maybe it’s a letter “A”, maybe it’s the number 4, maybe it’s a blue pixel at x-y coordinate (45, 98).
The important part is that we all agree how to “interpret” the current state of the switches.
After interpretation, the computer can be told to do things. Move the mouse cursor, turn this pixel to red, play a beep sound. “If these nine switches are on, play this song”.
**PART II – Turning On/Off Switches**
I tell the computer, “Anytime /u/PalaceOfPleasure69 types the letter G on the keyboard, turn switches 65, 101, and 876 on”.
I could also say, “Computer, anytime /u/PalaceOfPleasure69 moves the mouse to the left by one pixel, turn on switch 56, then 57, then 58.
So INPUTS from you, can control the current state of all the switches.
I could also make the switches react on or off based on a streaming video coming in. If the next byte off the ethernet is this, then turn those switches on.
So the important part here is that there are many things, both user-initiated and otherwise, that CHANGE the STATE of the switches.
**PART III – Go very fast!**
Now speed everything up to a million times a second. A million times a second, something changes some of the switches. A million times a second, some part of the computer looks at the switches and interprets them.
**PART IV – Putting it Together**
Inside your computer, are tens of millions of switches. They can either be on or off.
Let’s take the program Notepad as an example.
* When the user types an A, turn the appropriate switches on
* When these certain switches are on, show an A on the screen
* When the user clicks Save, turn other switches on.
* When these certain switches are on, take the contents of the Notepad, save it to disk.
This scales up to TREMENDOUS proportions. Everything on your computer is both setting switches on/off, and reading certain switches to make decisions. Sometimes it’s in reaction to you doing something, sometimes it’s in reaction to bytes coming in from the internet.
Does that help?
Do you remember the exercises you did as a kid when you diagrammed a sentence? You would find the nouns and the verbs, etc and use the structure of the sentence to build an understanding of it?
Compilers do the same thing. It takes the source files you give it, turns it into words and symbols, then diagrams them into a structure called an abstract syntax tree. From the AST, the compiler can figure out what order it needs to do stuff in, what parts are dependent on each other, and what the code actually means. Once it knows that, it can transform the program into machine code, which is the language your computer actually understands.
Machine code is a string of binary values, 1s and 0s. Each instructions is a specific string of binary values which control the machines inside the CPU.
The compiler turns your human readable source code into machine code.
Machine code is the low level mathematical instructions that can be understood by a CPU.
We conceptualise the machine code as ones and zeroes, but it represents electric signals that will be physically sent to the CPU when the program is run.
The CPU is physically designed and manufactured to perform mathematical operations based on the electric signals that are sent to it.
The specific design of what electric signals on what pins will trigger what operations – and likewise the way we translate the source code into the corresponding machine code – is the Instruction Set Architecture. They are documented standards, and you may have heard of some common ones such as ARM or x86.
Latest Answers