14 value comparison (cjne) – Texas Instruments MSC1210 User Manual
Page 210
Value Comparison (CJNE)
16-16
16.14 Value Comparison (CJNE)
CJNE (compare, jump if not equal) is a very important instruction. It is used to
compare the value of a register to another value and branch to a label based
on whether or not the values are the same. This is a very common way of
building a switch
…
case decision structure or an IF
…
THEN
…
ELSE structure
in assembly language.
The CJNE instruction compares the values of the first two parameters of the
instruction and jumps to the address contained in the third parameter, if the first
two parameters are not equal.
CJNE
A,#24h,NOT24
;Jumps
to
the
label
NOT24
if
accumulator
isn’t
24h
CJNE
A,40h,NOT40
;Jumps to the label NOT40 if accumulator is
;different than the value contained in Internal
;RAM address 40h
CJNE
R2,#36h,NOT36
;Jumps to the label NOT36 if R2 isn’t 36h
CJNE
@R1,#25h,NOT25 ;Jumps to the label NOT25 if the Internal RAM
;address pointed to by R1 does not contain 25h
As shown, the MCU compares the first parameter to the second parameter.
If they are different, it jumps to the label provided; if the two values are the
same then execution continues with the next instruction. This allows for the
programming of extensive condition evaluations.
For example, to call the PROC_A subroutine if the accumulator is equal to 30
H
,
call the CHECK_LCD subroutine if the accumulator equals 42
H
, and call the
DEBOUNCE_KEY subroutine if the accumulator equals 50
H
. This could be im-
plemented using CJNE as follows:
CJNE A,#30h,CHECK2
;If A is not 30h, jump to CHECK2 label
LCALL PROC_A
;If A is 30h, call the PROC_A subroutine
SJMP CONTINUE
;When we get back, we jump to CONTINUE label
CHECK2:
CJNE A,#42h,CHECK3
;If A is not 42h, jump to CHECK3 label
LCALL CHECK_LCD
;If A is 42h, call the CHECK_LCD subroutine
SJMP CONTINUE
;When we get back, we jump to CONTINUE label
CHECK3:
CJNE A,#50h,CONTINUE
;If A is not 50h, we jump to CONTINUE label
LCALL DEBOUNCE_KEY
;If A is 50h, call the DEBOUNCE_KEY subroutine
CONTINUE: {Code continues here}
;The rest of the program continues here
As shown, the first line compares the accumulator with 30
H
. If the accumulator
is not 30
H
, it jumps to CHECK2, where the next comparison is made. If the ac-
cumulator is 30
H
, however, program execution continues with the next instruc-
tion, which calls the PROC_A subroutine. When the subroutine returns, the
SJMP instruction causes the program to jump ahead to the CONTINUE label,
thus bypassing the rest of the checks.
The code at label CHECK2 is the same as the first check. It first compares the
accumulator with 42
H
and then either branches to CHECK3, or calls the
CHECK_LCD subroutine and jumps to CONTINUE. Finally, the code at
CHECK3 does a final check for the value of 50
H
. This time there is no SJMP
instruction following the call to DEBOUNCE_KEY because the next instruction
is CONTINUE.