[OpenBIOS] [RFC 1/3] Allow customisable behaviour when a divide-by-zero situation is encountered.

Mark Cave-Ayland mark.cave-ayland at ilande.co.uk
Mon Jan 7 16:07:39 CET 2013


Sun's OBP and Apple's OpenFirmware have different behaviours from each other
when a divide-by-zero error is encountered. Since we can't reenter Forth from
a trap on all platforms, detect this case before performing the division and
allow an optional defer word to be executed instead.

Signed-off-by: Mark Cave-Ayland <mark.cave-ayland at ilande.co.uk>
---
 openbios-devel/forth/bootstrap/bootstrap.fs |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/openbios-devel/forth/bootstrap/bootstrap.fs b/openbios-devel/forth/bootstrap/bootstrap.fs
index 6878449..dc20caf 100644
--- a/openbios-devel/forth/bootstrap/bootstrap.fs
+++ b/openbios-devel/forth/bootstrap/bootstrap.fs
@@ -445,10 +445,23 @@ variable leaves 0 leaves !
 \ 7.3.2.3 double number arithmetic
 \ 
 
+defer (div-by-zero-handler)
+
 : s>d      dup 0 < ; 
 : dnegate  0 0 2swap d- ;
 : dabs     dup 0 < if dnegate then ;
-: um/mod   mu/mod drop ;
+: um/mod   
+  dup 0 = if
+    \ Allow optional override of divide by zero handling
+    ['] (div-by-zero-handler) /n + @ 0 <> if
+      (div-by-zero-handler) drop
+    else
+      mu/mod drop
+    then
+  else  
+    mu/mod drop
+  then
+  ;
 
 \ symmetric division
 : sm/rem  ( d n -- rem quot )
-- 
1.7.10.4




More information about the OpenBIOS mailing list