0% found this document useful (0 votes)
23 views

branch instructions

Uploaded by

Darshan Aher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

branch instructions

Uploaded by

Darshan Aher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

26.

4 BRANCH JUMP) INSTRUCTIONS


********

s051 instructions have very powerful branch nstructions. When a


branch instruction is executed by the
sO51. a jump occurs, which can be a forward or a backward
jump. It can perform branch unconditionally or
based on a flag value. The 805l supports two types of jump instructions.
.Unconditional Jump Instructions
Conditional Jump Instructions
Unconditional Jump Instructions
The execution of this instruction always results in branch. The destination address is
a
provided as a part of
the instruction.
LJMP addr16 LJMP causes an unconditional branch to the indicated address,
by loading the program
counter with 16 bits address i.e. the second and third byte of the instruction
respectively.

EXAMPLE 3.103

LUMP 1200 [PC] 6 1200

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)

Syre>.) CINE compares the magni


Comepare arnd joumag ifnot eyudl (CINE <ist-èvte>, ade
of the tirst wo operands, and branches it
their values
are not qual. Branch address is computed by
set. if' unsigtnd intu
ot Program Counter (P©). The cary tag is
the signed displacement to the contents
value of sn-byte, else carry lay is clearc
value of <dest-byte is less than the unsignod integer
CINE A. direct. rel
Operation: [PC]-[PC}+3
IF(A]= M{direct)
THEN(PC]-[PC}+displacement
FA] M[direct]
THEN(CY]-
ELSE CY]-0

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

Operation: [PC]- [PC]+3


IF M [[Ri]] # # data
THEN [PC]-[PC]+displacement
IF M[[Ri]]< # data
THEN[CY]-1
ELSE [CY]-

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.

Operation: [PC] -[PC]+2


IF LA] =
0 THEN [PC]-[PC] +displacement
Jump if A is not zero (JNZ rel) IfA is not 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.

Operation: [PC] -[PC]+2


IF [A]# 0 THEN [PC] -[PC] + displacement

Conditional Jump Instructions


n this group, the instruction performs a branch, based on the condition of a single status flag. The status
ag used in this instruction is only a carry fiag.

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

MOV R2, #FFH Load FFH t o R2


decrement R2 until i t becomes zero
LOOP: DJNZ R2, LOOP

EXAMPLE 3.108 |||


Write 8o51 instructions to rotate the contents of A left by two positions.
ALGORITHM

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

Load data 25H to A


Step 1:
Cten 2: Load data 15H to memory location 20DH

Step 3: Load 00H to register R3


Step 4: Clear carry flag
Step 5: Sub contents oE location 20H and contents of CY ílag from
contents of A

Step 6: If contentS are equal, branch to Step 8

Step 7: Decrement R3, then R3 contains FFH


Step 8: Store contents of R3 in location 21H

MOV A, #25H Load data 25H to A


MOV 20, #15H Load data 15H to location 20H
MOV R3, #00 Load data 00H to R3
CLR C C l e a r carry ilag
SUBB A, 20H :Sub contents of location 20H and C flag
from the contents of A
JZ LOOP1 I f result is zero, branch to LOOP1
DEC R3 Decrement R3
LOOP1 MOV 21H, R 3 I f result is zero, store 00 in location 21H,
else store FF

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

MOV A, 177H o a d data 77H Lo A


MOV R2, 99H load data 99i to R
MOV R3, #00 Load data 00H to R3
ADD A, R2 Add cOntent3 of A with R2
MOV 2OH, A Store contents of A in Tocation 20H
JNC 10OP I f carry ag is resel, branch to LOOP
TNC R3 Increnent R3

1.OOP1: MOV 21H, R3; Store contents of R3 in Jocation 21H

You might also like