Branch Instructions
There are two kinds of branch instructions:
Unconditional jump instructions: upon their execution a jump to a new
location from where the program continues execution is executed.
Conditional jump instructions: a jump to a new program location is
executed only if a specified condition is met. Otherwise, the program
normally proceeds with the next instruction.
ACALL addr11 - Absolute subroutine call
addr11: Subroutine address
Description: Instruction unconditionally calls a subroutine
located at the specified code address. Therefore, the current
address and the address of called subroutine must be within the same
2K byte block of the program memory, starting from the first byte of
the instruction following ACALL.
EXAMPLE:
Before execution: PC=0123h
After execution: PC=0345h
AJMP addr11 - Absoulte jump
addr11: Jump address
Description: Program continues execution after executing a
jump to the specified address. Similar to the ACALL instruction, the
jump must be executed within the same 2K byte block of program memory
starting from the first byte of the instruction following AJMP.
EXAMPLE:
Before execution: PC=0345h SP=07h
After execution: PC=0123h SP=09h
CJNE A,direct,rel - Compares direct byte to the accumulator and jumps if not equal
A: accumulator
Direct: arbitrary register with address 0-255 (0-FFh)
addr: jump address
Description: Instruction first compares the number in the
accumulator with the directly addressed byte. If they are equal, the
program proceeds with execution. Otherwise, a jump to the specified
address will be executed. This is a short jump instruction, which
means that the address of a new location must be relatively near the
current one (-128 to +127 locations relative to the first following
instruction).
EXAMPLE:
Before execution: PC=0145h A=27h
After execution: if MAX≠27: PC=0123h
If MAX=27: PC=0146h
CJNE A,#data,rel - Compares immediate data to the accumulator and jumps if not equal
A: accumulator
Data: constant in the range of 0-255 (0-FFh)
Description: Instruction first compares the number in the accumulator
with the immediate data. If they are equal, the program proceeds
with execution. Otherwise, a jump to the specified address will be
executed. This is a short jump instruction, which means that the
address of a new location must be relatively near the current one (-128
to +127 locations relative to the first following instruction).
EXAMPLE:
Before execution: PC=0445h
After execution: If A≠33: PC=0423h
If A=33: PC=0446h
CJNE Rn,#data,rel - Compares immediate data to the register Rn and jumps if not equal
Rn: Any R register (R0-R7)
Data: Constant in the range of 0 - 255 (0-FFh)
addr: Jump address
Description: Instruction first compares immediate data to the
register Rn. If they are equal, the program proceeds with execution.
Otherwise, a jump to the specified address will be executed. This is
a short jump instruction, which means that the address of a new
location must be relatively near the current one (-128 to + 127
locations relative to the first following instruction).
EXAMPLE:
Before execution: PC=0345h
After execution: If R5≠44h: PC=0323h
If R5=44h: PC=0346h
CJNE @Ri,#data,rel - Compares immediate data to indirectly addressed register and jumps if not equal
Ri: Register R0 or R1
Data: Constant in the range of 0 - 255 (0-FFh)
Description: This instruction first compares immediate data to
indirectly addressed register. If they are equal, the program proceeds
with execution. Otherwise, a jump to the specified address in the
program will be executed. This is a short jump instruction, which means
that the address of a new location must be relatively near the current
one (-128 to +127 locations relative to the next instruction).
EXAMPLE:
Before execution: Register Address SUM=F3h
PC=0345h R0=F3h
After execution: If SUM≠44h: PC=0323h
If SUM=44h: PC=0346h
DJNZ direct,rel - Decrements direct byte by 1 and jumps if not 0
Direct: arbitrary register with address 0-255 (0-FFh)
addr: Jump address
Description: This instruction first decrements value in the
register. If the result is 0, the program proceeds with execution.
Otherwise, a jump to the specified address in the program will be
executed. As it is direct addressing, the register must be within
the first 255 locations of RAM. This is a short jump instruction, which
means that the address of a new location must be relatively near the
current one (-128 to +127 locations relative to the first following
instruction).
EXAMPLE:
Before execution: PC=0445h
After execution: If CNT≠0: PC=0423h
If CNT=0: PC=0446h
DJNZ Rn,rel - Decrements the Rn register by 1 and jumps if not 0
Rn: any R register (R0-R7)
addr: jump address
Description: This instruction first decrements the value in
the Rn register. If the result is 0, the program proceeds with
execution. Otherwise, a jump to the specified address in the program
will be executed. This is a short jump instruction, which means that
the address of a new location must be relatively near the current
one (- 128 to +127 locations relative to the first following
instruction).
EXAMPLE:
Before execution: PC=0445h
After execution: If R1≠0: PC=0423h
If R1=0: PC=0446h
JB bit,rel - Jump if direct bit is set
addr: Jump address
Bit: any bit of RAM
Description: If the bit is set, a jump to the specified
address will be executed. Otherwise, if the value of bit is 0, the
program proceeds with the next instruction. This is a short jump
instruction, which means that the address of a new location must be
relatively near the current one (-128 to + 127 locations relative to
the first following instruction).
EXAMPLE:
Before execution: PC=0323h
After execution: If P0.5=0: PC=0324h
If P0.5=1: PC=0345h
JC rel - Jump if carry flag is set
addr: Jump address
Description: Instruction first checks if the carry flag is
set. If set, a jump to the specified address is executed. Otherwise,
the program proceeds with the next instruction. This is a short
jump instruction, which means that the address of a new location
must be relatively near the current one (-129 to + 127 locations
relative to the first following instruction).
EXAMPLE:
Before instruction: PC=0323h
After instruction: If C=0: PC=0324h
If C=1: PC=0345h
JBC bit,rel - Jump if direct bit is set
Bit: any bit of RAM
addr: Jump Address
Description: This instruction first checks if the bit is set.
If set, a jump to the specified address is executed and the bit is
cleared. Otherwise, the program proceeds with the first following
instruction. This is a short jump instruction, which means that the
address of a new location must be relatively near the current one
(-129 to + 127 locations relative to the first following instruction).
EXAMPLE:
Before execution: PC=0323h
After execution: If TEST0.4=1: PC=0345h, TEST0.4=0
If TEST0.4=0: PC=0324h, TEST0,4=0
JNB bit,rel - Jump if direct bit is not set
addr: Jump address
Bit: any bit of RAM
Description: If the bit is cleared, a jump to the specified
address will be executed. Otherwise, if the bit value is 1, the
program proceeds with the first following instruction. This is a
short jump instruction, which means that the address of a new location
must be relatively near the current one (-129 to + 127 locations
relative to the first following instruction).
EXAMPLE:
Before execution: PC=0323h
After execution: If P0.5=1: PC=0324h
If P0.5=0: PC=0345h
JMP @A+DPTR - Jump indirect relative to the DPTR
A: accumulator
DPTR: Data Pointer
Description: This instruction causes a jump to the address
calculated by adding value stored in the accumulator to the 16-bit
number in the DPTR Register. It is used with complex program
branching where the accumulator affects jump address, for example
when reading a table. Neither accumulator nor DPTR register are
affected.
EXAMPLE:
Before execution: PC=223 DPTR=1400h
After execution: PC = 1402h if A=2
PC = 1404h if A=4
PC = 1406h if A=6
Note:
As instructions AJMP LABELS occupy two locations
each, the values in the accumulator specifying them must be
different from each other by 2.
JNZ rel - Jump if accumulator is not zero
addr: Jump Address
Description: This instruction checks if the value stored in
the accumulator is 0. If not, a jump to the specified address will
be executed. Otherwise, the program proceeds with the first
following instruction. This is a short jump instruction, which means
that the address of a new location must be relatively near the
current one (-129 to + 127 locations relative to the first following
instruction).
EXAMPLE:
Before execution: PC=0323h
After execution: If A=0: PC=324h
If A≠0: PC=283h
JNC rel - Jump if carry flag is not set
addr: Jump Address
Description: This instruction first checks whether the carry
flag is set. If not, a jump to the specified address will be
executed. Otherwise, the program proceeds with the first following
instruction. This is a short jump instruction, which means that the
address of a new location must be relatively near the current one
(-129 to + 127 locations relative to the first following
instruction).
EXAMPLE:
Before execution: PC=0323h
After execution: If C=0: PC=360h
If C=1: PC=324h
LCALL addr16 - Long subroutine call
addr16: Subroutine Address
Description: This instruction unconditionally calls a
subroutine located at the specified address. The current address and the
start of the subroutine called can be located anywhere within the
memory space of 64K.
EXAMPLE:
Before execution: PC=0123h
After execution: PC=1234h
JZ rel - Jump if accumulator is zero
addr: Jump Address
Description: The instruction checks whether the value stored
in the accumulator is 0. If yes, a jump to the specified address will
be executed. Otherwise, the program proceeds with the following
instruction. This is a short jump instruction, which means that the
address of a new location must be relatively near the current one
(-129 to + 127 locations relative to the first following
instruction).
EXAMPLE:
Before execution: PC=0323h
After execution: If A0: PC=324h
If A=0: PC=283h
LJMP addr16 - Long jump
addr16: jump address
Description: Instruction causes a jump to the specified 16-bit address.
EXAMPLE:
Before execution: PC=0123h
After execution: PC=1234h
RET - Return from subroutine
Description: This instruction ends every subroutine. After execution,
the program proceeds with the instruction following an ACALL or
LCALL.
EXAMPLE:
RETI - Return from interrupt
Description: This instruction ends every interrupt routine and
informs processor about it. After executing the instruction, the
program proceeds from where it left off. The PSW is not automatically
returned its pre-interrupt status.
SJMP rel - Short Jump (relative address)
addr: Jump Address
Description: Instruction enables jump to the new address which
should be in the range of -128 to +127 locations relative to the
first following instruction.
EXAMPLE:
Before execution: PC=323
After execution: PC=345