branch instructions
branch instructions
EXAMPLE 3.103
AJMP addrl 1 AJMP causes an unconditional branch to the indicated address, by loading the 11 bit
address to 0 to 10 bits of the program counter. The destination must therefore be within the same 2K blocks.
EXAMPLE 3.104
AJMP 200 [PC 10-01 200
S0 S51 NMierovontrollee: Handware Sattwar & Applications
SMP el The braneh destanation is conmputed by adding the signed displacement to the
pogram counter. Therefore it branches thom 128 bytes prevethng this instrawtin to 127 bytes fil
EXAMPLE .105 ||
SMP5
UMP lndirevt The eontents of an A is added (N bit unsigned) with l6 bit vontents of data poine
this l6 bit is loadead to program oounter. Neither the A nor the data pointer is altend and no tags are ars
EXANPLE .106||
JUME 8A DPTR: EC A+DPTR)
CINE A. #data.rel
Operation: [PC]-{PC}-3
IF[A] #data
THEN .PC]-{PC}*displacement
IF[A Fdata
TIHENICY]-1
ELSE CY] - 0
CJNE Rn.#data,rel
Operation: [PC}-[PC}+3
IF [Rnl #data
THENPC}e- {PC}dispBacement
F Rn] #data
TIUENCY}-1
FLSECY}- 0
Chapter 3 8051 Addressing Modes and Instruction Set 81
CJNE @Ri,#data,rel
Decrement and jumnp if not zero (DJNZ <byte>, Srel-addr>) DJNZ decrements the contents of the
memory or register by 1, and if the resulting value is not zero, it branches to the relative address indicated
by the second operand. No flags are affected and the branch destination is computed by adding the signed
value to the contents of program counter.
displacement
DJNZ Rn, rel
Operation: [PC]- [PC] +2
[Rn]-[Rn] - 1
IF [Rn] # 0
THEN [PC]-[PC]+ displacement
DINZ direct,rel
Operation: [PC]-[PC] +2
M[direct]-M[direct] - 1
IF M[direct] # 0
THEN[PC]-[PC]+displacement
Jump if A is zero (JZ rel) If A is zero, branch to the address indicated; otherwise execute the next
instruction. The branch address is computed by adding the signed displacement to the contents of PC, after
incrementing the PC twice. Flags are not affected.
p ifcarry is set (IC rel) If the carry flag is set, branch to the address indicated; otherwise execute
e next instruction. Flags are not afected. The branch destination is computed by adding the signed
ptacenment to the contents of PC, after incrementing the contents of PC by 2.
is not set, branch to the
address d,
indicated: otherw
not set (JNC rel)
Ifthe carry flag destination is computed by ad
Jump ifcarry is
instruction. Flags are
not affected.
The branch
contents of PC by 2.
dding t
execute the incrementing the
next
to the contents of PC, after
signed displacement
Operation: [PC]-[PC]+2
[PCJ-[PC]+displacement
IF (CY]-0 THEN
to become familiar
with JUMP instructions.
We will study following examples
EXAMPLE 3.107
becomes zero.
Write 8051 instructions to decrement
the contents of R2 until it
ALGORITHM
to register R2
Step 1: Load data
R2 u n t i l i t becomes
zero.
2: Decrement
Step
Instructions are as fOllowsS
1: Load data to A
Step
Load 02 to register R2
Step 2 position
3: Rotate contents of A by
left one
Step becomes zero
3 until R2
4: Decrement R2 and repeat Step
Step
Instructions are as follows
25H to A
MOV A, #25H Load
02H to R2
MOV R2, #02H Load
LOOP: RLA Rotate contents of A l e f t by one position
to LOOP u n t i l R2 becomes
DJNZ R2, LOOP; Decrement R2 and branch
zero
Chapter 3 8051 Addressing Modes and Instruction Set 83
EXAMPLE3.109
Write 805a instructions to compare the contents of A with the contents of 20H. If contents are equal,
in 21H, else store FFH in 21H.
store ooH
ALGORITHM
EXAMPLE 3.110
Write Bosa instructions to add two 8 bit numbers and store 16 bit results in location 2oH and 21H.
ALGORITHM
Step 1: Load data 77H to A
Step 2: Load data 99H to register R2
Step 3: Load 00H to register R3
Step 4: Add contents of register R2 with the contents of A
Step 5: Store contents of A (LSB result) in memory location 20H
e p 6: If carry ilag is reset, branch to Step 8
t e p 7: Increment R3, then R3 contains 01H
e p 8: Store contents of R3 in lo cation 21H
84 8O51 Microcontroller: Hardware, Software & Applications