I am learning how to program an operating system on the x86 architecture. The CPU throws an interrupt when it tries to divide by zero, the interrupt has to handle this, if not the system crashes (at least on Virtualbox). So I am wondering what happens in the CPU level and its registers when it tried to divide by zero? And why does it crash? why not for example just ignore it and give whatever was calculated on the register?
Asked
Active
Viewed 319 times
0
-
1CPU does not *crash*, its behaviour is documented at https://www.felixcloutier.com/x86/div It's a good question why CPU designers chose **#DE** on `DIV by 0` rather than **CF** to signalize overflow (as they did with ADD, MUL, SAL etc). Division overflow could be trapped at user level then, instead of crashing the OS/VirtualBox when its not handled properly. – vitsoft May 28 '20 at 16:04
-
Related: [On which platforms does integer divide by zero trigger a floating point exception?](https://stackoverflow.com/q/37262572) and [Why does integer division by zero result in a floating point exception?](https://stackoverflow.com/q/16928942) have a bit of history. The "what if" part might fit better as a question on https://retrocomputing.stackexchange.com/ about why 8086 made this design decision. – Peter Cordes May 28 '20 at 19:59
-
Apparently RISC-V doesn't fault on div by 0, according to this comment: [Did any mechanical computers or calculators handle divide by zero with an exception, or did they all loop?](https://retrocomputing.stackexchange.com/posts/comments/35238). Neither does ARM: [Why does integer division by -1 (negative one) result in FPE?](https://stackoverflow.com/q/46378104). I don't know what does happen on ARM. – Peter Cordes May 28 '20 at 20:00
-
1But the answer for x86 is simple: `#DE` exception with saved RIP = address of the div instruction, no change to RDX or RAX. (Or on 8086 itself, saved IP = instruction *after* the div) – Peter Cordes May 28 '20 at 20:01
-
Thanks a lot for all of the mentioned resources. – Mustafa Otbah May 29 '20 at 14:35