8000 GitHub - whoizahm3d/snake-game-x86-asm: Classic Snake game written in x86 real-mode Assembly — runs in DOS VGA text mode with direct hardware rendering, BIOS interrupts, and zero external dependencies. · GitHub
[go: up one dir, main page]

Skip to content

About

Classic Snake game written in x86 real-mode Assembly — runs in DOS VGA text mode with direct hardware rendering, BIOS interrupts, and zero external dependencies.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

🐍 snake-x86-asm

A fully functional Snake game built from scratch in x86 Real-Mode Assembly, running in DOS VGA text mode — no libraries, no OS abstractions, just raw hardware control.


📖 Overview

This project is a classic Snake game implemented entirely in x86 Assembly language (NASM/YASM-compatible Intel syntax). It targets the DOS real-mode environment and interfaces directly with VGA video memory, BIOS interrupts, and DOS system calls — making it a ground-up demonstration of low-level systems programming.

Originally developed as a university group project by:

  • Ali Ahmad — 23L-2619

🎮 Gameplay

  • Use the arrow keys to steer the snake
  • Eat the food (☺) that appears randomly on screen to grow longer and increase your score
  • The game speeds up slightly as your score increases
  • Colliding with yourself or the border ends the game
  • After Game Over, press 1 to restart or any other key to exit

✨ Features

  • Welcome screen with group member credits
  • Bordered play field drawn using VGA text characters (= and |)
  • Live score display in the corner
  • Progressive difficulty — snake moves faster after score reaches 5
  • Self-collision and wall-collision detection
  • Restart-without-relaunch support
  • Fully self-contained .com executable (no external dependencies)

🛠️ Technical Highlights

Direct VGA Text-Mode Rendering

All drawing is done by writing directly to video memory at segment 0xB800. Each character cell is a 2-byte word: one byte for the ASCII character and one byte for the color attribute. This bypasses BIOS/DOS entirely for rendering, making it extremely fast.

BIOS Timer Interrupt for Randomness (int 1Ah)

Random food positions are generated by reading the system clock tick counter via INT 1Ah. The tick count (stored in CX:DX) is divided by 25 and 80 to derive row and column coordinates, keeping food within the valid play area.

Keyboard Input via BIOS (int 16h)

  • AH=01h — non-blocking key poll (checks if a key is waiting)
  • AH=00h — blocking key read (used on the welcome and game-over screens)

Arrow key scan codes handled: 0x48 (↑), 0x50 (↓), 0x4B (←), 0x4D (→)

In-Memory Snake Position Tracking

Snake segment positions are stored as an array of word-sized VGA memory offsets (snake_locations). Each movement routine shifts the entire array, places the new head, and erases the old tail — all with direct memory writes.

Delay Loop for Speed Control

Game speed is controlled by a software countdown loop (delaytime). The delay value is set to 80000 ticks for lower scores and 120000 ticks for higher scores (the counter wraps at score ≥ 5), creating two speed tiers.

DOS Program Exit (int 21h)

  • AH=4Ch — terminate program cleanly and return to DOS

📁 Project Structure

snake-x86-asm/
├── snake.asm       # Full game source code (all logic, rendering, input, interrupts)
└── README.md       # This file

Note: The source file is provided as Snake game.txt and should be renamed to snake.asm before assembling.


🚀 Getting Started

Prerequisites

You need one of the following:

Tool Purpose
NASM Assembler (recommended)
YASM Alternative Intel-syntax assembler
DOSBox DOS emulator to run the .com file

Assembly & Running

1. Assemble with NASM:

nasm -f bin snake.asm -o snake.com

2. Run in DOSBox:

dosbox snake.com

Or copy snake.com into your DOSBox mounted directory and run it from the DOSBox prompt.


🖥️ System Requirements

Requirement Detail
Architecture x86 Real Mode (16-bit)
Video VGA Text Mode — 80×25, 16 colors
OS DOS or a DOS emulator (DOSBox, DOSEMU)
Format .COM flat binary (origin 0x0100)

🔍 Code Walkthrough

61E6
Routine Description
clearscreen Fills all 4000 bytes of VGA memory with spaces (char 0x20, attr 0x07)
welcomeMsg Renders the welcome screen with member names directly to VGA memory
draw_border Draws = on top/bottom rows and | on left/right columns
draw_snake Writes initial snake body characters to VGA and records their positions
move_snake_left/right/up/down Moves the snake one step, updates position array, erases tail, checks self-collision
check_death Checks if the snake head has hit any border cell
displayFood Uses INT 1Ah to pick a random valid cell and renders a food character (0x01)
printScore Writes Score: X to a fixed VGA offset
play_game Main game loop — handles delay, reads input, dispatches movement, checks food eaten
over Displays Game Over screen, shows final score, prompts for restart
start Entry point — calls play_game, then exits via INT 21h

Display

Snake Game Display


📚 Learning Value

This project is a great reference for anyone learning:

  • x86 real-mode assembly and the COM executable format
  • Direct hardware-level video programming (VGA text mode)
  • BIOS interrupt usage (int 10h, int 16h, int 1Ah)
  • DOS interrupt usage (int 21h)
  • Stack-based calling conventions in assembly
  • Game loop design and collision detection without any standard libraries

📄 License

This project was developed for academic purposes. Feel free to use it as a learning reference.

About

Classic Snake game written in x86 real-mode Assembly — runs in DOS VGA text mode with direct hardware rendering, BIOS interrupts, and zero external dependencies.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0