CMP

From ARMwiki
Revision as of 08:01, 22 December 2011 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
CMP
Instruction CMP
Function Compare
Category Data processing
ARM family All
Notes -

Contents

CMP : Compare

CMP permits you to compare two values, automatically updating the flags to reflect the result of the comparison. Internally, CMP subtracts the value of operand two from operand one.
Operand 1 is a register, operand 2 may be a register, shifted register, or an immediate value (which may be shifted).

There is no S bit, it is implied. The N, Z, V, and C flags are updated as usual (N - result bit 31, Z if result zero, V if overflow, C if NOT borrow).

CMP is important in conditional execution and decision making.

There is also a Compare Negative instruction for comparing negated values.

Syntax

  CMP  <op 1>, <op 2>

Function

  <flags> = op_1 - op_2  ; result is not stored, only flags updated

Example

  ; Mask off control codes, R0 is ASCII code. Passes high-ASCII.
  CMP    R0, #32    ; compare with 32 (space)
  MOVLT  R0, #46    ; if less, replace with 46 ('.')
  CMP    R0, #127   ; compare with 127 (backspace)
  MOVEQ  R0, #46    ; if equal, replace with 46 ('.')

Technical

The instruction bit pattern is as follows:

31 - 28 27 26 25 24 - 21 20 19 - 16 15 - 12 11 - 0
condition 0 0 I 1 0 1 0 1 op_1 0 0 0 0 op_2/shift
Personal tools
Namespaces

Variants
Actions
Navigation
Contents
Toolbox