Guardian Programmer's Guide

Table Of Contents
Debugging, Trap Handling, and Signal Handling
Guardian Programmer’s Guide 421922-014
25 - 17
Writing a Trap Handler: Examples
! Enter here on trap:
TRAP:
! Save registers R0 through R7 and allocate local storage:
CODE(PUSH %777; ADDS LOCALS);
! Call DEBUG if trap not an arithmetic overflow
! condition:
IF TRAPNUM <> 2 THEN CALL DEBUG;
! Format and print the message on the home terminal with
! the P register value displayed in octal:
SBUF ':=' "ARITHMETIC OVERFLOW AT %";
CALL NUMOUT(SBUF[24], PREG, 8, 6);
CALL WRITE(TERM^NUM,WBUF,30);
IF <> THEN CALL DEBUG;
! Clear the overflow bit in the ENV register:
EREG.OVERFLOW := 0;
! Exit the trap handler and restore old values of
! registers:
CALL ARMTRAP(0, $LMIN(LASTADDR,%77777) - 500);
END;
!------------------------------------------------------------
! Main procedure reads Startup message, calls OVERFLOWTRAP,
! opens the home terminal, and creates an arithmetic overflow
! condition by attempting to divide by zero.
!------------------------------------------------------------
PROC TRAP^USER MAIN;
BEGIN
STRING .TERM^NAME[0:MAXLEN - 1];
INT I, J, LEN;
! Read the Startup message:
CALL INITIALIZER;
! Call the OVERFLOWTRAP procedure to arm the trap:
CALL OVERFLOWTRAP;