Bit-Based Decisions and Branching (JB, JBC, JNB, JC, JNC)

16.13 Bit-Based Decisions and Branching (JB, JBC, JNB, JC, JNC)

It is often useful, especially in microcontroller applications, to execute different code based on whether or not a given bit is set or cleared. The 8052 instruction set offers five instructions that do precisely that.

JB means jump if bit set. The MCU checks the specified bit and, if it is set, jumps to the specified address or label.

JBC means jump if bit set, and clear bit. This instruction is identical to JB ex- cept that the bit is cleared if it was set. That is to say, if the specified bit is set, the MCU jumps to the specified address or label, and also clears the bit. In some cases, this can save you the use of an extra CLR instruction.

JNB means jump if bit not set. This instruction is the opposite of JB. It tests the specified bit and jumps to the specified label or address if the bit is not set.

JC means jump if carry set. This is the same as the JB instruction, but it only tests the carry bit. An additional instruction was included in the instruction set to test for this common condition because many operations and decisions are based on whether or not the carry flag is set. Thus, instead of using the instruc- tion JB C,label, which takes 3 bytes of program memory, the programmer may use JC label, which only takes 2.

JNC means jump if carry bit not set. This is the opposite of JC. This instruction tests the carry bit and jumps to the specified label or address if the carry bit is clear.

Some examples of these instructions are:

JB

40h,LABEL1

;Jumps to LABEL1 if user bit

40h

is

set

JBC 45h,LABEL2

;Jumps to LABEL2 if user bit 45h set, then clears it

JNB 50h,LABEL3

;Jumps to LABEL3 if user bit

50h

is

clear

JC

LABEL4

;Jumps

to

LABEL4

if

the

carry

bit is

set

JNC LABEL5

;Jumps

to

LABEL5

if

the

carry

bit is

clear

These instructions are very common, and very useful. Virtually all 8052 assembly language programs of any complexity will use them—especially the JC and JNC instructions.

8052 Assembly Language

16-15

 

Page 209
Image 209
Texas Instruments MSC1210 manual Bit-Based Decisions and Branching JB, JBC, JNB, JC, JNC