Is there a program transformation or compilation technique that removes potential runtime exceptions such as div by 0, floating point overflow / underflow, etc. I want to handle potential exceptions in my program before the CPU generates them. Furthermore, I'd like a transformation expressed at the x64 machine code level.
For instance, to remove the possibility of CPU generating div by 0
, I can check that divisor is 0 before invoking the div
instruction, something along the following lines.
cmp rcx 0
jz my_handler //some routine I wrote
div rcx
However, it is not clear how to implement this technique for floating point exceptions, especially underflow and overflow. Wouldn't the check for overflow cause the overflow itself? I realize that languages support exception handling, but my use case necessitates this.