0% found this document useful (0 votes)
7 views16 pages

Example 3-1: Looping in The 8051: Solution

The document provides a series of examples and solutions for programming loops and conditional jumps in the 8051 microcontroller. It includes programs for clearing registers, adding values, toggling bits, and analyzing stack contents. Additionally, it discusses the efficiency of using ACALL versus LCALL instructions in microcontroller programming.

Uploaded by

abishri.k2023
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)
7 views16 pages

Example 3-1: Looping in The 8051: Solution

The document provides a series of examples and solutions for programming loops and conditional jumps in the 8051 microcontroller. It includes programs for clearing registers, adding values, toggling bits, and analyzing stack contents. Additionally, it discusses the efficiency of using ACALL versus LCALL instructions in microcontroller programming.

Uploaded by

abishri.k2023
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/ 16

Example 3-1: Looping in the 8051

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=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
Example 3-2

What is the maximum number of times that the loop in Example 3-1 can be repeated?

Solution:

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.
Example 3-3: Loop Inside a Loop

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=10, the outer 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 loop is finished.
Table 3-1: 8051 Conditional Jump Instructions
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 ;jump if A is not zero
MOV R5,#55H
NEXT:...
Example 3-5

Find the sum of the values 79H, F5H, and E2H. Put the sum in registers 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_1: ADD A,#0F5H ;A=79+F5=6E and CY=1
JNC N_2 ;jump if CY=0
INC R5 ;If CY=1 then increment R5(R5=1)
N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1
JNC OVER ;jump if CY=0
INC R5 ;if CY=1, increment 5
OVER: MOV R0,A ;Now R0=50H, and R5=02
Example 3-6

Using the following list file, verify the jump forward address calculation.
Line PC Opcode Mnemonic Operand
01 0000 ORG 0000
02 0000 7800 MOV R0,#0
03 0002 7455 MOV A,#55H
04 0004 6003 JZ NEXT
05 0006 08 INC R0
06 0007 04 AGAIN: INC A
07 0008 04 INC A
08 0009 2477 NEXT: ADD A,#77h
09 000B 5005 JNC OVER
10 000D E4 CLR A
11 000E F8 MOV R0,A
12 000F F9 MOV R1,A
13 0010 FA MOV R2,A
14 0011 FB MOV R3,A
15 0012 2B OVER: ADD A,R3
16 0013 50F2 JNC AGAIN
17 0015 80FE HERE: SJMP HERE
18 0017 END
Example 3-6

Solution:

First notice that the JZ and JNC instructions both jump forward. The target
address for a forward jump is calculated by adding the PC of the following
instruction to the second byte of the short jump instruction, which is called the
relative address. In line 4 the instruction “JZ NEXT” has opcode of 60 and
operand of 03 at the addresses of 0004 and 0005. The 03 is the relative address,
relative to the address of the next instruction INC R0, which is 0006. By adding
0006 to 3, the target address of the label NEXT, which is 0009, is generated. In
the same way for line 9, the “JNC OVER” instruction has opcode and operand of
50 and 05 where 50 is the opcode and 05 the relative address. Therefore, 05 is
added to 000D, the address of instruction “CLR A”, giving 12H, the address of
label OVER.
Example 3-7

Verify the calculation of backward jumps in Example 3-6.

Solution:

In that program list, “JNC AGAIN” has opcode 50 and relative address F2H.
When the relative address of F2H is added to 15H, the address of the
instruction below the jump, we have 15H + F2H = 07 (the carry is dropped).
Notice that 07 is the address of label AGAIN. Look also at “SJMP HERE”, which
has 80 and FE for the opcode and relative address, respectively. The PC of the
following instruction, 0017H, is added to FEH, the relative address, to get
0015H, address of the HERE label (17H + FEH = 15H). Notice that FEH is -2 and
17H + (-2) = 15H. For further discussion of the addition of negative numbers, see
Chapter 6.
Example 3-8

Write a program to toggle all the bits of port 1 by sending to it the values 55H and AAH continuously.
Put a time delay in between each issuing of data to port 1. This program will be used to test the ports
of the 8051 in the next chapter.
Solution:
ORG 0
BACK: MOV A,#55H ;load A with 55H
MOV P1,A ;send 55H to port 1
LCALL DELAY ;time delay
MOV A,#0AAH ;load A with AA (in hex)
MOV P1,A ;send AAH to port 1
LCALL DELAY
SJMP BACK ;keep doing this indefinitely
;—————— this is the delay subroutine
ORG 300H ;put time delay at address 300H
DELAY: MOV R5,#0FFH ;R5 = 255(FF in hex),the counter
AGAIN: DJNZ R5,AGAIN ;stay here until R5 becomes 0
RET ;return to caller (when R5 = 0)
END ;end of asm file
Example 3-9

Analyze the stack contents after the execution of the first LCALL in the following.
Solution:
001 0000 ORG 0
002 0000 7455 BACK: MOV A,#55H ;load A with 55H
003 0002 F590 MOV P1,A ;send 55H to port 1
004 0004 120300 LCALL DELAY ;time delay
005 0007 74AA MOV A,#0AAH ;load A with AAH
006 0009 F590 MOV P1,A ;send AAH to port 1
007 000B 120300 LCALL DELAY
008 000E 80F0 SJMP BACK ;keep doing this
009 0010
010 0010 ;————————this is the delay subroutine
011 0300 ORG 300H
012 0300 DELAY:
013 0300 7DFF MOV R5,#0FFH ;R5=255
014 0302 DDFE AGAIN: DJNZ R5,AGAIN ;stay here
015 0304 22 RET ;return to caller
016 0305 END ;end of asm file
Example 3-10

Analyze the stack for the first LCALL instruction in the following program.
01 0000 ORG 0
02 0000 7455 BACK: MOV A,#55H ;load A with 55H
03 0002 F590 MOV P1,A ;send 55H to port 1
04 0004 7C99 MOV R4,#99H
05 0006 7D67 MOV R5,#67H
06 0008 120300 LCALL DELAY ;time delay
07 000B 74AA MOV A,#0AAH ;load A with AA
08 000D F590 MOV P1,A ;send AAH to port 1
09 000F 120300 LCALL DELAY
10 0012 80EC SJMP BACK ;keep doing this
11 0014 ;————————this is the delay subroutine
12 0300 ORG 300H
13 0300 C004 DELAY: PUSH 4 ;PUSH R4
14 0302 C005 PUSH 5 ;PUSH R5
15 0304 7CFF MOV R4,#0FFH ;R4=FFH
16 0306 7DFF NEXT: MOV R5,#0FFH ;R5=255
17 0308 DDFE AGAIN: DJNZ R5,AGAIN
18 030A DCFA DJNZ R4,NEXT
19 030C D005 POP 5 ;POP into R5
20 030E D004 POP 4 ;POP into R4
21 0310 22 RET ;return to caller
22 0311 END ;end of asm file
Example 3-10

Solution:
First notice that for the PUSH and POP instructions we must specify the direct
address of the register being pushed or popped. Here is the stack frame.
;MAIN program calling subroutines
ORG 0
MAIN: LCALL SUBR_1
LCALL SUBR_2
LCALL SUBR_3

HERE: SJMP HERE


;————————end of MAIN
;
Figure 3-1. 8051 SUBR_1: ....
....
Assembly Main RET
Program That Calls ;————————end of subroutine 1
;
Subroutines SUBR_2: ....
....
RET
;————————end of subroutine 2

SUBR_3: ....
....
RET
;————————end of subroutine 3
END ;end of the asm file
Example 3-11

A developer is using the Atmel AT89C1051 microcontroller chip for a product. This
chip has only 1K byte of on-chip flash ROM. Which instruction, LCALL or ACALL, is
more useful in programming this chip?

Solution:

The ACALL instruction is more useful since it is a 2-byte instruction. It saves one
byte each time the call instruction is used.
Example 3-12

Rewrite Example 3-8 as efficiently as you can.


Solution:
ORG 0
MOV A,#55H ;load A with 55H
BACK: MOV P1,A ;issue value in reg A to port 1
ACALL DELAY ;time delay
CPL A ;complement reg A
SJMP BACK ;keep doing this indefinitely

;————————this is the delay subroutine


DELAY:
MOV R5,#0FFH ;R5=255(FF in hex), the counter
AGAIN: DJNZ R5,AGAIN ;stay here until R5 becomes 0
RET ;return to caller
END ;end of asm file

You might also like