What does the assembly instruction trap do?

Niklas R. picture Niklas R. · Nov 26, 2012 · Viewed 8.1k times · Source

"RTFM"

A program typically issues a software trap when the program requires servicing by the operating system. The general exception handler for the operating system determines the reason for the trap and responds appropriately.

Is the assembly instruction trap alike the instruction TRAP in BASIC? The answer seems to be yes. Can you accept or reject my conclusion?

Code for "no interruption" is according to my instructions:

noint:
    PUSH r8
    movia r8,0x003b683a # machine code for TRAP
    ldw et,-4(ea) # read instr closest to return
    cmpeq et,et,r8 # compare
    POP r8
    bne et,r0,TrapHandler # if equal, jump to traphandler

Update

AFAIK in BASIC you can write e.g.

10 TRAP 20

to make row 20 the row for handling an error.

Answer

Alexey Feldgendler picture Alexey Feldgendler · Nov 26, 2012

Not sure what TRAP does in BASIC, but the TRAP instruction in the assembler manual that you linked generates a hardware exception that can be handled by the operating system.

There is rarely need for a programmer to use this instruction in their code. The typical use of it is to be inserted by a debugger into the code being debugged at the point where a stop is desired (breakpoint), then running/continuing the program, and regaining control once the TRAP instruction is reached.