The silent page awaits the waking dawn.
int add(int a, int b) {
return a + b;
}
int main(void) {
return add(10, 20);
}#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int size = MAX_SIZE;
return 0;
}#define MAX_SIZE 100
int size = MAX_SIZE;int size = 100;int result = a + b;a + ba + ;result = a + b;int a = 10;
int b = "hello";int add(int a, int b) {
return a + b;
}int calculate(void) {
int a = 10;
int b = 20;
return a + b;
}int add(int a, int b) {
return a + b;
}int add(int a, int b);
int main(void) {
return add(10, 20);
}int add(int a, int b) {
return a + b;
}execve("/path/to/program", argv, envp);
The Path from Compiler to Assembler to Machine Code
In the previous article we introduced the instruction set architecture (ISA) and saw that the ISA defines the machine-level contract between software and the processor.
But what we normally write is high-level code — C, Java, Go — while the CPU executes machine instructions. The two sit at noticeably different levels of abstraction.
So how, exactly, does a piece of source code become machine instructions?
Most people picture the process in a simple way:
That isn't wrong, but it hides a lot of important steps. The compiler has to understand the syntax and semantics of the source code and then translate the program into instructions suited to the target architecture; the assembler has to encode those instruction representations into machine code; the linker has to resolve the references between different object files; and the operating system still has to load the finished program into memory.
Using C and the common Linux ELF toolchain as our example, this article walks through the whole path from source code to execution on the CPU.
1. Source code is not what the CPU executes
Consider a simple C program:
A human can read this code directly: it defines an addition function and then calls it in main.
But the CPU cannot make sense of any of that directly:
All of these meanings have to be handled by the compiler first.
The compiler's job is not simply to swap each keyword for a machine instruction, but to progressively turn the program logic expressed in a high-level language into a form the target machine can execute.
A typical compile-and-run flow looks like this:
This is a simplified model of a typical toolchain, meant to make the idea easy to grasp. A real compiler may emit object files directly instead of printing assembly text and calling a separate assembler, and the exact pipeline varies from language to language and from toolchain to toolchain.
2. The compiler front end: understanding what the program expresses
The compiler's first problem is not producing machine code, but understanding the source code.
This part is usually called the compiler front end. It analyzes the source code and converts it into a structure the compiler can work with internally.
2.1 Preprocessing: expanding the directives in the source
For C, compilation normally begins with preprocessing.
The preprocessor handles the preprocessor directives that begin with #, for example:
The preprocessor expands header files, substitutes macros, and uses conditional compilation directives to decide which code takes part in the rest of the compilation.
For example:
After macro substitution, this is equivalent to:
Once preprocessing is done, what the compiler holds is the expanded source text.
Keep in mind that preprocessing is not a stage every language has. What we are discussing here is C/C++ and other languages that come with a preprocessing mechanism.
2.2 Lexical analysis: recognizing the basic units of the code
Next comes lexical analysis.
The compiler has to split the continuous stream of characters into meaningful lexical units, or tokens.
For example:
The lexer recognizes:
Source snippet
Token category
`int`
Keyword
`result`
Identifier
`=`
Operator
`a`
Identifier
`+`
Operator
`b`
Identifier
`;`
Separator
Lexical analysis answers the question of how characters combine into the basic elements of a language.
It does not decide whether a and b actually exist, nor whether they can be added together.
2.3 Syntax analysis: recognizing the structure of the code
Once lexical analysis has produced the tokens, the parser applies the language's grammar rules to decide whether those tokens can form a legal program structure.
For example:
can be recognized as an addition expression.
Whereas
does not fit the usual expression syntax and produces a syntax error.
The result of parsing is usually an abstract syntax tree (AST).
For the expression:
it can be abstracted into:
The AST preserves the program's structural relationships but drops many surface details that don't affect semantic analysis, such as some parentheses and separators.
2.4 Semantic analysis: deciding whether the program makes sense
Being syntactically correct does not mean the program is valid.
For example:
Syntactically speaking, this code parses fine, but the types don't match, so it is semantically flawed.
Semantic analysis checks:
Only after these analyses does the compiler gradually build up a complete picture of what the program means.
3. Intermediate representation: freeing the compiler from any one source language
If a compiler translated every high-level language directly into machine code for every CPU architecture, the number of combinations would be enormous.
Suppose there are 5 source languages and 4 target architectures. If each language implemented code generation for all target architectures on its own, you could end up maintaining 20 conversion paths.
More importantly, many basic operations and control flows in different languages have similar underlying representations.
To solve this, modern compilers usually introduce an intermediate representation (IR).
3.1 What the IR is
An IR is a representation of the program that the compiler works with internally.
It is neither as expressive for developers as C, nor as bound to a specific processor's instruction encoding as machine code.
Its goal is: to express a program's computation and control flow in a relatively uniform form, giving the compiler room to analyze and optimize it.
For example:
can be converted into LLVM IR like this:
Here, i32 denotes a 32-bit integer, add denotes integer addition, ret denotes returning the result.
This IR is no longer the original C code, but it is not yet x86-64 or ARM64 machine code either.
What it expresses is computational semantics that are relatively independent of any particular CPU architecture.
3.2 Why an IR is needed
The most important thing an IR does is decouple the front end from the back end.
The front end only has to convert the source language into IR; the back end only has to convert the IR into machine instructions for the target architecture.
That way you don't need a complete compiler of your own for every pairing of source language and CPU architecture.
Besides that, an IR gives optimizations a suitable space to work in.
For example:
The compiler can see that a and bare both constants known at compile time, so it performs the computation early and returns the result directly. This optimization is called constant folding.
An IR also makes it convenient to perform common subexpression elimination, dead code elimination, loop optimization, inlining, and other compiler optimizations.
3.3 An IR is not necessarily a single form
An IR is not one fixed format.
Different compilers use different intermediate representations, for example:
So don't casually lump all intermediate code together as one and the same IR. The layer they occupy, their design goals, and the way they execute can all differ.
4. The compiler back end: generating instructions for the target ISA
After the front-end analyses and the IR optimizations, the compiler has to turn the program into instructions the specific target architecture can execute.
This part is usually called the compiler back end.
The back end has to know the specific rules of the target ISA, for example:
4.1 The Same Logic, Different Instructions Across Architectures
Let's stick with the addition function:
On x86-64, it might generate:
On AArch64, it might generate:
Both express the same computational logic, but the instruction forms differ.
In the x86-64 example, the argument registers come from the System V AMD64 ABI; the AArch64 example uses the common AArch64 procedure call standard, which passes arguments inw0andw1passes the return value inw0.
This also illustrates an important distinction:
the ISA defines what instructions can do, while the ABI defines how separate software modules cooperate.
An ABI typically also covers data layout, calling conventions, object file formats, and the system call interface.
4.2 The Backend Also Has to Do Register Allocation
The IR may contain a large number of temporary variables, but a real processor has only a limited number of general-purpose registers.
For example, the IR might contain:
The compiler has to decide which physical registers hold these temporary values, or whether to spill some of them to stack memory when registers run short.
This process is called register allocation.
Register allocation directly affects the quality of the generated code. Poor register usage can increase memory traffic and instruction count, hurting performance.
The backend also performs instruction selection, instruction scheduling, and addressing mode selection, ultimately producing code that meets the target architecture's requirements.
Five: The Assembler Encodes Instruction Representations into Machine Code
The instructions produced by the compiler backend can first be expressed in assembly language.
Assembly is a textual form close to machine instructions, which is easy for humans to read and debug.
For example:
This code means:
But the CPU cannot execute this text directly.
The assembler converts each assembly instruction into machine code according to the instruction encoding rules defined by the target ISA.
5.1 How Assembly Instructions Become Bytes
Take the x86-64 instructions above:
One corresponding machine code encoding is:
You can break it down as follows:
Assembly instruction
Machine code
Meaning
`mov eax, 10`
`B8 0A 00 00 00`
write the immediate value 10 into EAX
`add eax, 20`
`83 C0 14`
add the immediate value 20 to EAX
`ret`
`C3`
return
These hexadecimal bytes are the instruction encoding that ends up in the object file.
Note that x86 instructions are not fixed in length. Different instructions may contain different numbers of opcodes, prefixes, addressing information, and immediate values.
ARM64 instructions, by contrast, are usually fixed-width 32-bit encodings. Encoding rules differ greatly between ISAs, which is one reason machine code for one architecture generally cannot run directly on another.
5.2 The Assembler Does More Than Simple Substitution
The assembler also has to handle some more involved work:
For example:
The assembler has to compute the distance from the jump instruction totargetand fill in the corresponding offset based on the instruction encoding format.
If the target address still cannot be determined, it may need to emit a relocation record and leave it for the later linking stage.
Six: Object Files — Machine Code Is Not Yet a Complete Program
What the assembler produces is usually not a complete, directly runnable program, but an object file.
On Linux systems, the common object file format is ELF (Executable and Linkable Format).
For example:
The-chere means compile and assemble only, without performing the final link.
The resultingmain.ocontains machine code, but may also carry a symbol table, relocation information, debug information, and other metadata.
6.1 What's Inside an Object File
An ELF object file usually consists of several sections.
Section name
Purpose
`.text`
Holds the program's instructions
`.data`
Holds initialized writable global or static data
`.bss`
Holds a description of the space needed for uninitialized or zero-initialized data
`.rodata`
Holds read-only data, such as string literals
`.symtab`
The symbol table, recording symbol information such as functions and variables
`.rela.text`
Relocation information related to the code section; its exact form depends on the architecture and file type
Here,.bssis usuallySHT_NOBITSa type; it doesn't actually store a whole block of zero data in the file, but instead has memory allocated and initialized for it at load time.
You can inspect the object file with the following commands:
6.2 Why Object Files Still Need a Symbol Table
Suppose there are two source files.
main.c:
add.c:
After compiling them, you get two object files:
When compilingmain.c, the compiler knows there is a function namedadd, but it doesn't know what memory address the function will ultimately be placed at.
So the object file retains a symbolic reference toadd.
This is where the symbol table and relocation information come into play.
Seven: The Linker Combines Multiple Object Files
The linker combines multiple object files and the required libraries into the final program.
It has to resolve two core problems:
7.1 Symbol Resolution
In the earlier example,main.oreferencedadd, whileadd.odefinedadd.
The linker has to find the definition of that symbol and establish the link between the two.
If no definition can be found for a symbol, you may see:
If the same symbol has several definitions that don't follow the rules, you may also run into a duplicate definition error.
7.2 Relocation
Once the symbol has been found, you still have to work out where it finally ends up.
For example,main.o contains an instruction that calls add, but when compiled on its own, add's final address hasn't been determined yet.
After the linker lays the object files out in the address space of the final program, it can patch the call site or the relevant address fields based on the symbol's address.
This process is called relocation.
Relocation doesn't mean that every address has to be settled at link time. For dynamically linked programs, some symbols and addresses may still be handled by the dynamic linker when the program is loaded.
7.3 Static Linking and Dynamic Linking
At link time, the libraries a program depends on can be organized in different ways.
Static linking copies the relevant parts of the library code it needs into the final executable. Once deployed, the program typically no longer depends on the corresponding shared library files, but the executable may be bigger, and library updates usually mean relinking the program.
Dynamic linking, on the other hand, leaves the program dependent on shared libraries at run time. The executable keeps references to those shared libraries and their symbols, and the dynamic linker performs the necessary mapping and symbol binding at load time.
Static and dynamic linking each suit different situations. A real project may well use both: some dependencies are linked statically, others dynamically.
8. The Operating System Loads the Program: Making It Ready to Run
After the linker produces the executable, the program still can't simply run out of thin air. The operating system has to create the process and map the program into the process's virtual address space.
On Linux, running a new program typically involvesexecve system call.
execveOn success, the program image of the current process is replaced by the new program's image; it doesn't simply create a new process.
8.1 The ELF Loading Process
The Linux kernel parses the ELF information in the executable, uses the program headers to determine which segments need to be mapped, and sets up the corresponding virtual memory regions.
Here we need to distinguish between ELF sections and segments:
When the operating system loads a program, it works from the segments described by the program headers rather than loading sections one by one.
The typical process can be summarized as:
For a dynamically linked program, the kernel usually finds the dynamic linker from the ELF information and hands control over to it first. Once the dynamic linker has loaded the required shared libraries and completed relocation, it passes control to the program's entry point.
For a statically linked program, no conventional dynamic linker takes part in this step.
8.2 The CPU Finally Starts Executing
Once the program image and the runtime environment are ready, the processor starts executing instructions at the entry address.
The CPU's basic work can be abstracted as:
That's the basic process once a machine instruction reaches the processor's execution stage. How modern CPUs speed things up internally through pipelining, out-of-order execution, and branch prediction will be covered in later chapters.
9. Different Languages Don't All Follow the Same Path
So far we've mostly used C to explain the native compilation toolchain, but not every language directly generates machine code for the target CPU.
Different languages may adopt different execution models.
9.1 C / C++: Usually Compiled Ahead of Time
C and C++ usually generate machine code for the target platform through ahead-of-time (AOT) compilation.
Most of the machine code generation is finished before the program runs. At run time it may still depend on shared libraries, runtime libraries, and operating system services.
9.2 Java: Bytecode and the JVM
Java usually compiles source code into JVM bytecode first:
Compilation produces a .class file, which contains JVM bytecode rather than machine code that directly targets x86-64 or ARM64.
The JVM can interpret bytecode, or it can compile hot code into native machine code according to its runtime policy. Which approach is used, and when it kicks in, is decided by the JVM implementation and its runtime policy.
So Java bytecode isn't the ISA of any particular physical CPU; it's a virtual instruction system defined by the JVM.
9.3 Go: Usually Produces Native Machine Code
Go typically has its compiler generate machine code for the target platform ahead of time, producing a native executable.
Go programs also usually embed the Go runtime, which provides garbage collection, goroutine scheduling, some memory management, and other runtime services.
So although Go typically generates native machine code directly, a Go program still can't run without its runtime environment.
10. Cross-Compilation: Building Programs for Another Architecture on One Machine
Once you understand the target ISA and the compiler backend, you can go further and understand cross-compilation.
Cross-compilation means running the compiler on one platform while producing a program for another.
For example, building an ARM64 Linux program on an x86-64 Linux machine.
Here, the environment the compiler runs in is not the same as the environment the target program will run in.
Cross-compilation is essential for embedded development, cloud builds, multi-architecture container images, and operating system development.
Still, cross-compilation isn't just about naming the target ISA; you also have to account for the target operating system, the ABI, system libraries, headers, and the linker. Generating instructions for the right architecture doesn't guarantee that the program will actually run correctly on the target device.
11. Putting the Whole Process Together
Now we can lay out the complete path from source code to execution on the CPU:
Each stage solves a different problem:
Stage
Core task
Preprocessing
Expand macros, headers, and conditional compilation
Compiler frontend
Understand the structure and semantics of the source code
IR and optimization
Build a unified representation and improve the program
Compiler backend
Generate instructions for the target ISA
Assembler
Encode instructions as machine code
Linker
Resolve symbols and perform relocation
OS loader
Set up the process image and the runtime environment
Dynamic linker
Load shared libraries and perform the necessary bindings
CPU
Fetch, decode, and execute machine instructions
12. Summary
Turning source code into machine instructions the CPU can execute isn't a simple text transformation; it's a complete chain of compilation, encoding, linking, and loading.
To understand that chain, you need to grasp a few key points:
At this point we've understood the ISA at a high level and seen how high-level languages turn into machine instructions step by step.
The next article moves into concrete instruction sets, taking x86-64 as an example, to look at the registers a processor exposes to programs, instruction operands, and memory addressing modes — and to understand what a single machine instruction is actually able to express.