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.
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
- 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
- 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
.comexecutable (no external dependencies)
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.
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.
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 (→)
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.
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.
AH=4Ch— terminate program cleanly and return to DOS
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.txtand should be renamed tosnake.asmbefore assembling.
You need one of the following:
| Tool | Purpose |
|---|---|
| NASM | Assembler (recommended) |
| YASM | Alternative Intel-syntax assembler |
| DOSBox | DOS emulator to run the .com file |
1. Assemble with NASM:
nasm -f bin snake.asm -o snake.com2. Run in DOSBox:
dosbox snake.com
Or copy snake.com into your DOSBox mounted directory and run it from the DOSBox prompt.
| 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) |
| 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 |
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
This project was developed for academic purposes. Feel free to use it as a learning reference.