do you know how your code

int f(){

return 42;

}

turns into

mov eax, 42 ret

and then into

B8 2A 00 00 00 C3

this?

<aside> šŸ’”

Here’s the short thing:

source code → something happens, Intermediate code forms → something happens again, Machine code is formed.

We’ll clear the ā€˜something’ in this article.

</aside>

Life would be very simple if we humans could write 1s and 0s and directly give machine it’s prefered machine code. But since we don’t have 1000 hands per person and the outputs that we’re expecting out of computers have evolved to complexities unimaginable, we need another simpler way to talk to the machines. And that is why we have different programming languages and their compilation processes.

There are compiled languages and interpreted languages divided on the basis of when the code is executed.

Compiled languages are programming languages that are converted into machine code by the compiler and only then, is there an executable file.

Interpreted languages are not converted into compiled code, rather the source code is directly executed line-by-line by the interpretor.

CAVEAT: most of the lanugages today use a mixture of the two ideas. Ex. Initially, the JVM interprets the Java bytecode produced by the Java compiler, executing it instruction by instruction until it identifies frequently executed (ā€œhotā€) code paths, which are then compiled into native machine code by the JIT compiler for faster execution.

Different representations of the code

Source code