by ajross · 3y ago · 0 comments
For clarity, because the article takes too long to get there: DIVU has a 32 bit dividend, but a 16 bit divisor and a 16 bit result. So if you try to divide e.g. 0x20000 by 2, the result should be 0x10000, which doesn't fit in the output register. So... the CPU sets the overflow flag but otherwise does nothing!
I'm not quite old enough to have been writing assembly for the 68k, but I've heard this issue before. This was surely not the only footgun situation with DIVU in the market.
It certainly is a bit of a footgun, because one normally doesn’t expect overflow on integer division. On the other hand, the other basic arithmetic operations all require overflow checks as well, or equivalent operand value analysis. And this is assembly we’re talking about, where you’re supposed to know what you’re doing.
The developer should check for overflow before using the value. If this assembly is compiler generated then it's probably classifiable as a compiler bug.
Cortex-M and -R have support for div-by-0 faults but it's disabled by default.
That said, I think this instruction would be safer and more useful if it still set at least the remainder result bits (which should always be valid). Then this case would not require checking, as would some other common cases like 'execute every odd iteration' kind of code.