16 bytes of x86 that draw Sierpinski rain and play it as sound
A demoscene release at Outline 2026 packs an infinite Sierpinski fractal generator and a PC speaker synthesizer into 16 bytes of real-mode DOS assembly. The routine sets video mode 0, points the data segment at the CGA text buffer at 0xB800, then loops: load a byte, step backward 56 bytes, XOR the accumulator into memory, and dump the result to port 61h. The BIOS-cleared text buffer provides a uniform starting canvas of 0x20/0x07 pairs, which is essential — predictable initial state is what lets the math produce a clean fractal rather than noise.
The core trick is recognizing that an additive prefix sum across memory generates binomial coefficients mod 256, and swapping the add for XOR collapses that to Rule 60, the elementary cellular automaton that draws Sierpinski’s triangle. Because bit 1 of port 61h directly drives the PC speaker cone, and the XOR sequence only ever toggles bit 1, the fractal’s geometry literally becomes the speaker waveform — denser regions produce square-wave tones, empty triangles produce silence. The remaining seven bits scramble into pseudo-random ASCII glyphs for the visual, and harmlessly ride along to the port.
The -56 byte stride (rather than a clean 16) is deliberate: it doubles the macro-cycle to 8,192 iterations, dropping the audio an octave, and shears the rendered fractal into ten evenly spaced ascending columns across the 40-column screen. It’s a tight demonstration of how a single arithmetic identity — Lucas’s theorem linking binomial coefficients to XOR — can be exploited so that one byte stream simultaneously satisfies the visual and audio outputs of a 16-byte program.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.