A custom x86-64 assembler and static linker implemented from scratch in C++17, targeting Linux ELF64.
- Project structure and CMakeLists.txt setup
- common.h with all required data structures (Token, Symbol, Relocation, etc.)
- Register information module (registers.cpp)
- Lexer (lexer.h/lexer.cpp) - complete tokenization of x86-64 assembly
The Lexer is fully implemented and ready to test. To test it:
# Build the project
./build.sh
# Test the lexer with hello.asm
./build/test_lexer tests/hello.asmExpected output: complete token stream with line numbers and token types.
- Parser implementation (parser.h/parser.cpp)
- Pass 1 - Symbol table construction (assembler.h/assembler.cpp)
- Pass 2 - Code emission + ELF object file writer (elf_writer.h/elf_writer.cpp)
- Linker implementation (linker.h/linker.cpp)
The project is organized into a multi-stage pipeline:
- Tokenizes .asm source file into structured tokens
- Recognizes mnemonics, registers, directives, literals, labels, and punctuation
- Tracks line numbers for error reporting
- Strips comments during tokenization
- Processes token stream into structured ParsedLine objects
- Classifies lines as: LabelDef, Instruction, Directive, or Blank
- Parses operands into typed Operand variants (Register, Immediate, Memory, Label)
Pass 1 - Symbol Table Construction:
- Walks through all parsed lines sequentially
- Maintains location counter per section (.text, .data, .bss)
- Records label definitions and equ constants
Pass 2 - Code Emission: -Encodes instructions to machine code
- Emits relocations for external references
- Builds relocation table
- Constructs valid ELF64 relocatable object files (.o)
- Emits proper section headers, symbol tables, and string tables
- Compatible with gcc-produced object files and GNU tools
- Loads multiple .o files
- Resolves symbols across object files
- Merges sections and assigns final virtual addresses
- Patches relocations
- Produces runnable ELF64 executables
mov,push,pop,lea
add,sub,mul,imul,div,idiv,inc,dec,neg
and,or,xor,not,shl,shr
jmp,je,jne,jl,jg,jle,jge,call,ret
cmp,test
syscall
section .text / .data / .bss- section selectionglobal <symbol>- export symbolextern <symbol>- import symboldb, dw, dd, dq- define initialized data (1, 2, 4, 8 bytes)resb, resw, resd, resq- reserve uninitialized spaceequ- symbolic constant definition
RAX, RBX, RCX, RDX, RSI, RDI, RSP, RBP, R8-R15
EAX, EBX, ECX, EDX, ESI, EDI, ESP, EBP, R8D-R15D
AX, BX, CX, DX, SI, DI, SP, BP, R8W-R15W
AL, AH, BL, BH, CL, CH, DL, DH, SIL, DIL, SPL, BPL, R8B-R15B
- CMake 3.10+
- C++17 compatible compiler (g++, clang++)
- Linux environment (WSL2 or native)
./build.shOr manually:
mkdir -p build
cd build
cmake ..
cmake --build ../build/test_lexer tests/hello.asmhelios/
├── CMakeLists.txt Build configuration
├── build.sh Build script
├── README.md This file
├── src/
│ ├── common.h Shared data types
│ ├── registers.cpp Register information functions
│ ├── lexer.h / lexer.cpp Lexical analysis
│ ├── parser.h / parser.cpp Parsing
│ ├── assembler.h / assembler.cpp Two-pass assembly
│ ├── elf_writer.h / elf_writer.cpp ELF file writing
│ ├── linker.h / linker.cpp Linking
│ ├── main.cpp CLI driver
│ └── test_lexer.cpp Lexer test driver
└── tests/
├── hello.asm Test program - Hello, World!
├── arithmetic.asm Arithmetic operations test
├── functions.asm Call/return test
└── multifile/ Multi-file linking test
├── main.asm
└── utils.asm
The assembler implements proper x86-64 encoding including:
- REX prefixes for 64-bit and extended registers
- ModRM bytes for addressing modes
- SIB bytes for complex memory addressing
- Displacement encoding (1 or 4 bytes)
- Proper relocation types (R_X86_64_64, R_X86_64_PC32, etc.)
All errors are printed to stderr and exit with code 1:
[Stage] Error at line N: message
- x86-64 ISA Manual (Intel)
- ELF64 Specification
- System V AMD64 ABI