8051 Conditional Branch Instructions


CONDITIONAL BRANCH INSTRUCTIONS

CJNE

The CJNE instruction compares the first two operands and branches to the specified destination if their values are not equal. If the values are the same, execution continues with the next instruction.

format
example
Operation
Opcode/ Encoding
Flags affected
Byte
Cycles
CJNE @Rn, #immd,offset
CJNE @R1, #24h,label
PC=PC+3
If (Rn) <>#data
PC=PC+offset
If(Rn) <#data
C=1
Else C=0
1011011n, #imm 8-bit, offset
C
3
2
CJNE A, #data, offset
CJNE A, #01h, label
PC=PC+3
If A <> #immd
PC=PC+offset
If A<#immd
C=1
Else
C=0
10110100, #immd, offset
C
3
2
CJNE A, direct, offset
CJNE A, 60h, label
PC=PC+3
If A<> (direct)
PC=PC+offset
If A < (direct)
C=1
Else
C=0
10110101, direct 8-bit adder, offset
C
3
2
CJNE Rn, #immd, offset
CJNE R6, #12h, label
PC=PC+3
If Rn <> immediate
PC=PC + offset
If Rn < immd
C=1
Else
C=0
10111nnn, #immd 8-bit, offset
C
3
2

 DJNZ

The DJNZ instruction decrements the byte indicated by the first operand and, if the resulting value is not zero, branches to the address specified in the second operand.

format 
example
Operation
Opcode/ Encoding
Flags affected
Byte
Cycles
DJNZ direct, offset
DJNZ 40h,label
PC PC+2
(direct)=(direct)-1
If (direct)<>0
PC=PC+offset
11010101, direct addr 8-bit, offset 8-bit
none
3
2
DJNZ Rn, offset
DJNZ R6, Label
PC=PC+2
Rn=Rn-1
If Rn<>0
PC=PC+offset
11011nnn, offset 8-bit
none
2
2

JB

The JB instruction branches to the address specified in the second operand if the value of the bit specified in the first operand is 1. The bit that is tested is not modified. No flags are affected by this instruction.

format
example
Operation
Opcode/Encoding
Flags affected
Byte
Cycles
JB bit, offset
JB P1.2, label
PC=PC+3
If (bit)=1
PC=PC+offset
00100000, bit, offset
none
3
2

JNB

The JNB instruction branches to the specified address if the specified bit operand has a value of 0. Otherwise, execution continues with the next instruction. No flags are affected by this instruction.

format
example
Operation
Opcode/Encoding
Flags affected
Byte
Cycles
JNB bit, offset
JNB P1.3, label
PC=PC+3
If (bit)=0
PC=PC+offset
00100000, bit, offset
none
3
2

JBC

The JBC Jump to branch address if Bit set, clear bit after branch

JBC bit, offset

PC=PC+3

If bit set

then

Jump to specified address if bit is set

clear the bit after

else

continue normal execution
Note
When this instruction is used to modify an output port, the value used as the port data is read from the output data latch, not the input pins of the port.

format
example
Operation
Opcode/Encoding
Flags affected
Byte
Cycles
JBC bit, offset
JBC P1.2, 44h
PC=PC+3
If (bit)=1
(bit)=0
PC=PC+offset
00010000, bit, offset
none
3
2

JC

Jump if carry bit in PSW is set

If not set; execution continues with the next instruction.

No flags are affected by this instruction.

format
example
Operation
Opcode/Encoding
Flags affected
Byte
Cycles
JC  offset
JC label
PC=PC+2
If  C=1
PC=PC+offset
01000000,  offset
none
2
2

JNC

Jump if carry not set (i.e. carry clear)

Otherwise, execution continues with the next instruction.

No flags are affected by this instruction.

format
example
Operation
Opcode/Encoding
Flags affected
Byte
Cycles
JNC  offset
JC label
PC=PC+2
If  C=0
PC=PC+offset
01010000,  offset
none
2
2

JZ

Jump if result in Accumulator is zero

Otherwise, the next instruction is executed.

Neither the accumulator nor any flags are modified by this instruction.

format
example
Operation
Opcode/Encoding
Flags affected
Byte
Cycles
JZ offset
JZ label
PC=PC+2
If  A=0
PC=PC+offset
01100000,  offset
none
2
2

JNZ

The JNZ instruction transfers control to the specified address if the value in the accumulator is not 0. If the accumulator has a value of 0, the next instruction is executed. Neither the accumulator nor any flags are modified by this instruction.

format
example
Operation
Opcode/ Encoding
Flags affected
Byte
Cycles
JNZ offset
JNZ label
PC=PC+2
If  A< >0
PC=PC+offset
01110000,  offset
none
2
2

 

error: Content is protected !!