0% found this document useful (0 votes)
4 views20 pages

LAB 6 Loop Instructions

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views20 pages

LAB 6 Loop Instructions

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

LAB 6

INSTRUCTION SET
© Dr J.Iqbal
© Dr J.Iqbal
© Dr J.Iqbal
© Dr J.Iqbal
LAB 6

LOOPS IN ASSEMBLY
LOOPS
 Repeating a sequence of instructions a certain
number of times is called a loop
 In the 8051, the loop is performed mostly by the
instruction

DJNZ Rn, label ; 1st decrement Rn and if


; not equal to 0, jump to label
DJNZ direct, label
LOOPS- EXAMPLE

The following program will add 3 to ACC ten times

MOV A, #0 ; A=0, clear ACC


MOV R2, #10 ; load counter R2=10
AGAIN:
ADD A, #03 ; add 03 to ACC
DJNZ R2, AGAIN ; repeat until R2 = 0 (10
; times)
MOV R5, A ; save A in R5
ADDITION OF MORE THEN TWO NUMBERS-
EXAMPLE
Suppose we want to add five numbers stored on RAM
starting from 40H

MOV R0, #40H ; load pointer


MOV R2, #5 ; load counter
CLR A ; A=0
MOV R7, A ; clear R7
AGAIN:
ADD A, @R0 ; add the byte pointer to A
; by R0
JNC NEXT ; if CY=0 don't accumulate
; carry
INC R7 ; keep track of carries
NEXT:
INC R0 ; increment pointer
DJNZ R2, AGAIN ; repeat until R2 is zero
LOOPS- EXERCISE
Transfer the block of data from 20h to 30h to location
40h to 50h after adding 2 to each number using loops

ORG 0
MOV R0, #20h ; initialize R0
MOV R1, #40h ; initialize R1
MOV R2, #11h ; initialize R2 as
; counter
LOOP1:
MOV A, @R0 ; read DATA
MOV @R1, A ; write DATA
INC R0
INC R1
DJNZ R2, LOOP1
LOOPS USING COMPARE INSTRUCTION
CJNE dest-byte, scr-byte, rel-address
; (Compare and jump if not equal)This instruction
compares dest-byte and scr-byte and jump to rel-
address if they are not equal

CJNE A, #34h, NEXT


CJNE R2, #40h, OVER
CJNE A, P1, WAIT
LOOPS USING COMPARE INSTRUCTION
Same example with compare instruction

MOV A, #0 ; A=0, clear ACC


MOV R2, #0 ; load counter R2=0
AGAIN:
ADD A, #03 ; add 03 to ACC
INC R2
CJNE R2, #10, AGAIN ; repeat until R2 = 10 (10
; times)
MOV R5, A ; save A in R5
LOOPS- EXERCISE
Continuously scan port P0. If data is other then 0FFh
multiply it with 10 and send it to port P1 and P2

ORG 0
MOV P0, #0FFh ; make P0 input port
START:
MOV A, P0 ; scan/ read P0
CJNE A, #0FFh, MULT ; compare with 0FFh
SJMP START ; jump to scan again
MULT:
MOV B, #10 ;
MUL AB ; multiply with 10
MOV P1, A ; send low byte to P1
MOV P2, B ; send high byte to P2
SJMP START
EXERCISE
Write a program that finds the position of the first high in
an 8-bit data item and display the result on P0. The data
is scanned from D7 to D0. Give the result for 68H
ORG 0
MOV A, #68h ; Let A = 68h
MOV R0, #8 ; initialize counter
AGAIN:
RLC A ; rotate left through carry
JC FND ; if MSB = 1, jump to FND
DJNZ R0, AGAIN ; repeat
MOV P0, R0 ; ‘1’ not found
SJMP $ ; stay here
FND:
MOV P0, R0 ; ‘1’ found, display its position
SJMP $
END
NESTED LOOPS

As register size is 8-bit, so we can repeat any loop at


maximum 255 times. What should we do if we
want to repeat a loop more than 255 times?

Answer: use a loop inside a loop, which is called a


nested loop
NESTED LOOPS- EXAMPLE
A program to implement ORG 00H
following C code

MOV A, #0
for(i = 0; i<1000; i++)
MOV R0, #4
{
LOOP1:
a = a+2;
MOV R1, #250
}
LOOP2:
ADD A, #2
As
DJNZ R1, LOOP2
1000/255 = 3.92
DJNZ R0, LOOP1

We take
END
1000/250 = 4
NESTED LOOPS- EXERCISE
Write a program to implement following C code in
assembly

for(i = 0; i<7800; i++)


{
a = a+2;
}
LOOK-UP TABLE - EXAMPLE
The following program will take a number ‘n’ (0, 1, 2, 3, 4, 5) from Port 1,
find its factorial using look-up table and display the result on Port 2
continuously
ORG 0
MOV DPTR, #FACT ; Base address of look-up table
MOV P1, #0FFH ; Configure port 1 as input
AGAIN:
MOV A, P1 ; Read value of n from Port 1
MOVC A, @A+DPTR ; Take Factorial of number
MOV P2, A ; Display result on Port 2
SJMP AGAIN

ORG 250H ; Look-up Table


FACT: DB 1, 1, 2, 6, 24, 120
END
LOOK-UP TABLE - EXAMPLE
Write a program that searches a letter ‘g’ in a string [“hello gee”,0]. String
is NULL terminated. If g is in the string, program should exit with “g” in
accu A otherwise zero in Accu A. Copy contents of A to R1

org 00
mov dptr,#200h
cont: mov a,#00
movc a,@A+dptr
jz exit
inc dptr
cjne a,#‘g',cont
exit: mov r1,a
jmp $
org 200h
string: db "hello gee“,0

END
LOOK-UP TABLE - EXERCISE
Write a program to add the following numbers and send the result to P1
and P2. The data is stored at ROM addresses starting from 250H
(53, 49, 94, 56, 92, 65, 43, 83)

ORG 0 NEXT:
MOV DPTR, #mydata INC DPTR
MOV R2, #8 DJNZ R2, AGAIN
MOV P1, R3
AGAIN:
MOV P2, R7
CLR A
SJMP $
MOVC A, @A+DPTR
ADD A, R3
ORG 250H
MOV R3, A
MYDATA: DB 53,
JNC NEXT 49, 94, 56, 92, 65, 43, 83
INC R7 END

You might also like