1. Why do we need to distinguish three kinds of code?
Programmers usually write programs in high-level programming languages, but a CPU cannot directly understand the syntax of high-level languages such as Python, Java, C, or C++.
Before a program actually runs, the code usually has to go through a series of transformations to convert the program written by the developer into a form the target platform can handle.
This process involves three important concepts:
- Source Code: the original program code written by the programmer.
- Object Code: an intermediate product generated by a compiler or other compilation tool, usually containing machine instructions, data, and symbol information.
- Machine Code: the instruction encoding that a processor architecture can execute directly.
The three are not related in a simple one-to-one way.
For example:
- A single source file may produce multiple object files.
- Multiple object files can be combined by linking into a single executable file.
- The machine instructions in an executable file are ultimately executed by the CPU.
- Some languages also introduce additional code forms such as bytecode and virtual machine instructions.
2. Source Code
2.1 What is source code?
Source code is the program text that a programmer writes using a programming language, and it is the primary object of editing during software development.
For example, here is a simple program written in C:
#include <stdio.h>
int main(void) {
int a = 10;
int b = 20;
int result = a + b;
printf("%d\n", result);
return 0;
}
This code is C source code.
It contains:
| Component | Example | Purpose |
|---|---|---|
| Preprocessor directive | #include |
Includes header files and other preprocessing content |
| Function definition | main() |
Defines the program entry function |
| Variable declaration | int a = 10 |
Declares variables and their types |
| Expression | a + b |
Describes a computation |
| Function call | printf() |
Calls a library function |
| Control statement | return |
Controls the function's return |
Source code is mainly aimed at developers and emphasizes readability, maintainability, and expressiveness.
2.2 Characteristics of source code
1. Highly readable
High-level languages express program behavior using syntax close to human logic.
For example:
result = a + b
Developers can understand the meaning of the computation directly without needing to care about how the CPU performs the addition.
2. Depends on the programming language specification
Source code must conform to the syntax and semantic rules of the corresponding language.
For example:
int number = 10;
This is a valid C variable declaration.
In different languages, the rules for variable declarations, type constraints, function definitions, and so on may be completely different.
3. Usually not tied to a specific CPU instruction set
The same C source code can be compiled by compilers on different platforms to produce programs for different processor architectures.
For example:
- x86-64
- ARM64
- RISC-V
But this does not mean the source code is fully cross-platform. Operating system APIs, compiler extensions, data type sizes, system calls, and third-party dependencies can all affect portability.
2.3 Source code is not the same as an executable program
Source code usually cannot be executed directly by a traditional CPU.
For example, the following C code:
int result = 10 + 20;
The CPU does not directly parse int, variable declarations, and high-level language expressions.
It needs to go through compilation and other processing to be converted into instructions the target platform can execute.
However, the execution models of interpreted languages and virtual machine languages are different. For example, Python source code can be handled by an interpreter, and Java source code can be compiled into bytecode and then executed by the JVM.
3. Object Code
3.1 What is object code?
Object code is the program product generated after a compilation tool compiles source code.
In a traditional native compilation workflow, object code usually exists in the form of object files.
Common file extensions:
| Platform or format | Common extension |
|---|---|
| Linux / Unix | .o |
| Windows | .obj |
| macOS | .o |
For example, compiling a C source file with GCC:
gcc -c main.c -o main.o
Here:
main.c: source code file.-c: compile only, do not perform final linking.main.o: generated object file.
3.2 What Does an Object File Contain?
An object file is not just a simple collection of machine instructions.
A typical object file may contain the following:
| Component | Purpose |
|---|---|
| Machine instructions | Processor instructions generated after compilation |
| Data segment | Content related to initialized and uninitialized data |
| Symbol table | Records symbol information such as functions and variables |
| Relocation information | Records address references that need to be corrected during linking |
| Debug information | Supports breakpoints, variable inspection, and source-code location |
| File header | Describes the file type, architecture, and information about each section |
The specific organization of different object file formats varies.
For example:
- ELF: a common executable and object file format on systems such as Linux.
- COFF: the basis for the object file format used by Windows and related toolchains.
- Mach-O: the file format used by Apple platforms such as macOS and iOS.
3.3 Why Can Object Code Usually Not Run Directly?
Although an object file already contains machine instructions, it has usually not yet completed all the address resolution and dependency integration required by the program.
For example, a program calls an external function:
#include <stdio.h>
int main(void) {
printf("Hello\n");
return 0;
}
The compiler can generate machine instructions that call printf, but when compiling main.c separately, it usually cannot yet determine which address printf will ultimately be located at.
Therefore, an object file may retain:
- Unresolved external symbols.
- Address references that need correction.
- Relocation records.
This information needs to be handled during the subsequent linking process.
Note that not all object files are necessarily non-executable. Some platforms or toolchains support special forms of object files, or the object file itself is in an executable format. However, in common native GCC and Clang compilation workflows, .o files are usually intermediate products waiting to be linked.
IV. Machine Code
4.1 What Is Machine Code?
Machine code is the instruction encoding that a processor can execute directly.
It is represented in binary form, and the specific encoding rules are defined by the CPU instruction set architecture (ISA).
For example, x86-64 and ARM64 use different instruction encoding rules; even if they perform the same computational task, the generated machine code is usually different.
Machine code usually contains:
- Opcode: specifies what operation to execute.
- Operand: specifies the data or location involved in the operation.
- Addressing information: describes how to access registers, memory, or other operands.
For example, a machine instruction may represent:
Add the values in two registers and write the result to the destination register
The binary encoding of an actual machine instruction is determined by the processor architecture.
4.2 Relationship Between Machine Code and Assembly Language
Machine code and assembly language express processor instructions at the same level, but in different forms.
| Comparison Aspect | Machine Code | Assembly Language |
|---|---|---|
| Representation | Binary bytes | Mnemonics and operands |
| Intended for | CPU | Programmers and assemblers |
| Readability | Relatively low | Relatively high |
| Execution method | Executed directly by the CPU | Must be assembled into machine code |
| Instruction correspondence | Actual instruction encoding | Usually corresponds to one or more machine instruction encodings |
For example, the following is an x86-64 assembly instruction:
mov eax, 10
It means placing the value 10 into the EAX register.
The corresponding machine code can be represented as hexadecimal bytes:
B8 0A 00 00 00
The byte encoding here is related to the rules of the x86 instruction set.
Note the following:
- Assembly language is a textual representation of machine instructions.
- Machine code is the actual encoding of instructions.
- An assembler is responsible for converting assembly instructions into machine code.
- A disassembler can decode machine code into assembly instructions.
4.3 Relationship Between Machine Code and the CPU
When the CPU executes machine code, it processes instructions according to the rules defined by the processor architecture.
A typical instruction execution process can be abstracted as:
flowchart TB
A["Fetch instruction<br/>Fetch"] --> B["Instruction decoding<br/>Decode"]
B --> C["Execute instruction<br/>Execute"]
C --> D["Access registers or memory"]
D --> E["Write back execution result"]
E --> F["Continue to the next instruction"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
This is a simplified abstract model for ease of understanding. Modern CPUs may use mechanisms such as pipelining, out-of-order execution, branch prediction, and multiple issue; the actual execution process is far more complex than the steps above.
5. From Source Code to Machine Code: The Complete Conversion Process
In the traditional native compilation model, source code usually goes through stages such as preprocessing, compilation, assembly, and linking.
5.1 Preprocessing
Preprocessing usually occurs in the compilation flow of languages such as C and C++.
It mainly handles:
- Macro expansion.
- Header file inclusion.
- Conditional compilation.
- Preprocessor directive handling.
For example:
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int size = MAX_SIZE;
return 0;
}
After preprocessing, the macro MAX_SIZE is replaced with 100, and the contents of header files are also expanded according to preprocessing rules.
You can use GCC to view the preprocessing result:
gcc -E main.c -o main.i
5.2 Compilation
The compiler converts the preprocessed source code into a form closer to the target processor.
The compilation process usually includes:
- Lexical analysis.
- Syntax analysis.
- Semantic analysis.
- Intermediate representation generation.
- Optimization.
- Target code generation.
Compilers usually use an intermediate representation (IR) internally to facilitate analysis and optimization.
5.3 Assembly
The assembly stage converts assembly instructions into machine instruction encodings and generates an object file.
For example:
gcc -c main.s -o main.o
Where:
main.s: assembly code.main.o: object file.
5.4 Linking
The linker combines multiple object files and the required libraries, resolves symbol references and address relocations, and generates the final program.
For example:
gcc main.o -o app
The linking stage usually involves:
- Symbol resolution.
- Relocation.
- Library dependency handling.
- Organization of sections and segments.
- Executable file generation.
The complete process is as follows:
flowchart TB
A["Source code<br/>main.c"] --> B["Preprocessing<br/>Preprocessing"]
B --> C["Compilation<br/>Compilation"]
C --> D["Assembly code<br/>main.s"]
D --> E["Assembly<br/>Assembly"]
E --> F["Object file<br/>main.o"]
F --> G["Linking<br/>Linking"]
H["Other object files and libraries"] --> G
G --> I["Executable file"]
I --> J["Load into memory"]
J --> K["CPU executes machine instructions"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style J fill:#FFF5DE,stroke:#E8D29A,color:#334155
style K fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px