CD Notes
Learn about assembly language, assembler functions, machine code generation, and assembly instruction set details
The assembler is a system software that translates assembly language code into machine code (binary). Unlike compilers which translate high-level languages, assemblers perform almost a one-to-one translation from symbolic assembly to machine instructions.
What is an Assembler?
An assembler is a program that converts assembly language into executable machine code. Assembly language uses mnemonic instructions (like MOV, ADD, JMP) that are more human-readable than raw binary machine code. The assembler translates these mnemonics into their binary equivalents.
Assembly Language Basics
Assembly language is specific to processor architectures. Here's a simple x86 example:
; Simple x86-64 assembly
section .data
message db "Hello", 0
section .text
global _start
_start:
MOV EAX, 1 ; System call: sys_exit
MOV EDI, 0 ; Exit code: 0
SYSCALL ; Call kernelCommon Assembly Instructions
| Instruction | Purpose | Example |
|---|---|---|
| MOV | Move/Copy data | MOV EAX, 10 |
| ADD | Addition | ADD EAX, EBX |
| SUB | Subtraction | SUB EAX, 5 |
| MUL | Multiplication | MUL EBX |
| DIV | Division | DIV ECX |
| JMP | Unconditional jump | JMP label |
| JE | Jump if equal | JE target |
| CALL | Function call | CALL function |
| RET | Return from function | RET |
| PUSH | Push to stack | PUSH EAX |
| POP | Pop from stack | POP EAX |
Assembler Functions and Workflow
Two-Pass Assembler
Most assemblers use two passes:
Assembler Process Diagram
Assembly Language Concepts
Labels and Symbols
Labels mark memory locations for jumps and data references:
Addressing Modes
Different ways to specify operands:
Symbol Table in Assemblers
The symbol table maintains mapping of labels to memory addresses:
| Symbol | Type | Address | Size |
|---|---|---|---|
| main | label | 0x401000 | - |
| count | data | 0x402000 | 1 byte |
| buffer | data | 0x402001 | 256 bytes |
| loop_start | label | 0x401010 | - |
Example: Simple Assembly Program
High-Level C Code:
int add(int a, int b) {
return a + b;
}
int main() {
int x = 5;
int y = 3;
int z = add(x, y);
return z;
}Assembly Code (x86-64):
section .text
global add
global main
add:
; a is in EDI, b is in ESI (x86-64 calling convention)
MOV EAX, EDI ; EAX = a
ADD EAX, ESI ; EAX = a + b
RET
main:
PUSH RBP
MOV RBP, RSP
MOV EDI, 5 ; First arg (x)
MOV ESI, 3 ; Second arg (y)
CALL add ; Call add, result in EAX
MOV RSP, RBP
POP RBP
RETGenerated Machine Code (hex):
Assembler Output: Object Code
After assembly, the output is object code containing:
Assembler vs Compiler
| Aspect | Assembler | Compiler |
|---|---|---|
| Input | Assembly | High-level language |
| Output | Machine code | Assembly/Machine code |
| Complexity | Low | High |
| Optimization | Minimal | Extensive |
| Translation ratio | ~1:1 | Many high-level statements to few machine instructions |
| Portability | Architecture-specific | More portable |
Interview Q&A
Q: What is the difference between an assembler and a compiler? A: A compiler translates high-level languages to machine code or assembly, handling complex transformations. An assembler does nearly one-to-one translation from symbolic assembly to machine code.
Q: Why do assemblers need two passes? A: First pass builds the symbol table by scanning all labels and their addresses. Second pass generates code using this symbol table to resolve forward references.
Q: What is an object file? A: An object file is the output of an assembler containing machine code and metadata (symbol table, relocation information) that a linker uses to create executable programs.
Q: What are relocation entries? A: Relocation entries in object files tell the linker how to adjust addresses when linking multiple object files together.
Q: What is calling convention? A: Calling convention defines how arguments are passed to functions and where return values are placed. Different architectures have different conventions (x86-64: EDI/ESI for first two args, EAX for return).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for The Assembler.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, language, processing, system, assembler
Related Compiler Design Topics