Skip to content
Tech News
← Back to articles

AttoChess, a complete, playable chess program for 16-bit x86 DOS in 278 bytes

read original more articles
Why This Matters

AttoChess demonstrates how optimizing core functions like board rendering and move decoding can significantly reduce the program size to just 278 bytes on 16-bit x86 DOS systems. These innovations not only make the chess engine more efficient but also showcase the potential for highly compact software in constrained environments, inspiring developers to rethink traditional rendering and processing methods.

Key Takeaways

05 Optimizations

AttoChess keeps the original's core search verbatim. All ten bytes come from rethinking the two things around the search, how the board is drawn and how the move is decoded, plus one change inside the search loop that frees a register.

01 · Display The board draws itself: no render buffer, no int 21h/09h This accounts for the largest saving. The original renders the position into a separate buffer: it walks the board, transforms each square into a printable character, stores it, appends a $ terminator, and prints the whole string with DOS function 09h ( int 21h ). That path costs a buffer pointer setup, the copy loop's store, the terminator write, and, because the buffer lives after the board, a reserved board array in the image. AttoChess deletes all of it. The board's border columns are laid out as CR, LF, CR, LF ( 0Dh, 0Ah, 0Dh, 0Ah ) instead of the previous 08h filler. Both CR and LF still have bit 3 set, so every existing border/color mask test fires exactly as before, but now the raw board bytes are already a printable frame. The display loop streams each byte straight to the console with int 29h (DOS fast console output): borders become newlines, empty squares become NULs, and only real pieces take the ASCII transform. main_loop: mov si, offset board_db + 24 ; row 2 (black back rank), col 0 mov cl, 98 ; 8 rank rows + final CR,LF (CH=0) disp_loop: lodsb ; read square contents test al, 30h ; piece? jz disp_cont ; no: emit raw (CR / LF / NUL) inc ax ; zero-align king and al, 27h ; isolate piece type + black/lowercase bit add al, 4Bh ; K, N, B, P, Q, R (upper/lower by color) disp_cont: int 29h ; fast console output of AL loop disp_loop Removed in one move: the render buffer, its pointer setup, the $ terminator, the int 21h / 09h string print, and the reserved board array in the file image.

02 · Startup No BIOS mode-set; deterministic startup instead The original opens with int 10h to force BIOS display mode 0. AttoChess drops it and instead makes its two genuine entry assumptions explicit, which is both smaller overall and correct regardless of how the program is launched: start: cld ; DF is not guaranteed clear at entry mov cx, 13 ; row count (entry CX is not guaranteed) Streaming through int 29h needs no particular video mode, so the mode-set is not required.

03 · Input The input decoder folds every constant into one base address Reading a move means turning two typed characters (a file and a rank) into a board address. The original does this in stages: read the file char, add it, read the rank char, mask it down with and al, 0Fh , load 12 into ah , mul to get the row offset, and subtract. AttoChess collapses the arithmetic by pre-folding the ASCII bias constants directly into the base address and letting 16-bit pointer math wrap around mod 64K. The normalization step and the separate multiply setup both disappear: read_sub: mov bp, di mov di, offset board_db + 123 + 0CE0h ; base pre-folds the ASCII offsets mov ah, 01h int 21h ; read file char add di, ax ; AX = 0100h + file char int 21h ; read rank char imul ax, 12 ; AX = 12 * (0130h + rank digit) sub di, ax ; land on the target square imul ax, 12 (an 80186 immediate-form multiply) replaces the mov ah,12 + mul ah pair, and the wrap-around base makes the explicit and al, 0Fh input mask unnecessary.

04 · Search The source loop frees CX, so depth is never reloaded Inside the recursive search, the original scans candidate source squares with a counted loop ( mov cl, 92 ... loop src_loop ). That reuses CX as the loop counter, which clobbers the search depth living in CX, so on every recursive call it must re-read the depth back off the stack frame ( mov cx, [si + 32] ) before decrementing it. AttoChess walks the source squares by comparing the pointer to the end of the board instead: src_cont: inc bp cmp bp, offset board_db + 120 ; past the last square? jnz src_loop CX is never touched, so it stays as the live depth counter for the whole scan. The recursive call site then does dec cx directly, and the stack reload of depth is gone entirely.