June 15th, 2026
heartmind blownlikecompelling13 reactions

The time the x86 emulator team found code so bad that they fixed it during emulation

During an exchange of war stories, a colleague of mine told one from back in the days when Windows included a processor emulator for x86-32 on systems that natively ran some other processor. (This has happened many times. And no, I don’t know which processor this particular story applied to.)

This particular emulator employed binary translation, generating native code to perform the equivalent operations of the original x86-32 code. This offered a significant performance improvement over emulation via interpreter. You can imagine that x86-32 is just a bytecode, and the emulator is a JIT compiler.

Anyway, my colleague found that there was one program that needed to allocate around 64KB of memory on the stack and initialize it. The standard way of doing this is to perform a stack probe to ensure that 64KB of memory is available, then subtracting 65536 from the stack pointer, and then initializing the memory in a small, tight loop.

But using a loop to initialize the memory was too mundane for whatever compiler was used to compile this code. Instead of generating a loop to initialize each byte of the buffer, the compiler “optimized” the code by unrolling the loop into 65,536 individual “write byte to memory” instructions, each 4 bytes long.

All in all, it took this program 256 kilobytes of code to initialize 64 kilobytes of data.

This offended the team so much that they added special code to the translator to detect this horrible function and replace it with the equivalent tight loop.

Topics

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

28 comments

Sort by :
  • Chris Quirke

    Speed, size, pick one… sounds like that code was optimized for speed?

    In Z80, we’d use runs of LDI rather than a spacially-concise LDIR when speed was of the esscance, as often the case in early 1980s.

    At the other extreme, I spent 3 weeks shrinking a 1027-byte program to fit the ZX Spectrum print buffer. Instead of an if-then logic, I modified code before execute.

    Prolly not an approved approach in the 21st…

  • Igor Levicki

    @Raymond Chen

    Come on Raymond, don’t leave us hanging, shame that compiler which did infinite loop unrolling?

  • Michael Brundage 6 days ago

    Thanks?

  • Josh Burna 6 days ago

    .. is it just me, or does *EVERY SINGLE ONE* or the comments – other than mine – absolutely scream “I was written by a shitty AI comment bot” or no??

    • Chris Iverson

      Says the guy using em-dashes in his writing.

      In all seriousness, though, I didn’t see anything that screams “AI” to me.

      There’s a bunch of technical stuff, but that’s to be expected on a post explicitly talking about technical details.

      • LB 5 days ago

        Microsoft’s blog software coverts normal dashes to emdashes. Take a guess why Microsoft would make that change.

    • IS4 6 days ago

      I wanted to write that I am surprised people in 2026 don’t understand how replies work… not that the comments seem somewhat AI-ish.

      • Chris Iverson

        Blame the commenting website software for that one.

        There’s been tons of reports of people’s comments getting added to the wrong thread, or not added to a thread at all, or being shown out-of-order.