Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Helios - Custom x86-64 Assembler and Static Linker

A custom x86-64 assembler and static linker implemented from scratch in C++17, targeting Linux ELF64.

Project Status

Completed

  • 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

Current Stage: Stage 1 - Lexer Testing

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.asm

Expected output: complete token stream with line numbers and token types.

Upcoming

  • 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)

Architecture

The project is organized into a multi-stage pipeline:

Stage 1: Lexical Analysis (Lexer)

  • 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

Stage 2: Parsing (Parser)

  • 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)

Stage 3: Two-Pass Assembly

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

Stage 4: ELF Object File Writer

  • 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

Stage 5: Linker

  • Loads multiple .o files
  • Resolves symbols across object files
  • Merges sections and assigns final virtual addresses
  • Patches relocations
  • Produces runnable ELF64 executables

Instruction Set Supported

Data Movement

  • mov, push, pop, lea

Arithmetic

  • add, sub, mul, imul, div, idiv, inc, dec, neg

Bitwise Operations

  • and, or, xor, not, shl, shr

Control Flow

  • jmp, je, jne, jl, jg, jle, jge, call, ret

Comparison

  • cmp, test

System Calls

  • syscall

Directives Supported

  • section .text / .data / .bss - section selection
  • global <symbol> - export symbol
  • extern <symbol> - import symbol
  • db, dw, dd, dq - define initialized data (1, 2, 4, 8 bytes)
  • resb, resw, resd, resq - reserve uninitialized space
  • equ - symbolic constant definition

Register Support

64-bit

RAX, RBX, RCX, RDX, RSI, RDI, RSP, RBP, R8-R15

32-bit (zero-extends upper 32 bits)

EAX, EBX, ECX, EDX, ESI, EDI, ESP, EBP, R8D-R15D

16-bit

AX, BX, CX, DX, SI, DI, SP, BP, R8W-R15W

8-bit

AL, AH, BL, BH, CL, CH, DL, DH, SIL, DIL, SPL, BPL, R8B-R15B

Building

Prerequisites

  • CMake 3.10+
  • C++17 compatible compiler (g++, clang++)
  • Linux environment (WSL2 or native)

Build

./build.sh

Or manually:

mkdir -p build
cd build
cmake ..
cmake --build .

Testing

Stage 1: Lexer Testing

./build/test_lexer tests/hello.asm

Project Files

helios/
├── 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

X86-64 Encoding Notes

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.)

Error Handling

All errors are printed to stderr and exit with code 1:

[Stage] Error at line N: message

References

  • x86-64 ISA Manual (Intel)
  • ELF64 Specification
  • System V AMD64 ABI

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages