WIN87EM.DLL 的數學例外處理程式碼為何如此混亂
Code Smells of Desperation (2023)
OS/2 Museum 作者檢視 Windows 3.1 的數學模擬與 FPU 支援程式庫 WIN87EM.DLL 後,指出其 IRQ 13 數學錯誤處理常式充斥重複操作,像是多次清除錯誤狀態與執行減速指令,呈現出為了讓程式在特定硬體上運作而反覆修改的痕跡。
AI 伴隨問答
針對這篇報導有疑問?可直接點選快捷問題或向 AI 追問背景脈絡與技術細節:
This Code Smells of Desperation | OS/2 Museum
OS/2, vintage PC computing, and random musings

- OS/2 History
- * OS/2 Beginnings
- * OS/2 1.0
- * OS/2 1.1
- * OS/2 1.2 and 1.3
- * OS/2 16-bit Server
- * OS/2 2.0
- * OS/2 2.1 and 2.11
- * OS/2 Warp
- * OS/2 Warp, PowerPC Edition
- * OS/2 Warp 4
- * OS/2 Timeline
- * OS/2 Library
- * OS/2 1.x SDK
- * OS/2 1.x Programming
- * OS/2 2.0 Technical Library
- DOS History
- * DOS Beginnings
- * DOS 1.0 and 1.1
- * DOS 2.0 and 2.1
- * DOS 3.0, 3.1, and 3.2
- * DOS 3.3
- * DOS 4.0
- * DOS Library
Learn Something Old Every Day, Part VIII: RTFM →
This Code Smells of Desperation
Posted on[August 5, 2023](https://www.os2museum.com/wp/this-code-smells-of-desperation/ "3:24 pm")by[Michal Necasek](https://www.os2museum.com/wp/author/michaln/ "View all posts by Michal Necasek")
A few weeks ago I had the questionable pleasure of diving into the math exception handler of WIN87EM.DLL, the Windows 3.1 math emulator and FPU support library. Actually WIN87EM.DLL appears to have been first shipped with Windows 3.0, and the version in Windows 3.1 is more or less identical. The version shipped with Windows 3.11 is byte for byte identical to the one in Windows 3.1.
The main function of WIN87EM.DLL is, as the name suggests, an x87 floating-point emulator. It appears to be an outgrowth of the math emulation packages shipped with many Microsoft languages. But even on a system with x87 hardware, WIN87EM.DLL has some work to do.
Namely WIN87EM.DLL intercepts math errors (exceptions). Depending on the system type, WIN87EM.DLL hooks either NMI vector 2 (PC and PC/XT) or IRQ 13 (PC/AT and compatibles).
The math interrupt handler in WIN87EM.DLL is very, _very_ strange. It bears all the hallmarks of code that was written, rewritten, rewritten again, hacked, tweaked, modified, and eventually beaten into submission even if the author(s) had no real idea _why_ it finally worked.
Now, x87 math error handing is a very tricky subject where numerous details changed over FPU and CPU generations, and Intel had a fair share of bugs in this area. But the code in WIN87EM.DLL looks very much like the result of changes made in desperation until it worked _somehow_, even though the changes made little or no sense.
What WIN87EM.DLL Does
The IRQ 13 handler in WIN87EM.DLL performs the following steps:
- Disable interrupts (CLI)
- PUSH and POP the AX register 70 times, maybe it will slow things down
- Write zero to I/O port F0h to clear the PC/AT FPU error latch
- Mask a selection of interrupts
- Write an EOI to the master interrupt controller (but not slave)
- Execute the FNSTSW instruction to store the FPU status word (but only that)
- PUSH and POP the AX register 16 times, because speed kills
- Write zero to I/O port F0h again, in case it didn’t work the first time
- Execute the FNCLEX instruction to clear pending FPU exceptions
- Write zero to I/O port F0h again, because third time’s the charm
- PUSH and POP the AX register 16 times, because it was so much fun last time
- Execute the FNCLEX instruction again, just to be really sure
- PUSH and POP the AX register 16 times, because it’s the thing to do
- Write zero to I/O port F0h again, because three times might not have been enough
- Finally jump to code that doesn’t look crazy
The not so crazy looking code executes FNSTENV to store the FPU environment, swaps in the previously saved status word (which was subsequently changed by FNCLEX), examines the code at the stored FPU error pointer to see if instruction prefixes should be skipped (that is where things may crash), enables interrupts with STI, and finally jumps to common code that’s executed on both PCs and ATs and handles the actual math error.
All in all, the math error interrupt handler in WIN87EM.DLL makes very little sense. I am extremely doubtful that, for example, writing to I/O port F0h four times does anything useful, or that executing FNCLEX twice is better than doing it once. However, it is entirely possible that the extra time it takes might do something. Likewise the slowdown loops that push and pop AX are very unlikely to be necessary, but may have done something seemingly useful on some particular system.
The code really does look like a desperate attempt to make things work, and there is some possibility that it is the result of trying to support broken hardware. Other similar error handlers I looked at don’t appear nearly as convoluted and are much more “by the book”.
We’ll probably never know why the code ended up so crazy, but we can speculate.
This entry was posted in Bugs, Microsoft, PC architecture, x87. Bookmark the [permalink](https://www.os2museum.com/wp/this-code-smells-of-desperation/ "Permalink to This Code Smells of Desperation").
Learn Something Old Every Day, Part VIII: RTFM →
22 Responses to _This Code Smells of Desperation_
1. Geoff Arnold says: August 5, 2023 at 6:31 pm This kind of thing triggers all sorts of flashbacks from 40 years ago….
2. crazyc says: August 5, 2023 at 7:04 pm I spent time digging though this while trying to get fpu error handing to work in winevdm with visual basic (which expects a precise stack layout because it installs a handler that never returns to win87em). The origin of the fpu emulator seems to go back to the earliest microsoft c compilers and it supports two different register allocation methods one where it would try to keep everything in the x87 stack and one where it would spill to memory. This means it has to keep the invalid exception always enabled so it can catch a stack over/underflow and handle it properly. This also means it has to potentially replay any fault so it needs to get the addresses from fsave which is impossible on newer intel cpus thanks to fcs/fds deprecation. Thankfully all win16 programs i’ve seen use the keep in fpu registers allocation method so it can be ignored entirely in winevdm.
3. Michal Necasek says: August 5, 2023 at 8:08 pm Do tell.
4. Michal Necasek says: August 5, 2023 at 8:18 pm It’s probably older than Microsoft C. MS Pascal 3.0 had an emulation library, and that came out in early 1983 or late 1982. Well before MS C 3.0 (the first C compiler written at Microsoft).
I wonder if some of the code goes even to MS BASIC. Maybe not, or probably not exactly, since the oldest MS tools used a different format for binary numbers. MS Pascal 3.0 used IEEE 784 format and supported also real 8087. MS Pascal 2.0 AFAICT did not support emulation or a real 8087. Not too surprising since the 8087 was not available in the early PC days.
And yes, old MS languages used an “infinite” FPU stack which may trigger exceptions during normal operation. I don’t believe that was used anymore by the late 1980s or so.
5. Richard Wells says: August 6, 2023 at 8:45 am MSBASIC incorporated 8087 support in 1987 with QuickBASIC 3 and QuickBASIC 4 defaulted to using the 8087 code along with an emulator. So BASIC got it after C and probably was using a variation on the C version of the 8087 emulator. BASIC 6 switched back to including something similar to the pre-8087 floating point libraries since those were faster than the 8087 emulator.
There were a number of third party libraries that did interface MS BASIC with 8087 but I never used one and information on them does not turn up in a quick search. The Startz book shows some techniques for incorporating ASM routines to interface with 8087 which look cumbersome. Makes me happy I stuck to FORTRAN for most of the code that used numeric coprocessors back in the 80s.
I haven’t found the WIN87EM code but I suspect that something has to replace the floating WAITs that require an installed 8087 to respond.
6. Williamsays: August 6, 2023 at 4:31 pm Haha It’s nostalgic to read such post. Reminds me of coding in a 1990s desktop.
7. John R Sundmansays: August 6, 2023 at 6:06 pm You may be amused by my little novella Cheap Complex Devices, which contains a long short story called “Bees, or The Floating Point Error”.
In fact, on one of several possible interpretations of the authorship of Cheap Complex Devices, the whole novella may be the output from the brain of a comatose computer designer, as processed by a somewhat sentient, and definitely bug-infested floating point processor. In any event you’ll find some geeky jokes about floating point errors that only true floating point error obsessives can appreciate.
I won’t spam you with links but the book can be found for purchase (with lots of glowing reviews) on the usual sites. It’s also for free download here: https://dl.bookfunnel.com/1e7142jomu
8. Peter Lund says: August 6, 2023 at 8:44 pm Seems to me it is trying really, really hard to not use FWAIT. This is the kind of code I would write in case the 8028780286 connection was unreliable (but reliable enough that ordinary 80286 code worked). Were there common problems with 80287 chips that were unreliable or just not socketed correctly?
9. no one in particular says: August 6, 2023 at 11:21 pm The comments there do smell of desperation (you can find the source for this handler by googling for “emoemwin.asm”, and then see __fpIRQ13) but there are some interesting bits like
“486 bug – must wait till after last “out f0″ to clear fp exceptions or IGNNE# will be permanently active.”
Given that Microsoft was on the forefront on the early stepping 386 and 486 weirdness it could be more than just nonsense
10. Michal Necasek says: August 7, 2023 at 12:38 pm I think that although the finished code looks very much like nonsense, it most likely did solve some poorly understood problem. And yes, it may well have been a problem on pre-release hardware that was itself buggy. The 486 with a built-in FPU required different motherboard support circuitry compared to the older CPUs/FPUs. It’s fairly certain that Microsoft had pre-release 486 systems and it’s likely such systems had “interesting” bugs.
Intel made changes to the FERR# and IGNNE# documentation in the early revisions of the 486 datasheet, which suggests that the documentation was if not incorrect then poorly understood.
The source code also illustrates a major problem with such code: It doesn’t explain what problem _exactly_ it’s trying to solve. That means the next programmer can’t easily ditch such nonsensical looking code because they can’t be sure that it isn’t solving some relatively widespread and legitimate problem. Testing won’t help too much because it can not be assumed that the hardware in question is actually available for testing.
11. Zir Blazer says: August 7, 2023 at 3:38 pm When I see in retro stuff those things that appears to be madness, I instead admire them as some form of twisted brilliance.
If before the age of CPUID, the same piece of code needed to run in systems spanning multiple CPU + FPU generations with a bunch of different -known- errata (Plus even further NDA errata, and perhaps even discovering bugs on your own before it was officially documented as errata), I would assume than the lowest common denominator that could run in every system would be a total hodgepodge. The whole POP/PUSH between I/O Port writes seems like some form of NOP/wait states, which I recall that were necessary in some fast systems to workaround slow I/O accesses, like this: https://forum.vcfed.org/index.php?threads/io-delays-on-8088-class-computers.45713/
I imagine that it would be fun to see the developer’s notes to see how that piece of code evolved as less test systems failed until they settled on that.
12. Michal Necasek says: August 8, 2023 at 5:59 pm It’s not brilliance, it’s desperation. I’ve been there myself; when the hardware doesn’t behave the way it’s supposed to, you just try different things until something (hopefully) works. But you may never know why it works, and you definitely won’t know if it’s the best workaround.
Yes, the PUSH/POP loops are meant to slow things down, more so than just an “empty” LOOP instruction. The trouble is that the speed of these loops is highly, highly variable across CPU models. So they end up wasting a lot of time on slower CPUs, and potentially may still run too fast on newer CPUs.
All those delays around port I/O, I’m pretty convinced half of it was simply a cargo cult. The other half was working around hardware which was a bit too slow and not smart enough to insert wait states. As that vcfed thread mentions, newer machines don’t really have that problem because the JMP $+2 trick stopped doing anything useful on the i486 and the chipset had to take care of things.
There _are_ chips that require random delays, like the Yamaha OPL chips, but that’s not a problem with the bus design. More sanely designed hardware has status bits which can be polled and these bits tell the host if the chip is ready or not. Older chip datahseets often simply documented that certain situations require the host to wait some microseconds or milliseconds. That’s what’s best achieved by dummy port I/O because using pure CPU delays is quite problematic (due to the vast speed differential between slow and fast x86 processors, plus CPUs stopped running at constant speeds anyway).
13. MiaM says: August 9, 2023 at 4:36 pm My impression from for example reading the readme for DOS/windows re options for himem.sys and other places is that Microsoft probably got paid by various hardware vendors to specifically support broken/odd hardware behavior. Sure, floating point exception handling and A20 gate handling are two very different things, but I think that what the readme says about A20 probably gives a clue for other things too. In particular that larger companies like HP probably paid Microsoft for them to support their hardware, while smaller companies just had to make things 100% compatible or just ignore that their hardware might not work with updated/new software.
A super long term test would be to recompile without these “desperation measures” and distribute the recompiled version among the vintage computing communities and ask everyone who has old computers to test if the recompiled version works or not, and thus try to find what actual hardware might require these desperate measures.
14. Richard Wells says: August 9, 2023 at 7:49 pm Finding a system with an intermittent connection between the x86 and x87 processors seems difficult these days. The code doesn’t just cover the no coprocessor test at the beginning but makes the short waits that can be recovered by the x86 processor frequently. With the 486 and later chips, the introduced waits probably have little impact on performance and changing the design risks unexpectedly breaking something.
15. Victor Khimenko says: August 12, 2023 at 1:35 am Someone out there have found an interesting link which may explains things: https://www.cs.earlham.edu/~dusko/cs63/prepentium.html
80486 errata:
A bug in the FPU creates three cases when the FERR# error is generated by a floating point operation, but it is not reported correctly.
If an unmasked exception occurs when the numeric exception bit in CR0 is clear and the IGNNE# pin is active, the performance of the FPU will be retarded as long as the exception remains pending.
Thus it may not be as much of a desperation and more as precise knowledge. And it doesn’t matter that faster CPUs would become available: they are not supposed to have this bug and thus wouldn’t need to have a fix for 80486-only bug!
16. Michal Necasek says: August 19, 2023 at 12:12 am The information given at the link unfortunately has one fundamental deficiency: It does not list which 486 steppings are affected by those bugs. For most of the bugs, the information is not present in the source (Hummel) either, and the text just says that “some versions of the 80486 may be affected”.
17. Joshua says: September 3, 2023 at 7:51 am WIN87EM was also around in the Windows 2.x days.
It was available as an optional EXE file, though.
There also was an update/fix available in the Windows 3.1x days (Q86869, WW0548.EXE).
WIN87EM also virtualized the physical x87, for multitasking with multiple Windows programs.
18. Michal Necasek says: September 4, 2023 at 1:10 pm “Virtualizing” is a strange term for FPU context switching. Why would you call it that?
Thanks for the pointer though, I see that the old WIN87EM (with EXE extension, although it was a DLL of course) was for whatever bad reason not shipped with Windows 2.x itself, but rather with the Windows 2.x SDK, and applications were expected to ship it if they needed it.
Q86869 is interesting because there’s absolutely no hint as to what it’s supposed to fix, just that it only applies to systems with a 387 and not 486 machines.
19. Yuhong Bao says: September 15, 2023 at 6:47 am Don’t forget the external hardware needed to do FERR# and IGNNE#.
20. Vikki McDonough says: September 30, 2024 at 6:38 am @Zir Blazer:
If before the age of CPUID, the same piece of code needed to run in systems spanning multiple CPU + FPU generations with a bunch of different -known- errata (Plus even further NDA errata, and perhaps even discovering bugs on your own before it was officially documented as errata)
Who the hell in their right mind NDAs errata? That seems like a surefire way to make software developers’ jobs much harder and accomplish frick-all else.
21. Zuxy says: April 5, 2025 at 6:38 am Which tool did you use to dissemble this DLL? semblance (https://github.com/zfigura/semblance) can only disassemble exported functions, not the IRQ handler.
I’m asking because recent Intel CPUs no longer save fds and fcs in fsave, leading to general protection error in this DLL such as https://www.reddit.com/r/windows98/comments/1380mz4/missing_a_autoexecnt_file_in_my_system32_folder/ I’m curious to know how exactly it happens and whether there can be a workaround.
22. Michal Necasek says: April 5, 2025 at 8:45 pm I used IDA Pro of course.
And yes there is an imperfect workaround, but it requires modifying WIN87EM.DLL because Intel simply screwed up their hardware too badly. I don’t know what they were thinking…
Leave a Reply
Your email address will not be published.Required fields are marked *
Comment *
Name *
Email *
Website
Δ
This site uses Akismet to reduce spam. Learn how your comment data is processed.
- ### Archives
- October 2026
- * August 2026
- * July 2026
- * June 2026
- * May 2026
- * April 2026
- * March 2026
- * February 2026
- * January 2026
- * December 2025
- * November 2025
- * October 2025
- * September 2025
- * August 2025
- * July 2025
- * June 2025
- * May 2025
- * April 2025
- * March 2025
- * February 2025
- * January 2025
- * December 2024
- * November 2024
- * October 2024
- * September 2024
- * August 2024
- * July 2024
- * June 2024
- * May 2024
- * April 2024
- * March 2024
- * February 2024
- * January 2024
- * October 2023
- * September 2023
- * August 2023
- * July 2023
- * June 2023
- * May 2023
- * April 2023
- * March 2023
- * January 2023
- * December 2022
- * November 2022
- * October 2022
- * September 2022
- * July 2022
- * June 2022
- * May 2022
- * April 2022
- * March 2022
- * February 2022
- * January 2022
- * December 2021
- * November 2021
- * October 2021
- * September 2021
- * August 2021
- * July 2021
- * June 2021
- * May 2021
- * April 2021
- * March 2021
- * February 2021
- * January 2021
- * December 2020
- * November 2020
- * October 2020
- * September 2020
- * August 2020
- * July 2020
- * June 2020
- * May 2020
- * April 2020
- * March 2020
- * February 2020
- * January 2020
- * December 2019
- * November 2019
- * October 2019
- * September 2019
- * August 2019
- * July 2019
- * June 2019
- * May 2019
- * April 2019
- * March 2019
- * February 2019
- * January 2019
- * December 2018
- * November 2018
- * October 2018
- * August 2018
- * July 2018
- * June 2018
- * May 2018
- * April 2018
- * March 2018
- * February 2018
- * January 2018
- * December 2017
- * November 2017
- * October 2017
- * August 2017
- * July 2017
- * June 2017
- * May 2017
- * April 2017
- * March 2017
- * February 2017
- * January 2017
- * December 2016
- * November 2016
- * October 2016
- * September 2016
- * August 2016
- * July 2016
- * June 2016
- * May 2016
- * April 2016
- * March 2016
- * February 2016
- * January 2016
- * December 2015
- * November 2015
- * October 2015
- * September 2015
- * August 2015
- * July 2015
- * June 2015
- * May 2015
- * April 2015
- * March 2015
- * February 2015
- * January 2015
- * December 2014
- * November 2014
- * October 2014
- * September 2014
- * August 2014
- * July 2014
- * June 2014
- * May 2014
- * April 2014
- * March 2014
- * February 2014
- * January 2014
- * December 2013
- * November 2013
- * October 2013
- * September 2013
- * August 2013
- * July 2013
- * June 2013
- * May 2013
- * April 2013
- * March 2013
- * February 2013
- * January 2013
- * December 2012
- * November 2012
- * October 2012
- * September 2012
- * August 2012
- * July 2012
- * June 2012
- * May 2012
- * April 2012
- * March 2012
- * February 2012
- * January 2012
- * December 2011
- * November 2011
- * October 2011
- * September 2011
- * August 2011
- * July 2011
- * June 2011
- * May 2011
- * April 2011
- * March 2011
- * January 2011
- * November 2010
- * October 2010
- * August 2010
- * July 2010
- ### Categories
- 286
- * 386
- * 386MAX
- * 3Com
- * 3Dfx
- * 486
- * 8086/8088
- * Adaptec
- * AGP
- * AMD
- * AMD64
- * Apple
- * Archiving
- * Assembler
- * ATi
- * BIOS
- * Books
- * Borland
- * BSD
- * Bugs
- * BusLogic
- * C
- * C&T
- * CD-ROM
- * Cirrus Logic
- * CompactFlash
- * Compaq
- * Compression
- * Computing History
- * Conner
- * Corrections
- * CP/M
- * Creative Labs
- * Crystal Semi
- * Cyrix
- * DDR RAM
- * Debugging
- * DEC
- * Development
- * Digital Research
- * Documentation
- * DOS
- * DOS Extenders
- * Dream
- * E-mu
- * Editors
- * EISA
- * Ensoniq
- * ESDI
- * Ethernet
- * Fakes
- * Fixes
- * Floppies
- * Floppy Images
- * Graphics
- * Hardware Hacks
- * I18N
- * IBM
- * IDE
- * Intel
- * Internet
- * IPMI
- * K7
- * Keyboard
- * Kryoflux
- * Kurzweil
- * LAN Manager
- * Legal
- * Linux
- * LSOED
- * Marketing
- * MCA
- * Microsoft
- * MIDI
- * NetWare
- * Networking
- * NeXTSTEP
- * NFS
- * Novell
- * NT
- * OS X
- * OS/2
- * PC architecture
- * PC hardware
- * PC history
- * PC press
- * PCI
- * PCMCIA
- * Pentium
- * Pentium 4
- * Pentium II
- * Pentium III
- * Pentium Pro
- * Plug and Play
- * PowerPC
- * Pre-release
- * PS/2
- * QNX
- * Quantum
- * Random Thoughts
- * RDRAM
- * Roland
- * Ryzen
- * S3
- * SCO
- * SCSI
- * Seagate
- * Security
- * Site Management
- * SMP
- * Software Hacks
- * Solaris
- * Sound
- * Sound Blaster
- * Source code
- * Standards
- * Storage
- * Supermicro
- * TCP/IP
- * ThinkPad
- * Trident
- * Typewriter
- * UltraSound
- * Uncategorized
- * Undocumented
- * UNIX
- * UnixWare
- * USB
- * VGA
- * VirtualBox
- * Virtualization
- * VLB
- * Watcom
- * Wave Blaster
- * Western Digital
- * Windows
- * Windows 95
- * Windows XP
- * Wireless
- * WordStar
- * X11
- * x86
- * x87
- * Xenix
- * Xeon
- * Yamaha
[Proudly powered by WordPress.](https://wordpress.org/ "Semantic Personal Publishing Platform")