.ORIG   xFD70         ; Where this routine resides
                ST      R7, SaveR7     ; Save R7 for subsequent  RET
                ST      R1, SaveR1     ; R1: a temp for MC register
                ST      R0, SaveR0     ; R0 is used as working space

; print message that machine is halting

                LD      R0, ASCIINewLine
                TRAP    x21
                LEA     R0, Message
                TRAP    x22
                LD      R0, ASCIINewLine
                TRAP    x21

; clear bit 15 at $FFFF to stop the machine

                LDI     R1, MCR        ; Load MC register into R1
                LD      R0, MASK       ; R0 = $7FFF
                AND     R0, R1, R0     ; Mask to clear the top bit
                STI     R0, MCR        ; Store R0 into MC register

; return from HALT routine. 
; (how can this routine return if the machine is halted above?)
;
                LD      R1, SaveR1     ; Restore registers
                LD      R0, SaveR0
                LD      R7, SaveR7     ; Restore trap return
                RET

; Some constants

ASCIINewLine    .FILL   x000A
SaveR0          .FILL   x0000
SaveR1          .FILL   x0000
SaveR7          .FILL   x0000
Message         .FILL   "Halting the machine."
MCR             .FILL   xFFFF   ; Address of MCR
MASK            .FILL   x7FFF   ; Mask to clear the top bit
                .END