You are on page 1of 18

Loop and Jump Instructions

Repeating a sequence of instructions a certain


number of times is called a loop.
The loop action is performed by the instruction
DJNZ reg,label
In this instruction, the register is decremented; if it is
not zero, it jumps to the target address referred to by
the label.
Prior to the start of the loop the register is loaded
with the counter for the number of repetitions.

Example

Write a program to clear the Acc, then add 3 to the


accumulator ten times.
MOV

A,#0

MOV

R2,#10

AGAIN: ADD

A,#03

DJNZ

R2,AGAIN

MOV

R5,A

Other Conditional Jumps


Instruction

Action

JZ

Jump if A = 0

JNZ

Jump if A 0

DJNZ

Decrement and jump if A 0

CJNE A,byte

Jump if A byte

CJNE reg,#data

Jump if byte #data

JC

Jump if CY = 1

JNC

Jump if CY = 0

JB

Jump if bit = 1

JNB

Jump if bit = 0

JBC

Jump if bit = 1 and clear bit

Unconditional Jump Instructions

All conditional jumps are short jumps, meaning that the address of
the target must be within -128 and +127 bytes of the contents of the
program counter (PC).

Unconditional jump instructions are:

LJMP (Long jump) 3 byte instruction

SJMP (Short jump) 2 byte instruction

CALL instructions

CALL instruction is used to call a subroutine

LCALL (long call) 3 byte instruction

ACALL (absolute call) 2 byte instruction


When

a subroutine is called, control is transferred to that


subroutine.

After

finishing execution of the subroutine, the instruction


RET (return) transfers control back to the caller.

Time Delay Generation and Calculation


For the CPU to execute an instruction takes a certain
number of clock cycles.
In the 8051 family, these clock cycles are referred to
as machine cycles.
We can calculate a time delay using the available
list of instructions and their machine cycles.
In the 8051, the length of the machine cycle
depends on the frequency of the crystal oscillator
connected to the 8051 system.

Time Delay Generation and Calculation


(contd)

The frequency of the crystal connected to the 8051 family can vary
from 4MHz to 30MHz.

In the 8051, one machine cycle lasts 12 oscillator periods.

Therefore, to calculate the machine cycle, we take 1/12 of the


crystal frequency and then take the inverse.

Example

The following shows crystal frequency for three different 8051-based


systems. Find the period of the machine cycle in each case.
(a) 11.0592MHz
(b) 16MHz
(c) 20MHz

Solution
1/11.0592MHz = period per oscillation
Machine cycle = 12x
= 1.085s
1/16MHz = period per oscillation
Machine cycle = 12x
= 0.75s
1/20MHz = period per oscillation
Machine cycle = 12x
= 0.6s

Question

For an 8051 system of 11.0592MHz, find how long it takes to execute


each of the following instructions:
(a) MOV

R3,#55

(b) DEC

R3

(c) DJNZ

R2,target

Solution
(a) MOV

R3,#55

1x1.085s

(b) DEC

R3

1x1.085s

(c) DJNZ

R2,target

2x1.085s

Delay Calculation

A delay subroutine consists of two parts:


(a) setting a counter
(b) a loop

Most of the time delay is performed by the body of the loop.

Very often we calculate the time delay based on the instructions


inside the loop and ignore the clock cycles associated with the
instructions outside the loop.

Largest value a register can hold is 255; therefore, one way to


increase the delay is to use the NOP command.

NOP, which stands for No Operation simply wastes time.

Example

Find the size of the delay in the following program, if the crystal
frequency is 12MHz.

MOV
A,#55H
AGAIN: MOV
P1,A
ACALL
DELAY
CPL
A
SJMP
AGAIN
DELAY: MOV
R3,#200
HERE: DJNZ
R3,HERE
RET
What does the above program do?

Solution
Crystal Cycle
DELAY:

MOV

R3,#200

12

HERE:

DJNZ

R3,HERE

24

RET

12

Therefore, we have a delay of [(200X24)+12+24]x0.083s = 402s

Loop inside a loop delay


Another way to get a large delay is to use a loop inside a loop,
which is also called a nested loop. E.g.
Crystal Cycle
DELAY:MOV
R3,#250
12
HERE: NOP
12
NOP
12
NOP
12
NOP
12
DJNZ
R3,HERE
24
RET
24
Time Delay=[250.(12+12+12+12+24)]x0.083s + (12+24)x0.083s = s

Question

For a machine cycle of 1s, find the time delay of the following
subroutine.

DELAY:
AGAIN:
HERE:

MOV
MOV
NOP
NOP
DJNZ
DJNZ
RET

R2,#200
R3,#250

R3,HERE
R2,AGAIN

Solution
HERE Loop: 250x1s = 250 s
AGAIN Loop: Repeats the HERE loop 200 times i.e.
200x250s = 200ms