Texas Instruments MSP430x1xx User Manual

Page 372

Advertising
background image

Instruction Set Overview

B-22

CMP[.W]

Compare source and destination

CMP.B

Compare source and destination

Syntax

CMP

src,dst or

CMP.W

src,dst

CMP.B

src,dst

Operation

dst + .NOT.src + 1
or
(dst – src)

Description

The source operand is subtracted from the destination operand. This is
accomplished by adding the 1s complement of the source operand plus 1. The
two operands are not affected and the result is not stored; only the status bits
are affected.

Status Bits

N: Set if result is negative, reset if positive (src >= dst)
Z: Set if result is zero, reset otherwise (src = dst)
C: Set if there is a carry from the MSB of the result, reset otherwise
V: Set if an arithmetic overflow occurs, otherwise reset

Mode Bits

OscOff, CPUOff, and GIE are not affected.

Example

R5 and R6 are compared. If they are equal, the program continues at the label
EQUAL.

CMP

R5,R6

; R5 = R6?

JEQ

EQUAL

; YES, JUMP

Example

Two RAM blocks are compared. If they are not equal, the program branches
to the label ERROR.

MOV

#NUM,R5

; number of words to be compared

L$1

CMP

&BLOCK1,&BLOCK2

; Are Words equal?

JNZ

ERROR

; No, branch to ERROR

DEC

R5

; Are all words compared?

JNZ

L$1

; No, another compare

Example

The RAM bytes addressed by EDE and TONI are compared. If they are equal,
the program continues at the label EQUAL.

CMP.B EDE,TONI

; MEM(EDE) = MEM(TONI)?

JEQ

EQUAL

; YES, JUMP

Example

Check two keys connected to port pins P0 and P1. If key1 is pressed, the pro-
gram branches to label MENU1; if key2 is pressed, the program branches to
MENU2.

P0IN

.EQU

010h

KEY1

.EQU

01h

KEY2

.EQU

02h

CMP.B #KEY1,&P0IN
JEQ

MENU1

CMP.B #KEY2,&P0IN
JEQ

MENU2

Advertising