4 ms·
I once had a piece of Windows code that made a conditional jump, after executing a 3rd-party library function. The jump frequently went the wrong way, even thou
by denton-scratch 5y ago
I once had a piece of Windows code that made a conditional jump, after executing a 3rd-party library function. The jump frequently went the wrong way, even though the values for the conditional jump were as expected.
It took a long time to crack, using a machine-code debugger.
It turned out that the assumptions of the Microsoft code generator (used to compile the problematic piece of code) were different from the assumptions of the compiler used to build the 3rd-party library, which I think was built using Borland.
Specifically, the Microsoft compiler saved the flag register after entry to a function, and restored it prior to exit; the Borland compiler saved the flag register before calling a function, and restored it after the function call returned.
So library functions compiled with the Borland compiler usually stomped on the flag register, but they worked fine with caller code compiled by Borland. But if you called a Borland function from Microsoft code, anything in the flag register before the function call was lost forever.
It doesn't matter whether it's the caller's job or the callee's job to save and restore the flag register; but it's critical that caller and callee respect the same convention. The Borland convention was shared by most other non-Microsoft compilers. It was the Microsoft C compiler that was deviant.
I believe that this was a deliberate effort by Microsoft to make competing C compilers not work properly with Microsoft code.