cmp.w

Compare (Wide)

CMP.W <Rn>, <Operand2>

Thumb-2 32-bit Compare (Subtract and update flags).

Details

Compare (Subtract and update flags): computes Rn - Operand2, updating the N, Z, C, and V flags based on the result without writing to a destination register. This is a Thumb-2 (32-bit) instruction available in T32 execution state and is commonly used to set condition flags for subsequent conditional branches.

Pseudocode Operation

result ← Rn - Operand2
N ← result[31]
Z ← (result == 0)
C ← NOT BorrowFrom(Rn - Operand2)
V ← OverflowFrom(Rn - Operand2)

Example

CMP.W r1, r2

Encoding

Binary Layout
1110101
1101
1
Rn
0
imm3
1111
imm2
stype
Rm
 
Format Thumb2 Data Proc
Opcode 0xEBB00F00
Extension T32 (Thumb2)

Operands

  • Rn
    First source / base general-purpose register
  • Operand2
    Flexible second operand (register or shifted register)

Reference (Arm AArch32 ISA)

Instruction Forms

Encoding Instruction ISA Bit pattern
0x03500000 CMP{<c>}{<q>} <Rn>, #<const> A32 cond | 00110 | 10 | 1 | Rn | 0 | 0 | 0 | 0 | imm12
0x2800 CMP{<c>}{<q>} <Rn>, #<imm8> T32 001 | 01 | Rn | imm8
0xF1B00F00 CMP{<c>}.W <Rn>, #<const> T32 11110 | i | 0 | 1101 | 1 | Rn | 0 | imm3 | 1111 | imm8
0x01500060 CMP{<c>}{<q>} <Rn>, <Rm>, RRX A32 cond | 00010 | 10 | 1 | Rn | 0 | 0 | 0 | 0 | 00000 | 11 | 0 | Rm
0x01500000 CMP{<c>}{<q>} <Rn>, <Rm> {, <shift> #<amount>} A32 cond | 00010 | 10 | 1 | Rn | 0 | 0 | 0 | 0 | imm5 | stype | 0 | Rm
0x4280 CMP{<c>}{<q>} <Rn>, <Rm> T32 010000 | 1010 | Rm | Rn
0x4500 CMP{<c>}{<q>} <Rn>, <Rm> T32 010001 | 01 | N | Rm | Rn
0xEBB00F30 CMP{<c>}{<q>} <Rn>, <Rm>, RRX T32 1110101 | 1101 | 1 | Rn | 0 | 000 | 1111 | 00 | 11 | Rm
0xEBB00F00 CMP{<c>}.W <Rn>, <Rm> T32 1110101 | 1101 | 1 | Rn | 0 | imm3 | 1111 | imm2 | stype | Rm
0x01500010 CMP{<c>}{<q>} <Rn>, <Rm>, <type> <Rs> A32 cond | 00010 | 10 | 1 | Rn | 0 | 0 | 0 | 0 | Rs | 0 | stype | 1 | Rm

Description

Compare (register) subtracts an optionally-shifted register value from a register value. It updates the condition flags based on the result, and discards the result.

Operation

if ConditionPassed() then
    EncodingSpecificOperations();
    shifted = Shift(R[m], shift_t, shift_n, PSTATE.C);
    (result, nzcv) = AddWithCarry(R[n], NOT(shifted), '1');
    PSTATE.<N,Z,C,V> = nzcv;