我用512字节的引导扇区写了一个乒乓游戏。
I wrote a Pong game in a 512-byte boot sector

原始链接: https://akshatjoshi.com/i-wrote-a-pong-game-in-a-512-byte-boot-sector/

阿克沙特·乔希挑战自己,创作一个完全可运行的 Pong 游戏,使其能装入单个软盘启动扇区——仅 510 字节的代码!这个项目并非旨在创建一个可用的操作系统,而是关于基于极端约束的编程。 这款游戏使用原始 x86 汇编编写,绕过任何操作系统、驱动程序或库,直接使用 BIOS 中断操作视频内存 (0xB800)。它具有使用 W/S 键控制的玩家挡板、CPU 对手、计分、颜色切换(C 键)和重置功能(R 键)。 主要技术成就包括通过 `stosw` 直接访问视频内存、高效的屏幕定位计算、使用 BIOS 中断 0x16 实现的非阻塞键盘输入,以及一个简单、确定性的球体物理引擎。整个游戏在 GitHub 上可用,可以使用 QEMU 或在较旧的硬件上运行,展示了紧凑编码的非凡壮举。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 我用512字节的引导扇区写了一个乒乓游戏 (akshatjoshi.com) 9点 由 akshat666 1小时前 | 隐藏 | 过去 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文

written by - Akshat Joshi

Sometime back, I set myself a crazy constraint:


Load only one thing at boot - A Pong game that fits in one floppy disk sector — 512 bytes.

Why would I build this?

I have played with Operating Systems and their workings by customizing them and tweaking their behavior. But this was about pushing constraints to the extreme. Can I have an OS which just loads one PONG GAME ?

The challenge

  • No operating system.
  • No drivers.
  • No libraries.
  • Just raw x86 assembly, BIOS interrupts, and the video memory at 0xB800.

The boot sector is the first 512 bytes a computer loads from disk. It has to end with the magic bytes 0x55 0xAA — leaving only 510 bytes for actual code.
I wanted to see if I could fit:

  • Player paddle (W/S keys)
  • CPU opponent
  • Ball with velocity
  • Scoring
  • Color toggle (C key)
  • Full reset (R key)

After learning about Operating systems and the bootup flow, I finally managed to get a pong game running 😄

How it works!

The game runs in 80x25 text mode (VGA mode 03h) using BIOS interrupt 10h.
Everything is drawn directly into video memory (0xB800) with stosw — of course no graphics libraries here.

Controls

W / S — Move your paddle
C — Cycle paddle and midline colors
R — Reset game (reboots the sector)

It’s a deterministic ball-tracking logic. It checks the ball’s Y position every frame:

  • If ball is above paddle → move up
  • If below → move down
  • Never goes off-screen

Simple. Fast. Fits in 510 bytes.

Demo run

Key Technical Highlights

  • Video Memory Direct Access Used ES:DI = 0xB800:0000 and rep stosw to clear screen in one instruction.
  • Efficient Positioning - imul di, [playerY], 160 → converts row to video offset (80 cols × 2 bytes per char).
  • Real-Time Input - BIOS int 0x16 with ah=1 (peek) → non-blocking keyboard check.
  • Physics & Collision - Ball velocity stored in single bytes (ballVeloX, ballVeloY). Reversed on wall/paddle hit using neg byte.
  • Delay Loop - Used BIOS timer at 0x46C for frame pacing — no hlt, no busy-wait burn.
  • Color Cycling - C key increments drawColour by 0x10 → rotates through 16 background colors.

The Full Code

The entire game is in one file: pong.asm
Publicly available on GitHub:
https://github.com/akshat666/-bootponggame

Run It Yourself

  1. Clone the repo
  2. Assemble with NASM
  3. Run in QEMU (or burn to USB and boot on old hardware)
联系我们 contact @ memedata.com