CMP

From ARMwiki
(Difference between revisions)
Jump to: navigation, search
m (Added link to CMN.)
m (Tweak.)
Line 19: Line 19:
  
 
===Syntax===
 
===Syntax===
   CMP<suffix> <op 1>, <op 2>
+
   CMP  <op 1>, <op 2>
  
 
===Function===
 
===Function===

Revision as of 16:46, 27 November 2011

CMP
Instruction CMP
Function Compare
Category Data processing
ARM family All
Notes -

Contents

CMP

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