Code Smells of Desperation (2023) os2museum.com · 11 points · 3 comments · HN th...

@best

Code Smells of Desperation (2023) os2museum.com · 11 points · 3 comments · HN thread

TL;DR The author disassembles the math exception handler in WIN87EM.DLL, the x87 floating-point emulator shipped with Windows 3.0 through 3.11, and finds its IRQ 13 handler full of repeated delays and redundant FPU and port operations that look like trial-and-error fixes. The article concludes the code likely works around broken hardware, but the original reason is undocumented and probably lost.

  • WIN87EM.DLL first shipped with Windows 3.0; the Windows 3.1 version is nearly identical and the 3.11 version is byte-for-byte identical to 3.1.
  • On PC/AT-class systems it hooks IRQ 13; on PC and PC/XT it hooks NMI vector 2.
  • The IRQ 13 handler repeats PUSH/POP AX delay loops, writes zero to I/O port F0h four times, and executes FNCLEX twice.
  • The author finds the sequence doubtful in effect and suspects it was added in desperation, possibly to support broken hardware.
  • Other error handlers the author compared against are far more conventional and 'by the book'.
  • The code has no explanatory comments, so later maintainers cannot safely remove it.

Discussion The thread is short, with three comments. It centers on why undocumented workaround code is dangerous to remove, and the mood is wry and lightly joking, with one commenter poking fun at the sprawling delay loops.

  • Undocumented workarounds are dangerous to delete (some): Scott_w quotes the article's point that the code never states what problem it solves, so a future programmer cannot tell whether it fixes a legitimate hardware issue. Testing cannot settle this because the affected hardware may be unavailable.
  • Delay loops look like someone adding waits until it worked (some): Ebiederm says the pattern resembles 'keep adding delays until it works', though the basic operations are sane. They note that an outb to an ISA port takes a deterministic amount of time and can replace a delay loop.
  • Joking about the handler still failing on other hardware (a few): Bitwize jokingly suggests that a user's Weitek 3167 coprocessor still fails and that more PUSH/POP AX iterations might hit a timing window, mocking the same trial-and-error pattern.

#x87 #Windows3 #retrocomputing #reverseEngineering #legacyCode