# `trap`

[M68K Documentation](https://asm-editor.specy.app/documentation/m68k.md) › [Instructions](https://asm-editor.specy.app/documentation/m68k/instruction.md)

Web page: https://asm-editor.specy.app/documentation/m68k/instruction/trap

Category: System

Raises a software trap or calls a simulator task.

Used in: [trap #15 and its tasks](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/trap-15-and-its-tasks.md) (M68K assembly), [The screen, keyboard and mouse through traps](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/screen-keyboard-and-mouse.md) (M68K assembly), [Exceptions and the vector table](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/exceptions-and-the-vector-table.md) (M68K assembly), [Interrupts and cycles](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/interrupts-and-cycles.md) (M68K assembly), [Print a string](https://asm-editor.specy.app/learn/courses/m68k/examples/hello-world.md) (M68K assembly), [Read two numbers and print their sum](https://asm-editor.specy.app/learn/courses/m68k/examples/sum-of-two-numbers.md) (M68K assembly), [Convert a number to a string in any base](https://asm-editor.specy.app/learn/courses/m68k/examples/number-to-string.md) (M68K assembly), [Drawing shapes on the screen](https://asm-editor.specy.app/learn/courses/m68k/examples/drawing-on-the-screen.md) (M68K assembly), [A bouncing ball](https://asm-editor.specy.app/learn/courses/m68k/examples/bouncing-ball.md) (M68K assembly), [Move a square with the keyboard](https://asm-editor.specy.app/learn/courses/m68k/examples/keyboard-control.md) (M68K assembly)

## Flags

| Flag | Effect |
| --- | --- |
| X | not changed |
| N | not changed |
| Z | not changed |
| V | not changed |
| C | not changed |

## Forms

- `trap #imm`

## Operands

- [`#imm`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/immediate.md): A trap number from `0` to `15`. `#0` to `#14` run the handler in vectors 32 to 46; `#15` runs the simulator’s task numbered in `d0.b`.

## Description

`trap #0` through `trap #14` run handlers installed by the program. `trap #15` runs an EASy68K simulator task.

For a software trap, the processor saves the next instruction’s address and the status register on the supervisor stack, enters supervisor mode, and runs the handler from vector 32 + n. The vector entry for `trap #n` is at `$80` + 4 × n. For example, `move.l #service, $8C` installs `service` as the handler for `trap #3`. The handler returns to the next instruction with [rte](https://asm-editor.specy.app/documentation/m68k/instruction/rte.md). Without a handler, the program ends at the trap and reports the missing vector. See [writing a handler](https://asm-editor.specy.app/documentation/m68k/exceptions/handlers.md).

For `trap #15`, the low byte of `d0` selects a simulator task. Task 13, for example, prints the null-terminated string at `a1`. Execution continues after the trap, and the call is available in user mode. The [trap task reference](https://asm-editor.specy.app/documentation/m68k/traps.md) lists each task and its registers.

## Example

```m68k
* --- trap #15: one of the simulator's tasks ---
move.b #4, d0
trap #15 ; task 4: read a number into d1

* --- trap #0 to #14: a handler of your own ---
move.l #double, $80 ; trap #0's vector, number 32, is at $80
trap #0 ; runs double, then carries on below

move.b #3, d0
trap #15 ; task 3: print d1, twice the number read
bra END

double:
    add.l d1, d1
    rte ; back to the instruction after the trap

END:
```
