Microprocessor & Microcontrollers: Jump, Loop, and Call Instructions
Microprocessor & Microcontrollers: Jump, Loop, and Call Instructions
MICROCONTROLLERS
Chapter 3
Jump, loop, and Call Instructions
11/2/20 www.iiu.edu.pk 1
Loop and Jump Instructions (DJNZ)
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. ; Decrement Jump if Not 0
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.
In this instruction both the register decrement and the
decision to jump arc combined into a single instruction.
The registers can be any of R0 - R7. The counter can also
be a RAM location
11/2/20 www.iiu.edu.pk 2
Loop and Jump Instructions (DJNZ)
Example 3-1
Write a program to (a) clear ACC, then
(b) add 3 to the accumulator ten times.
Solution:
; In this program, R2 is used to keep the inner loop count. In the instruction
; "DJNZ R2, AGAIN", whenever R2 becomes 0 it falls through and
; "DJNZ R3, next" is executed- This instruction forces the CPU to load R2
; with the count 70 and the inner loop starts again. This process will continue
; until R3 becomes zero and the outer
11/2/20 loop is finished.
www.iiu.edu.pk 4
8051 Conditional Jump Instructions
11/2/20 www.iiu.edu.pk 5
8051 Conditional Jump Instructions
JZ (jump if A = 0)
In this instruction the content of register A is checked. If it is zero, it
jumps to the target address.
JZ instruction can be used only for register A.
It can only check to see whether the accumulator is zero, and it does not
apply to any other register.
Don't have to perform an arithmetic instruction such as decrement to
use the JZ instruction.
JNZ (jump if A 0)
In this instruction the content of register A is checked. If it is not zero, it
jumps to the target address.
Example 3-4
Write a program to determine if R5 contains the value 0. If so put 55H in it.
Solution:
MOV A, R5 ; copy R5 to A
JNZ NEXT ; j ump if A
MOV R5, #55H
NEXT
11/2/20: ... www.iiu.edu.pk 6
Loop inside a Loop (Nested Loop)
Example 3-5
Find the sum of the values 79H, F5H, and E2H. Put the sum in register's R0
(low byte) and R5 (high byte).
Solution:
MOV A, #0 ; Clear A (A = 0)
MOV R5, A ; Clear R5
ADD A, #79H ; A=0+79H=79H
JNC N_1 ; if no carry, add next number
INC R5 ; if CY=1, increment R5
N_l: ADD A,#0F5H ; A-79+F5=6E and CY=1
JNC N_2 ; jump if CY=0
INC R5 ; if CY=1, increment 5
JNC OVER
N_2: ADD A, #0E2H ; Now R0=50H, and R5=02
incomplete
11/2/20 www.iiu.edu.pk 7
8051 Conditional Jump Instructions
11/2/20 www.iiu.edu.pk 8
END OF
CHAPTER 3
11/2/20 www.iiu.edu.pk 9