CMN

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

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

Contents

CMN : Compare Negative

CMN permits you to compare two values, automatically updating the flags to reflect the result of the comparison. Internally, CMP subtracts the value of NOT operand two from operand one (effectively an addition).
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).

CMN is important in conditional execution and decision making.

There is also a Compare instruction for comparing regular values.

Syntax

  CMN  <op 1>, <op 2>

Function

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

Example

  ; Have we reached the end of the list? Data is loaded
  ; into R0, and a value of -1 means we're done.
  CMN    R0, #1     ; compare with -1
  BEQ    finished   ; if true, we're done
  B      next_data  ; else, continue through the list

It is necessary to use CMN for comparisons with negative numbers as CMP won't work correctly (due to subtracting a negative becoming an addition). If you attempt an invalid instruction such as

  CMP    R0, #-1

you may find your assembler either generates an error (bad immediate constant) or, if smarter, substitutes the correct CMN instruction in this case.

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 1 1 op_1 0 0 0 0 op_2/shift
Personal tools
Namespaces

Variants
Actions
Navigation
Contents
Toolbox