Examples [ Trap Handling Programmer's Guide ] MPE/iX 5.0 Documentation
Trap Handling Programmer's Guide
Examples
To use the ARITRAP, HPENBLTRAP, and XARITRAP intrinsics, you must do the
following:
* Declare the intrinsics in your source, using whatever conventions are
appropriate to your language.
* Declare the trap handling procedure, using the appropriate formal
parameters.
NOTE Since you can provide only one plabel to the XARITRAP intrinsic,
your arithmetic trap handling procedure must handle all types of
enabled traps associated with that particular intrinsic.
Example 2-1 is an HP Pascal/XL code excerpt that illustrates how you can
handle the IEEE Floating Point Divide By Zero trap condition.
When the program containing this excerpt is executed, it results in an
IEEE Divide By Zero exception, and the user-written trap handling routine
is invoked. The trap handler replaces the result of the division with
the desired value. The output of this program is the value of
maxlongreal.
Example 2-1 Arithmetic Trap Handler
(* Declaring record to receive trap information returned by trap mechanism on a *)
(* trap condition *)
PROGRAM XAMPL21(output);
TYPE
real_ptr = ^real;
long_ptr = ^longreal;
user_info_rec = record
instruction : integer;
offset : integer;
space_id : integer;
error_code : integer;
status : integer;
operation : integer;
format : integer;
source1_ptr : localanyptr;
source2_ptr : localanyptr;
result_ptr : localanyptr;
end; (* record *)
user_info_ptr = ^user_info_rec;
(* Constants for the trap procedure *)
CONST
(* mask for trapping all ieee exceptions *)
ieee_mask = hex('0007C000');
(* error code for divide by zero *)
fdiv_zero = hex('00020000');
(* maximum longreal value *)
maxlongreal = 1.79L308;
(* maximum real value *)
maxreal = 3.40E38;
VAR
L1, L2, L3 : longreal;
oldmask : integer;
oldplabel : integer;
PROCEDURE ARITRAP; intrinsic;
PROCEDURE XARITRAP; intrinsic;
Example 2-1 Arithmetic Trap Handler, continued
PROCEDURE Trap_Handler(user_info : user_info_ptr); (* Trap handling routine *)
VAR
long_res_ptr : long_ptr;
real_res_ptr : real_ptr;
BEGIN
(* Handle only divide by zero; ignore all others *)
With user_info^ do
if ( error_code = fdiv_zero ) then
BEGIN
(* Change the value of the result *)
if ( format = 0 ) then
BEGIN
real_res_ptr := result_ptr;
real_res_ptr^ := maxreal;
END
else if ( format = 1 ) then
BEGIN
long_res_ptr := result_ptr;
long_res_ptr^ := maxlongreal;
END;
END;
END;
BEGIN (* Main program *)
ARITRAP(1); (* enable all traps *)
XARITRAP( ieee_mask, baddress( Trap_Handler),oldmask, oldplabel);
(* arm all ieee traps *)
L1 := 233.0;
L2 := 0.0;
L3 := L1/L2; (* ieee divide by zero *)
(* The following statement is executed upon return from the trap handler; the *)
(* value of L3 is maxlongreal *)
writeln(L3);
END.
(* end example 2-1 *)
MPE/iX 5.0 Documentation