week3 - 1 Week 3 Jump, Loop, and Call Instructions Chapter...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Week 3 Jump, Loop, and Call Instructions Chapter 3 2 Looping in the 8051 ¡ Repeating a sequence of instructions a certain number of times is called a loop . ¢ Activity works several times. ¢ It needs some control transfer instructions. ¢ 8051 jump instructions • do activity first if exists • test the condition later ¢ DJNZ, JZ, JNC Initialization Activity Action & test condition Jump condition is true Not Jump condition is false Label 3 DJNZ - more ¡ Decrement and jump if not zero DJNZ Rn, target MOV R2,#02H CLR A HERE: INC A DJNZ R2,HERE ¢ Rn is one of the registers R0 - R7. ¢ Target is a label. A label HERE is the ROM address of the instruction INC A. ¢ 2 times in the loop for R2=2,1,0 ⇒ A=2 ¢ 2 times INC, DJNZ; 1 time jump MOV R2,#02H CLR A INC A DEC R2, Test Jump if R2 ≠ Not Jump if R2 = 0 HERE 4 DJNZ - more ¡ Direct access mode DJNZ direct, target MOV 30H,#02 ;X=the value in RAM 30H CLR A ;A=0 HERE:INC A ;increase A by 1 DJNZ 30H,HERE ;Decrement X and ;Jump to HERE if X ≠ ¢ Direct means “directly access the RAM with address”. ¢ See Appendix A-1 page 534 & 5 Example 3-1 Write a program to (a) clear ACC, then (b) add 3 to the accumulator ten times. (c) save the result in R5 Solution: ;This program adds value 3 to the ACC ten times CLR A ;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 The loop can be executed at most 256 times. HOW? Answer: Set the initial counter value to 0, with the first DJNZ it will become 255 and the loop will be repeated 255 more times. 6 Nested Loops ¡ A single loop is executed 256 times in maximum. ¡ If we want to repeat an action more times than 256, we use a loop inside a loop. ¡ This is called a nested loop. ¡ For Example: ¢ The inner loop is 256 ¢ The outer loop is 2 ¢ Total 256*2=512 inner loop outer loop 7 Example 3-3 Write a program to (a) load the accumulator with the value 55H, and (b) complement the ACC 700 times. Solution: The following code shows how to use R2 and R3 for the count. 700 = 10 × 70 Inner loop: R2=70 Outer loop: R3=10 DJNZ R2 AGAIN DJNZ R3 NEXT AGAIN NEXT MOV R2,#70 MOV R3,#10 8 Example 3-3 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 70 times(inner loop) DJNZ R3,NEXT-------------------------------------------------------------------- MOV A,#55H ;1 time MOV R3,#10 ;1 time NEXT: MOV R2,#70 ;10 times AGAIN:CPL A ;10x70 times DJNZ R2,AGAIN; 10x70 times DJNZ R3,NEXT ; 10 times i n n e r l o o p o u t e r l o o p 9 8051 Conditional Jump Instructions 10 JZ ¡ Jump if A = zero JZ target MOV A,R5 JZ NEXT MOV R5,#55H NEXT: ......
View Full Document

This note was uploaded on 01/09/2012 for the course CS cs464 at Bilkent University.

Page1 / 49

week3 - 1 Week 3 Jump, Loop, and Call Instructions Chapter...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online