Skip to content

This is a complete single-pass compiler written in x86-64 assembly that translates arithmetic expressions into executable x86-64 assembly code

Notifications You must be signed in to change notification settings

0u44/SinglePass-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Includes;

  1. Lexer: Tokenizes input into numbers, operators (+, -, *, /), and parentheses
  2. Parser: Recursive descent parser implementing operator precedence (*, / before +, -)
  3. Code Generator: Emits x86-64 assembly instructions directly

Tokenization: get_next_token scans characters, classifies them into token types, and handles numeric parsing with decimal-to-binary conversion.

Parsing: Three-level precedence hierarchy:

  1. parse_expression: Handles addition/subtraction (lowest precedence)
  2. parse_term: Handles multiplication/division
  3. parse_factor: Handles numbers and parenthetical expressions (highest precedence)

Code Generation: Emits assembly using register rax for computation:

  1. Numbers: mov rax, immediate
  2. Addition: add rax, operand
  3. Multiplication: imul rax, operand
  4. Division: mov rdx, 0; mov rbx, divisor; idiv rbx

Uses fixed buffers for input (256 bytes), output (2048 bytes), and token storage. Output pointer tracks current write position.

Usage:

  1. Compile with nasm -f elf64 compiler.asm && ld compiler.o
  2. Run ./a.out, input expression like 2+3*4, outputs complete executable assembly
  3. The generated code can be assembled and run independently

Warning

Handles positive integers only, basic error handling, and generates straightforward register-based code without optimization. Real compilers would add symbol tables, type checking, register allocation, and optimization passes.

About

This is a complete single-pass compiler written in x86-64 assembly that translates arithmetic expressions into executable x86-64 assembly code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published