{"id":112419,"date":"2026-06-15T07:00:00","date_gmt":"2026-06-15T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112419"},"modified":"2026-06-15T20:29:26","modified_gmt":"2026-06-16T03:29:26","slug":"20260615-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260615-00\/?p=112419","title":{"rendered":"The time the x86 emulator team found code so bad that they fixed it during emulation"},"content":{"rendered":"<p>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&#8217;t know which processor this particular story applied to.)<\/p>\n<p>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.<\/p>\n<p>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 <a title=\"How do compilers ensure that large stack allocations do not skip over the guard page?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260311-00\/?p=112134\"> perform a stack probe to ensure that 64KB of memory is available<\/a>, then subtracting 65536 from the stack pointer, and then initializing the memory in a small, tight loop.<\/p>\n<p>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 &#8220;optimized&#8221; the code by unrolling the loop into 65,536 individual &#8220;write byte to memory&#8221; instructions, each 4 bytes long.<\/p>\n<p>All in all, it took this program 256 kilobytes of code to initialize 64 kilobytes of data.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Offensive content in the eyes of a software engineer.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-112419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Offensive content in the eyes of a software engineer.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=112419"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112419\/revisions"}],"predecessor-version":[{"id":112420,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112419\/revisions\/112420"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=112419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}