CMP

From ARMwiki
(Difference between revisions)
Jump to: navigation, search
m (Tweak.)
(Added more descriptive title.)
 
Line 8: Line 8:
 
}}
 
}}
  
==CMP==
+
==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.<br>
 
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.<br>
 
Operand 1 is a [[Registers|register]], operand 2 may be a register, [[Shifts|shifted register]], or an immediate value (which may be [[Shifts|shifted]]).
 
Operand 1 is a [[Registers|register]], operand 2 may be a register, [[Shifts|shifted register]], or an immediate value (which may be [[Shifts|shifted]]).

Latest revision as of 07:01, 22 December 2011

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

Contents

[edit] 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.

[edit] Syntax

  CMP  <op 1>, <op 2>

[edit] Function

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

[edit] 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 ('.')

[edit] 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