Compiler: Turns high-level source code into machine code.
Assembler: Encodes assembly code into machine code.
Note: Javac
- Java is special in that it compiles into bytecode for the JVM rather than a particular CPU’s machine code.
- “Write once, run everywhere”
Linker: Combine object files into an executable.
-lm
in gcc to link the math library for a C program.Loader: Brings executable into memory and starts execution.
Example: Manually calling the compiler, assembler, and linker
# Compiler gcc -S sum.c # Assembler as -o sum.o sum.s # Linker ld -o sum sum.o # Execute (Uses Loader) ./sum
- Etymology: The linker is unintuitively named
ld
because it stands for “link editor”
\text{Assembly Language} \to \text{(Assembler)} \to \text{Machine Language}
Assembly Language: Symbolic representation of machine language.
Machine Language: Set of instruction codes used to represent operations and date.
When to Use Assembly:
- Speed and size is critically important
- To use specialized instructions not available in high-level languages
- e.g., INT, single instruction loop, SIMD, etc.
- Platform only supports assembly.
Assembly Drawbacks:
- ISA dependent (architecture dependent)
- Not portable
- Longer than high-level language
- No high-level constructs
- Hard to read.
- This is why good comments are important.
More on Assembly:
- A programming language
- Provides only a few, simple types of data and control flow
- Typically no types.
- Programmers define the type
- All control flow implemented via go to.
- Difficult and error-prone
\text{Assembler's Job: } \text{Source} \to \text{Object}
Assembler: Translate a file of assembly language statements (source) into a file of binary machine instructions and binary data (object).
Two Major Parts
Object File Contains: