3 ms·
Years ago I was developing firmware for a measuring instrument (an ultra-low current ammeter) based on an 8-bit CPU, the Motorola 6809. I was the first person i
by RogerC60 4y ago
Years ago I was developing firmware for a measuring instrument (an ultra-low current ammeter) based on an 8-bit CPU, the Motorola 6809. I was the first person in the company to use our newly-adopted C language development system with in-circuit emulator. The firmware used interrupts for I/O and was built on an RTOS, so it was multi-threaded. All of the math for measurement processing was done with long (32-bit) integers (on the 8-bit CPU).
Once in a while the instrument's readings were wildly wrong. After we ruled out actual signal glitches in the analog front end, I started poking around with the debugger, which did have some very nice breakpoint capabilities. This is when I discovered that the C compiler implemented long math operators using anonymous function calls. And that these functions were not thread safe (they used temporary variables below the stack pointer)! If an interrupt occurred during a math operation, the function returned a wrong value. Disabling interrupts for long enough to do a long divide was not an option as that would seriously mess up the operation of the analog front end.
I duplicated the functions in assembler, but with proper use of the stack, and then replaced all the naked math operators in my source code that operated on longs with calls to my new functions.