Maze ASM - Pure ARM64

assembly c game development algorithm

A maze game written entirely in ARM64 assembly for macOS. Navigate through randomly generated mazes using classic recursive backtracking.

The source code can be found on GitHub.

Demo Videos

About

This project demonstrates building a complete graphical application in pure assembly language without relying on high-level graphics libraries like SDL. Everything from window management to pixel rendering is implemented using native macOS frameworks called directly from assembly via the Objective-C runtime.

Why Assembly?

  • Learn how graphics really work at the lowest level
  • Understand CPU architecture and calling conventions
  • Direct interaction with the operating system
  • No abstraction layers hiding the details

Features

  • Recursive Backtracking Maze Generation - Creates perfect mazes with exactly one path between any two points
  • Software Rasterizer - Custom pixel-by-pixel drawing routines
    • Bresenham's line algorithm
    • Midpoint circle algorithm
    • Filled and outlined rectangles/circles
  • Native macOS Integration - No SDL or external graphics libraries
    • Cocoa window management via objc_msgSend
    • CoreGraphics for display and keyboard input
    • Direct system calls for console output
  • Smooth Controls - Movement cooldown for precise single-tile navigation

Controls

KeyAction
↑ / WMove up
↓ / SMove down
← / AMove left
→ / DMove right
RGenerate new maze
ESC / QQuit

Project Structure

maze_asm/
├── include/
│   └── constants.inc          # Shared constants and key mappings
├── src/
│   ├── maze.s                 # Main game loop and rendering
│   ├── shared/
│   │   ├── maze_gen.s         # Maze generation (recursive backtracking)
│   │   └── raster.s           # Software rasterizer (platform-independent)
│   └── platform/
│       └── macos/
│           ├── window.s       # Cocoa window management
│           ├── keyboard.s     # CoreGraphics keyboard input
│           ├── timing.s       # Frame timing (usleep)
│           └── print.s        # Console output (syscalls)
├── build/                     # Compiled output (build/maze)
├── Makefile
└── README.md

Maze Algorithm

The maze uses iterative backtracking (stack-based to avoid deep recursion):

  1. Start with a grid of walls
  2. Pick starting cell, mark as path
  3. Randomly shuffle directions (N, E, S, W)
  4. For each direction, if cell 2 steps away is unvisited:
    • Carve path to that cell
    • Push current position to stack
    • Move to new cell
  5. If no valid directions, pop from stack (backtrack)
  6. Repeat until stack is empty

This generates a "perfect maze" - exactly one path between any two points.

Technical Details

Native Implementation Stack

LayerImplementation
WindowCocoa (NSApplication, NSWindow, NSView, CALayer)
DisplayCoreGraphics (CGImage, CGDataProvider)
InputCoreGraphics (CGEventSourceKeyState)
DrawingCustom assembly (framebuffer manipulation)
Timinglibc usleep
ConsolemacOS syscall SYS_WRITE

ARM64 Calling Convention

Arguments:     x0-x7 (w0-w7 for 32-bit)
Return value:  x0
Callee-saved:  x19-x28
Frame pointer: x29
Link register: x30
Stack:         16-byte aligned

Graphics Primitives

The software rasterizer provides:

  • _raster_init / _raster_free - Framebuffer management
  • _raster_set_color - Set RGBA drawing color
  • _raster_clear - Fill framebuffer with current color
  • _raster_plot - Draw single pixel
  • _raster_line - Bresenham's line algorithm
  • _raster_rect / _raster_rect_outline - Rectangles
  • _raster_circle / _raster_circle_filled - Midpoint circle algorithm

Building

Requirements

  • macOS (Apple Silicon / ARM64)
  • Xcode Command Line Tools

Build & Run

make run

Why This Matters

Writing graphics applications in pure assembly provides deep insights into:

  • CPU Architecture: Understanding registers, calling conventions, and memory layout
  • Operating System Interfaces: Direct interaction with system calls and frameworks
  • Graphics Fundamentals: Pixel manipulation, rasterization algorithms, and framebuffer management
  • Performance Optimization: Every instruction matters when you're writing them by hand

This project proves that even in 2024, it's possible to create engaging graphical applications using nothing but assembly language and the raw power of the CPU.

A complete maze game implemented in pure ARM64 assembly, demonstrating low-level graphics programming, maze generation algorithms, and native macOS system integration without any high-level libraries.



Projects

Site

Games

Tags