0% found this document useful (0 votes)
87 views9 pages

Microprocessor & Microcontrollers: Jump, Loop, and Call Instructions

This document discusses loop, jump, and call instructions for microprocessors and microcontrollers. It provides examples of using DJNZ instructions to implement loops that repeat a sequence of instructions a specified number of times. It also gives an example of a nested loop using two registers to allow looping more than 256 times. Additionally, it covers the 8051's conditional jump instructions like JZ and JNZ and provides an example of using them to conditionally jump based on the value in register A.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views9 pages

Microprocessor & Microcontrollers: Jump, Loop, and Call Instructions

This document discusses loop, jump, and call instructions for microprocessors and microcontrollers. It provides examples of using DJNZ instructions to implement loops that repeat a sequence of instructions a specified number of times. It also gives an example of a nested loop using two registers to allow looping more than 256 times. Additionally, it covers the 8051's conditional jump instructions like JZ and JNZ and provides an example of using them to conditionally jump based on the value in register A.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MICROPROCESSOR &

MICROCONTROLLERS
Chapter 3
Jump, loop, and Call Instructions

Engr. Rashid Farid Chishti


Faculty of Engineering & Technology
International Islamic University Islamabad
e-mail: [email protected]

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:

;This program adds value 3 to the ACC ten times


MOV A,#0 ;A=0, Clear ACC
MOV R2,#10 ;load counter R2=1O
AGAIN: ADD A,#03 ;add 03 to ACC
DJNZ R2,AGAIN ;repeat until R2=0{10 times)
MOV R5,A ;save A in R5

; Since R2 holds the count and R2 is an 8-bit register,


; it can hold a maximum of FFH (255 decimal)
; therefore, the loop can be repeated a maximum of 256 times.

What happens if we want to repeat an


11/2/20
action more times than 256?
www.iiu.edu.pk 3
Loop inside a Loop (Nested Loop)
Example 3-3
Write a program to (a) load the accumulator with the value 55H, and (b)
complement the ACC 700 times.
Solution: Since 700 is larger than 255 (the maximum capacity of any
register), we use two registers to hold the count. The following code shows
how to use R2 and R3 for the count

MOV A, #55H ;A=55H


MOV R3, #10 ;R3=l0, the cuter loop count
NEXT: MOV R2, #70 ;R2=70, the inner loop count
AGAIN: CPL A ;complement A register
DJNZ R2, AGAIN ;repeat it 70 times (inner loop)
DJNZ R3, NEXT

; 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

You might also like