# `chk`

[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/chk

**Check register against bounds**

Category: System

Checks whether a data register is between zero and an upper bound, with both endpoints included.

## Flags

| Flag | Effect |
| --- | --- |
| X | not changed |
| N | undefined (do not rely on it). Defined only when the CHK exception is raised: set if the register is below zero, cleared if it is above the bound. |
| Z | undefined (do not rely on it) |
| V | undefined (do not rely on it) |
| C | undefined (do not rely on it) |

## Forms

- `chk <ea>, Dn` · Size .w
  - Operand 1: Dn, (An), (An)+, -(An), d(An), d(An,Xn), abs, d(PC), d(PC,Xn), #imm (data)

## 68020 only

These forms require the 68020. Select `opt p=68020` before any code or data.

- `chk <ea>, Dn` · Size .l
  - Operand 1: Dn, (An), (An)+, -(An), d(An), d(An,Xn), abs, d(PC), d(PC,Xn), #imm (data)

## Operands

- [`<ea>`](https://asm-editor.specy.app/documentation/m68k/addressing-mode.md): An operand using one of the addressing modes listed for this form. The list determines whether it can be a register, memory or an immediate value. With `opt p=68020`, an allowed `d(An,Xn)` or `d(PC,Xn)` mode also permits the corresponding 68020 indexed and memory indirect forms. See [68020 addressing](https://asm-editor.specy.app/documentation/m68k/addressing-mode/68020.md).
- [`Dn`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/direct.md): A data register, `d0` to `d7`.
- [`#imm`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/immediate.md): An immediate value written after `#`: a number, character or label, such as `#10`, `#$FF` or `#'a'`. It must fit the operation’s size: byte `-128` to `255`, word `-32768` to `65535`, or long `-2147483648` to `4294967295`; signed and unsigned spellings are accepted.

## Description

`chk.w` reads the bound and the register’s low word as signed words. A value below zero or above the bound raises a CHK exception through vector 6 at `$18`. The handler returns to the instruction after `chk` with `rte`. N is set for a value below zero and cleared for a value above the bound. Z, V and C are undefined by the instruction and may change. X is unchanged.

Without a handler, the program ends at `chk`. See [writing a handler](https://asm-editor.specy.app/documentation/m68k/exceptions/handlers.md). On the 68020, `chk.l` checks the whole register against a signed long bound. [chk2](https://asm-editor.specy.app/documentation/m68k/instruction/chk2.md) lets you supply both bounds.

## Example

```m68k
* --- Keep an index between 0 and 9 ---
move.l #out_of_range, $18 ; vector 6 is at $18
moveq #4, d0
chk #9, d0 ; 4 is in range: nothing happens
moveq #12, d0
chk #9, d0 ; 12 is above 9: out_of_range runs
move.w d0, d1 ; rte comes back here: d1 = 9
bra END

out_of_range:
    moveq #9, d0 ; use the last index instead
    rte

END:
```
