# `cmp2`

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

**Compare register against bounds** · 68020

Category: Comparison

Compares a register with a lower and an upper bound and sets the flags, with C set when it is outside them.

Sizes .b .w .l, .w by default · 68020 only: select `opt p=68020` before any code or data

## Flags

| Flag | Effect |
| --- | --- |
| X | not changed |
| N | undefined (do not rely on it) |
| Z | depends on the result |
| V | undefined (do not rely on it) |
| C | depends on the result |

## Forms

- `cmp2 <ea>, Rn`
  - Operand 1: (An), d(An), d(An,Xn), abs, d(PC), d(PC,Xn) (control)
  - Operand 2: Dn, An

## 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).
- [`Rn`](https://asm-editor.specy.app/documentation/m68k/addressing-mode/direct.md): A data or address register, `d0` to `d7` or `a0` to `a7`.

## Description

The memory operand names two consecutive bounds, lower first, each stored at the instruction’s size. A data register is compared at that size. An address register is compared as a full long, with byte or word bounds sign-extended to long. Both endpoints are included in the range.

Interpret the bounds and value as signed numbers. If the lower bound is at or below the upper bound, the range includes every value between them. If the lower bound is above the upper bound, the range wraps: a value is in range when it is at or above the lower bound, or at or below the upper bound. For example, `dc.b 16, 240` stores the signed bounds 16 and -16. Its accepted byte values are 16 through 127 and -128 through -16, which are the unsigned values 16 through 240.

Z is set when the value equals either bound and cleared otherwise. C is set outside the range and cleared inside it. N and V are undefined, and X is unchanged. Use `bcs` after the comparison to branch on an out-of-range value. [chk2](https://asm-editor.specy.app/documentation/m68k/instruction/chk2.md) performs the same comparison and also raises an exception when the value is outside.

## Example

```m68k
opt p=68020
* --- Count the digits in a string ---
lea text, a0
moveq #0, d1 ; the count
next:
    move.b (a0)+, d0
    beq END ; the 0 that ends the string
    cmp2.b digits, d0 ; C is set when d0 is not from '0' to '9'
    bcs next
    addq.l #1, d1 ; at the end, d1 = 3
    bra next

text:
    dc.b 'a1b22cd', 0
digits:
    dc.b '0', '9' ; the lower bound, then the upper one

END:
```
