8051
REGISTER
BANKS AND
STACK
Stack And Bank
1 Conflict
(cont’)
Example 2-10
Examining the stack, show the contents of the register and SP after
execution of the following instructions. All value are in hex.
MOV SP, #5FH ;make RAM location 60H
;first stack location
MOV R2, #25H
MOV R1, #12H
MOV R4, #0F3H
PUSH 2
PUSH 1
PUSH 4
Solution:
25
12
F3
After PUSH 4
SP = 62
60
61
62
63
Start SP = 5F SP = 60 SP = 61
60 60 25 60 25
61 61 61 12
62 62 62
63 63 63
After PUSH 2 After PUSH 1
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
Chung-Ping Young
楊中平
JUMP, LOOP AND CALL
INSTRUCTIONS
The 8051 Microcontroller and Embedded
Systems: Using Assembly and C
Mazidi, Mazidi and McKinlay

LOOP AND
JUMP
INSTRUCTIONS
Looping
􀂉 Repeating a sequence of instructions a
certain number of times is called a
loop
􀂾 Loop action is performed by
DJNZ reg, Label
􀂃 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 loop the register is loaded
with the counter for the number of repetitions
􀂃 Counter can be R0 – R7 or RAM location
;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
A loop can be repeated a
maximum of 255 times, if
R2 is FFH

LOOP AND
JUMP
INSTRUCTIONS
Nested Loop
􀂉 If we want to repeat an action more
times than 256, we use a loop inside a
loop, which is called nested loop
􀂾 We use multiple registers to hold the
count
Write a program to (a) load the accumulator with the value 55H, and
(b) complement the ACC 700 times
MOV A,#55H ;A=55H
MOV R3,#10 ;R3=10, outer loop count
NEXT: MOV R2,#70 ;R2=70, inner loop count
AGAIN: CPL A ;complement A register
DJNZ R2,AGAIN ;repeat it 70 times
DJNZ R3,NEXT

Post a Comment

0 Comments

Close Menu