After this piece of code has been lying around for way too long, it is finally time to publish it. Back in 2019, I spent a weekend writing a small game that fits in one boot sector. A boot sector is 512 bytes in size, with 2 bytes being a magic word. In the end, there are 510 bytes of plain memory left for the program code itself. With that restriction, there must be every possible measure taken to minimize the binary size.

To name just a few of those measures: Complex instructions are used to lower the instruction count. Prologs and epilogs of function calls are removed if possible and jumps are preferred over calls. Variables are placed in the screen memory of the graphics card. Thus, the state of the variables is visible in the top right corner as flashing pixels. There are also algorithmic tricks used that result in less size: Once the ball hits a brick, one pixel is colored in the background color. Then, an algorithm searches for black pixels in brick areas. If such a pixel is found, the brick is re-colored in another color code. Luckily, there are two different color codes available for the color black. This trick lets the code distinguish between the background and a dead brick without being visible on the screen anymore.

In the end, a somewhat playable game was the result. Unluckily, there is no randomness. The ball takes the same trajectory each time. BUT … the thing is bootable without an OS which was great fun developing.

For the full code have a look at my GitHub:

  mov ax, 0x0013      ; VGA 320x200 256 color mode (bottom line for vars)
  int 0x10
  mov ax, 0xa000      ; set segments
  mov ds, ax          ; video segment
  mov es, ax
  cld                 ; and some basic adjustments

  mov cx, BRICKS_NR   ; init this number of bricks  
  mov ax, BK_LIVE_COLOR ; ... with color: living
  xor di, di
  rep stosb

; ...
; ...
; ...