1 Chapter 3 Jump, Loop, and Call Instructions. 2 Sections 3.1 Loop and Jump Instructions 3.2 Call Instructions 3.3 Time Delay Generation and Calculation.

Slides:



Advertisements
Similar presentations
Week4. Program Branching  From what we have covered so far, our assembly programs execute one instruction after another in sequence  Programs that solve.
Advertisements

Week 8 Stack and Subroutines. Stack  The stack is a section of data memory (or RAM) that is reserved for storage of temporary data  The data may represent.
Chapter 4 I/O Port Programming
The 8051 Microcontroller and Embedded Systems
Chapter Addressing Modes
The 8051 Microcontroller and Embedded Systems
Chapter Programming in C
1 Chapter 3 Jump, Loop, and Call Instructions. 2 Sections 3.1 Loop and Jump Instructions 3.2 Call Instructions 3.3 Time Delay Generation and Calculation.
Msc. Ivan A. Escobar Broitman Microprocessors 1 1 The 8051 Instruction Set.
TK 2633 Microprocessor & Interfacing
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
Chapter 7 Logic Instructions and Programs
8051 ASSEMBLY LANGUAGE PROGRAMMING
Microcontroller Intel 8051
The 8051 Microcontroller architecture
MICROCONTROLLER INSTRUCTION SET
CoE3DJ4 Digital Systems Design Chapter 3: instruction set summary.
Prof. Cherrice TraverEE/CS-152: Microprocessors and Microcontrollers The 8051 Assembly Language.
MICROCONTROLLERS 8051.
University Of Engineering And Technology Taxila REF::NATIONAL TAIWANOCEAN UNIVERSITY 國立台灣海洋大學 Chapter 3 JUMP, LOOP and CALL Instructions.
The 8051 Assembly Language Branching & Subroutines
The x86 PC Assembly Language, Design, and Interfacing By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey © 2010, 2003, 2000, 1998 Pearson.
The 8051 Microcontroller and Embedded Systems
Today’s Lecture Unconditional branch instruction
Lecture Set 4 Programming the 8051.
The 8051 Assembly Language. Overview Data transfer instructions Addressing modes Data processing (arithmetic and logic) Program flow instructions.
MICRO CONTROLLER PROGRAMMING & APPLICATIONS UNIT V Mr. S. VINOD LECTURER EEE DEPARTMENT.
Assembly Language Programming of 8085 BY Prof. U. V. THETE Dept. of Computer Science YMA.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
JUMP, LOOP, AND CALL INSTRUCTIONS
1 EKT 225 MICROCONTROLLER I CHAPTER ASSEMBLY LANGUAGE PROGRAMMING.
The 8051 Assembly Language. Overview Introduction Addressing modes Data processing (arithmetic and logic) Data transfer instructions Program flow instructions.
8051 Micro Controller. Microcontroller versus general-purpose microprocessor.
HJD Institute of Technical Education & Research- Kera(Kutch) The 8051 Microcontroller architecture PREPARED BY: RAYMA SOHIL( )
Microprocessors I 8051 Addressing Modes CS Prof. Msc. Ivan A. Escobar
Unit 1 Instruction set M.Brindha AP/EIE
Assembly Language Programming of 8085
Microprocessor T. Y. B. Sc..
3.Instruction Set of 8085 Consists of 74 operation codes, e.g. MOV
Introduction to Micro Controllers & Embedded System Design Assembly Language Programming Department of Electrical & Computer Engineering Missouri University.
The 8051 Microcontroller and Embedded Systems
Lecture Set 5 The 8051 Instruction Set.
Subroutines and the Stack
Microprocessor and Assembly Language
TAO1221 COMPUTER ARCHITECTURE AND ORGANIZATION LAB 3 & 4 Part 2
ECE,JYOTHI ENGG COLLEGE
Data Processing Instructions
The 8051 Microcontroller.
EMT 348: Microcontroller Timer/counter
SCHOOL OF ELECTRONICS ENGINEERING Electronics and Communication
8051 Single Board Computer (SBC) Version 1.0
Subroutine Call; Stack
Timer.
(Electrical Engg 6th Semester)
Branching Instructions
Introduction to Micro Controllers & Embedded System Design Instruction set Department of Electrical & Computer Engineering Missouri University of Science.
Introduction to Micro Controllers & Embedded System Design Addressing Mode Department of Electrical & Computer Engineering Missouri University of Science.
Subroutines and the Stack
Microcontroller 8051 Made By: Arun Branch. 4th Sem. I&C Engg.
Conditional Jumps and Time Delays
Introduction to Micro Controllers & Embedded System Design Instruction set Department of Electrical & Computer Engineering Missouri University of Science.
Conditional Jumps and Time Delays
DMT 245 Introduction to Microcontroller
Introduction to Micro Controllers & Embedded System Design
First Design Key board R L S.
Conditional Jumps and Time Delays
8051 ASSEMBLY LANGUAGE PROGRAMMING
Subroutines and the Stack
Addressing Modes in 8051 MC S. Lourduraj Asst. Prof. of Physics
Presentation transcript:

1 Chapter 3 Jump, Loop, and Call Instructions

2 Sections 3.1 Loop and Jump Instructions 3.2 Call Instructions 3.3 Time Delay Generation and Calculation

3 Objective 我們寫一個簡單的 8051 程式,常常會做一些 Control transfer 的動作。所以我們要瞭解: 什麼是 Control transfer : conditional jump, unconditional jump, call subroutine. 怎麼用 8051 的 Jump 指令寫一個簡單的程式。 怎麼用 8051 的 Call 指令寫一個簡單的副程式。 8051 是如何執行 Jump 指令的。 8051 是如何執行 Call 指令的。 如何計算每一個指令所需要的執行時間。 如何計算每一個 Loop 所需要的執行時間。 如何計算每一個程式所需要的執行時間。

4 Section 3.1 Loop and Jump Instructions

5 Looping in the 8051 Repeating a sequence of instructions a certain number of times is called a loop. –Activity works several times. –It need some control transfer instructions. –8051 jump instructions Do Action First Test the condition later –DJNZ, JZ, JNC –Example 3-1 ~ 3-5 Initialization Activity Action & test condition Jump condition is true Not Jump condition is false Label

6 DJNZ ( 1/2 ) Decrement and jump if not zero DJNZ Rn, target MOV R2,#02H CLR A HERE: INC A DJNZ R2,HERE –Rn is one register of R0 - R7. –Target is a label. A label ( HERE ) is the ROM address of the following 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≠0 Not Jump if R2 = 0 HERE

7 DJNZ ( 2/2 ) 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≠0 –Direct means “directly access the RAM with address”. –See Appendix A-1 ( page 336 )

8 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=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

9 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,, the loop can be repeated a maximum of 256 times by setting R2=0. Thus, R2 = 0H, FFH, FEH,..., 2, 1, 0 ( total 256 times of ADD & DJNZ ). See Example3-2.a51.

10 Nested Loop A single loop is repeated 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 nested loop. For Example: –The inner loop is 256 –The outer loop is 2 –Total 256*2=512 inner loop outer loop

11 Example 3-3 (1/2) 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

12 Example 3-3 (2/2) 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 DJNZ R2 AGAIN DJNZ R3 NEXT AGAIN NEXT MOV R2,#70

13 JZ Jump if A = zero JZ target MOV A,R5 JZ NEXT MOV R5,#55H NEXT:... –This instruction examines the contents of the ACC and jumps if ACC has value 0. MOV R5,#55H Test Jump if A = 0 Not Jump if A ≠0 NEXT MOV A, R5

14 JNZ Jump if A is not zero JNZ target MOV A,R5 JNZ NEXT MOV R5,#55H NEXT:... –This instruction examines the contents of the ACC and jumps if ACC is not 0. MOV R5,#55H Test Jump if A≠0 Not Jump if A =0 NEXT MOV A, R5

15 Example 3-4 Write a program to determine if R5 contains the value 0. If so, put 55H in it. Solution: MOV A,R5 JNZ NEXT MOV R5,#55H NEXT:... MOV R5,#55H Test Jump if A = 0 Not Jump if A ≠0 NEXT MOV A, R5

16 JNC Jump if no carry ( CY=0 ) JNC target MOV A,#FFH ADD A,#01H JNC HEXT INC R5 NEXT:... –CY is PWS. –This instruction examines the CY flag, and if it is zero it will jump to the target address. INC R5 Test Jump if CY=0 Not Jump if CY ≠0 NEXT ADD A, #01H

17 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(R5=0) ADD A,#79H ;A=0+79H=79H JNC N_1 ;if CY=0,add next number INC R5 ;if CY=1, increment R5 N_1: ADD A,#0F5H ;A=79+F5=6E and CY=1(R5=0) JNC N_2 ;jump if CY=1 INC R5 ;if CY=1, increment R5 N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1(R5=1) JNC OVER ;jump if CY=0 INC R5 ;CY=1, increment 5 OVER:MOV R0,A ;Now R0=A=50H,and R5=02 R5 R0 A CY

18 Unconditional Jump Instructions The conditional jump is a jump in which control is transferred to the target location if it meets the required condition. –DJNZ, JZ, JNC... The unconditional jump is a jump in which control is transferred unconditionally to the target location. There are two unconditional jumps : –LJMP ( Long Jump ) –SJMP ( Short Jump ) –Example 3-6 ~ 3-7

19 Long Jump ( LJMP ) a 3-byte instruction –The first byte is the opcode –The next two bytes are the target address (real address) LJMP is used to jump to any address location within the 64K byte code space of the Bits opcode = 02 target address

20 LJMP Jump to a new address LJMP 16-bit-target-addr. Line PC Opcode Mnemonic Operand HERE: LJMP HERE 18 OO18 END –The opcode of LJMP is 02. –When executing Line 17, jump to the target address 0015H. –The 8051 Assembler can transfer the label “HERE” to the value 0015H.

21 Short Jump ( SJMP ) a 2-byte instruction –The first byte is the opcode –The second byte is the signed number displacement, which is added to the PC to get the target address. –The target address must be within -128 to +127 bytes of the PC from the program counter. –The address is referred to as a relative address since the target address is -128 to 127 bytes relative to the PC. Bits opcode = 80 relative address

22 SJMP Jump to a new address SJMP 8-bit-relative-address Line PC Opcode Mnemonic Operand FE HERE: SJMP HERE 18 OO17 END –Then opcode of “SJMP” is 80. –The target label HERE has the value 0015H. –PC=0017H. –The relative address is 0015H-0017H=FEH The target address is PC+the relative address=0017H+FEH (The CARRY is dropped of the low byte)

23 Example 3-6 (1) Using the following list file, verify the jump forward address calculation. Line PC Opcode Mnemonic Operand ORG OOOO MOV R0,# MOV A,#55H JZ NEXT INC R AGAIN: INC A INC A NEXT: ADD A,#77H B 5005 JNC OVER D E4 CLR A E F8 MOV R0,A 12 OOOF F9 MOV R1,A 13 OO1O FA MOV R2,A 14 OO11 FB MOV R3,A 15 OO12 2B OVER: ADD A,R3 16 OO13 50F2 JNC AGAIN FE HERE: SJMP HERE 18 OO17 END

24 Example 3-6 (2) Solution: The target address > PC  jump forward JZ NEXT Opcode=60; the target address= NEXT =0009H; PC=0006H The relative address = the target address - PC = 0009 - 0006 = 0003 JNC OVER Opcode=05; the target address= OVER =0012H; PC=000DH The relative address = the target address - PC = 0012 - 000D = 0005

25 Example 3-7 Verify the calculation of backward jumps in Example 3-6. Solution: The target address < PC  jump backward JNC AGAIN Opcode=50; the target address= AGAIN =0007H; PC=0015H The relative address = the target address - PC = 0007 - 0015 = -14=F2H The target address = 0015H + F2H = 0007H SJMP HERE Opcode=80; the target address= HERE =0015H; PC=0017H The relative address = the target address - PC = 0015 - 0017 = -2=FEH The target address=PC+ FEH=0017H+FEH=0015H

26 Other Conditional Jumps The usage of these instructions –See Appendix A.1, Table A-1 ( page 356 ), Tables 10 & 11 in Appendix H ( page 418 & 422 ) All conditional jumps are short jumps. –They have a 1-byte relative address. –The 8051 Assembler changes the target label into the relative offset to PC and save the offset in the instructions. –The target address cannot be more than -128 to +127 bytes away from the program counter.

27 InstructionAction JZ ( Jump Zero ) Jump if A=0 JNZ ( Jump no zero ) Jump if A≠0 DJNZ Rn,targetDecrement and jump if byte≠0 CJNE A,byte Compare A with byte and jump if not equal (A≠byte) CJNE reg,#data Compare reg. with #data and jump if not equal (byte ≠ #data) JC ( Jump carry ) Jump if CY=1 JNC ( Jump no carry ) Jump if CY=0 JB ( Jump bit ) Jump if bit=1 JNB ( Jump no bit ) Jump if bit=0 JBC ( jump bi clear bit ) Jump if bit=1 and clear bit Table 3-1: 8051 Conditional Jump Instructions

28 Other Unconditional Jump –discuss later Absolute jump ( AJMP ) AJMP 11-bit-target-address –The target address must be within 2K bytes of program memory. Bits target opcode target address

29 Section 3.2 Call Instructions

30 CALL Another control transfer instruction is the CALL instruction, which is used to call a subroutine. Subroutines are often used to perform tasks that need to be performed frequently. This make a program more structured in addition to saving memory space.   In the 8051 there are two instructions for call : –LCALL ( long call )( Examples 3-8 ~ 3-10 ) –ACALL ( absolute call )( Examples 3-11 ~ 3-12 )

31 The Flow of Control Involving a Procedure 

32 Figure Assembly Main Program That Calls Subroutines ;MAIN program calling subroutines ORG 0 MAIN: LCALL SUBR_1 LCALL SUBR_2 LCALL SUBR_3 HERE: SJMP HERE ; ---- end of MAIN SUBR_1: RET ; ---- end of subroutine 1 SUBR_2: RET ; ---- end of subroutine 2 SUBR_3: RET ; end of subroutine 3 END ;end of the asm file

33 Long Call ( LCALL ) a 3-byte instruction –The first byte is the opcode –The next two bytes are the target address LCALL is used to jump to any address location within the 64K byte code space of the Bits opcode = 02 target address

34 LCALL Jump to a new address LCALL 16-bit-target-addr. Line PC Opcode Mnemonic Operand LCALL DELAY 05 OO07 74AA MOV A,#0AAH ORG 300H DFF DELAY: MOV R5,#0FFH RET –The opcode of LCALL is 12. –The target address is –The return address is 0007 subroutine DELAY return addresstarget address

35 LCALL and Memory Addresses LCALL DELAY; MOV A#0AAH ; DELAY RAM addr MOV R5,#0FFH; RET; 0000 RAM addr. return address

36 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,#OFFH ;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

37 Example 3-9 (1/2) Analyze the stack contents after the execution of the first LCALL in the following. Solution: (a) ORG BACK: MOV A,#55H ;load A with 55H F590 MOV P1,A ;send 55H to port LCALL DELAY ;time delay AA MOV A,#0AAH ;load A with AAH F590 MOV P1,A ;send AAH to port B LCALL DELAY E 80F0 SJMP BACK ;keep doing this ; --- this is the delay subroutine ORG 300H 012 O300 DELAY: 013 O300 7DFF MOV R5,#OFFH ;R5= O302 DDFE AGAIN: DJNZ R5,AGAIN ;stay here 015 O RET ;return to caller 016 O305 END ;end of asm file

38 SP = A Example 3-9 (2/2) Solution: (b) When the first LCALL is executed, the address of the instruction “ MOV A,#0AAH ” is saved on the stack. Notice that the low byte goes first and the high byte is last. The last Instruction of the called subroutine must be a RET instruction which directs the CPU to POP the top bytes of the stack into the PC and resume executing at address 07. The diagram shows the stack frame after the first LCALL.

39 The Process of Calling a Subroutine After execution of the called subroutine, the 8051 must know where to com back to. The process of calling a subroutine : –A subroutine is called by CALL instructions. –The 8051 pushes the PC onto the stack. –The 8051 copies the target address to the PC. –The 8051 fetches instructions from the new location. –When the instruction RET is fetched, the subroutine ends. –The 8051 pops the return address from the stack. –The 8051 copies the return address to the PC. –The 8051 fetches instructions from the new location.

40 Example 3-10 (1/3) Analyze the stack for the first LCALL instruction in the following program ORG BACK: MOV A,#55H ;load A with 55H F590 MOV P1,A ;send 55H to port C99 MOV R4,#99H D67 MOV R5,#67H LCALL DELAY ;time delay B 74AA MOV A,#0AAH ;Load A with AA D F590 MOV P1,A ;send AAH to port F LCALL DELAY EC SJMP BACK ;keep doing this ; this is the delay subroutine ORG 300H 13 O300 CO04 DELAY:PUSH 4 ;PUSH R4 14 O302 C005 PUSH 5 ;PUSH R5 15 O304 7CFF MOV R4,#0FFH;R4=FFH 16 O306 7DFF NEXT: MOV R5,#0FFH;R5= O308 DDFE AGAIN:DJNZ R5,AGAIN A DCFA DJNZ R4,NEXT C D005 POP 5 ;POP INTO R E D004 POP 4 ;POP INTO R RET ;return to caller END ;end of asm file

41 Example 3-10 (2/3) Solution: The stack keeps track of where the CPU should return after completing the subroutine.For this reason, the number of PUSH and POP instructions must always match in any called subroutine. PCL 0B08 PCH A 0B PCL 0B08 PCH 0009 R4 990A 0B PCL 0B08 PCH 0009 R4 990A R5 670B After the first LCALL After PUSH 4After PUSH 5 SP=0ASP=0BSP=09

42 Example 3-10 (3/3) Solution: Also 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. PCL 0B08 PCH A 0B PCL 0B08 PCH 0009 R4 990A 0B A 0B After the POP 5 After POP 4 After RET SP=09SP=07SP=0A

43 Absolute Call ( ACALL ) a 2-byte instruction –The first 5 bits is the opcode –The last 11 bits is the target address. –The target address must be within 2K bytes of program memory. –Using ACALL can save memory space than using LCALL. Bits target opcode target address 10001

44 ACALL Jump to a new address ACALL 11-bit-target-address Line PC Opcode Mnemonic Operand ACALL DELAY AA MOV A,#0AAH ORG 300H DFF DELAY: MOV R5,#0FFH RET –The opcode of ACALL is 10001B (5 bits). –The target address is 0300H= B(11 bits). –The return address is B=7100H

45 Example 3-11 A developer is using the Atmel AT89C1051 microcontroller chip for a product. This chip has only 1K bytes of on-chip flash ROM. Which of the instructions LCALL and ACALL is most 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.

46 Example 3-12 Rewrite Example 3-8 as efficiently as you can. Solution: ORG 0 MOV A,#55H ;A= B=55H BACK: MOV P1,A ;put reg A to port 1 ACALL DELAY ;time delay CPL A ;A= B=0AAH SJMP BACK ;indefinitely loop ; --- this is the delay subroutine DELAY: MOV R5,#OFFH ;R5=255, the counter AGAIN: DJNZ R5,AGAIN ;jump if R5 becomes 0 RET ;return to caller END ;end of asm file

47 Section 3.3 Time Delay Generation and Calculation

48 Time Delay We have written a delay subroutine in Ex3-8. How to calculate exact delays ? How to generate various time delay ?

49 Machine Cycle ( 1/2 ) For the CPU to execute an instruction takes a certain number of clock cycles. In the 8051 family, these clock cycles are referred to as machine cycles. –Ex : RET needs 2 machine cycles The 8051 has an on-chip oscillator which generates machine cycles. The 8051 requires an external clock ( a quartz crystal oscillator ) to run the on-chip oscillator.  

50 Machine Cycle ( 2/2 ) The relationship between two oscillators : –The length of the machine cycle is 12 of the oscillator period. –The frequency of the machine cycle is 1/12 of the crystal frequency. The frequency of the external crystal can be vary from 4 MHz to 30MHz. Very often the MHz crystal oscillator is used to make the 8051-based system compatible with the serial port of the IBM PC ( See Chapter 10 ).

51 The 8051 Oscillators C2 30pF C1 30pF XTAL2 XTAL1 GND internal oscillator The period of MC = 12 × oscillator period The frequency of MC = 1/12 × external oscillator external oscillator machine cycle external clock machine cycle 

52 Example 3-13 The following shows crystal frequency for three different based systems. Find the period of the machine cycle in each case. (a) MHz (b) 16 MHz (C) 20 MHz Solution: (a) /12 = KHz machine cycle is 1/921.6 KHz =  s (microsecond) (b) oscillator period = 1/16 MHz =  s machine cycle (MC) =  s ×12 = 0.75  s (c) 20 MHz/12 = 1.66 MHz MC = 1/1.66 MHz = 0.60  s

53 Example 3-14 For an 8051 system of MHz, find how long it takes to execute each of the following instructions. (a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2, target (d) LJMP (e) SJMP (f) NOP (g) MUL AB Solution: The machine cycle for a system of MHz is  s. Table A-1 shows machine cycles for each instructions. Instruct Machine cycles Time to execute (a) MOV R3,#55 1 1×1.085  s =  s (b) DEC R3 1 1×1.085  s =  s (c) DJNZ R2,target 2 2×1.085  s = 2.17  s (d) LJMP 2 2×1.085  s = 2.17  s (e) SJMP 2 2×1.085  s = 2.17  s (f) NOP 1 1×1.085  s =  s (g) MUL AB 4 4×1.085  s = 4.34  s

54 Example 3-15 Find the size of the delay in the following program, if the crystal frequency is MHz. MOV A,#55H AGAIN: MOV P1,A ACALL DELAY CPL A SJMP AGAIN ; --- Time delay Machine Cycle DELAY: MOV R3,#200 1 HERE: DJNZ R3,HERE 2 RET 2 Solution: Table A-1 The time delay is [(200 × 2)+1+2] ×  s =  s.

55 Delay Calculation A delay subroutine consists of two parts –setting a counter ( initialization ) –a loop Very often we calculate the time delay based on the instructions inside the loop and ignore the clock cycles associated with the instructions outside the loop. Two way to get a large delay is –to use NOP ( Example 3-16 ) –to use a loop inside a loop ( nested loop )( Example 3-17 )

56 Example 3-16 Find the time delay for the following subroutine, assuming a crystal frequency of MHz. Machine Cycle DELAY: MOV R3,#250 1 HERE: NOP 1 NOP 1 DJNZ R3,HERE 2 RET 2 Solution: the HERE loop & the two instructions outside the loop { [250 ( )] + 3 } ×  s = (1500+2) ×  s =  s.

57 Example 3-17 For a machine cycle of  s, find the time delay in the following subroutine. DELAY: Machine Cycle MOV R2,#200 1 AGAIN: MOV R3,#250 1 HERE: NOP 1 NOP 1 DJNZ R3,HERE 2 DJNZ R2,AGAIN 2 RET 2 Solution: the HERE loop = 250 (1+1+2)=1000 MCs the AGAIN loop = 200( ) = MCs the whole program = = MCs = ×  s =  s

58 You are able to (1/2) Code 8051 Assembly language instructions using loops Code 8051 Assembly language conditional jump instructions Explain conditions that determine each conditional jump instruction Code long jump instructions for unconditional jumps Code short jump instructions for unconditional short jumps

59 You are able to (2/2) Calculate target addresses for jump instructions Code 8051 subroutines Describe precautions in using the stack in subroutines Discuss crystal frequency versus machine cycle Code 8051 programs to generate a time delay

60 Homework Chapter 3 Problems : 12,13,14,23,25,26,32,35 Note: –Please write and compile the program of Problems 12,13,26. –For the programming problem, please use "8 LED learning board" for the following question. –Please count the total delay for Problems 32 and 35.