1 / 72

Chapter 3 Jump, Loop, and Call Instructions

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

cormac
Télécharger la présentation

Chapter 3 Jump, Loop, and Call Instructions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Jump, Loop, and Call Instructions

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

  3. Application of Jump & Call • 電動門 • 判斷手動開關與感應器的ON/OFF以進行電動機的運作與否 • 紅綠燈 • 顯示文字維持一段時間 • 產生一段時間的延遲 • 判斷接下來的流程 • 電子琴 • DO的頻率262Hz  產生週期為3816ms的方波 • RE的頻率294Hz  產生週期為3401ms的方波 DO 262, RE 294, ME 330, FA 349, SO 392, LA 440, SI 494, DO 522

  4. Sections 3.1 Loop and jump instructions 3.2 Call instructions 3.3 Time delay for various 8051 chips

  5. Section 3.1Loop and Jump Instructions

  6. Conditional/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... • Example 3-1~3-5 • 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

  7. Initialization Label Activity Action&test condition Jump condition is true Not Jump condition is false 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 Activity First if exists • Test the condition later • DJNZ, JZ, JNC • Example 3-1~3-5

  8. MOV R2,#02H CLR A HERE INC A DEC R2, Test Jump if R2≠0 Not Jump if R2 = 0 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). • Activity: twice (for R2=2,1,0) A=2 • INC, DJNZ: twice; jump once

  9. DJNZ(2/2) • Direct addressing 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 534)

  10. 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

  11. 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).

  12. 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 times outer loop inner loop activity

  13. NEXT MOV R2,#70 AGAIN DJNZ R2 AGAIN DJNZ R3 NEXT 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 MOV R3,#10

  14. NEXT MOV R2,#70 AGAIN DJNZ R2 AGAIN DJNZ R3 NEXT 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;repeat70times(inner loop) DJNZ R3,NEXT MOV R3,#10

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

  16. MOV A, R5 Jump if A≠0 Test Not Jump if A =0 MOV R5,#55H NEXT 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.

  17. MOV A, R5 Jump if A≠0 Test Not Jump if A =0 MOV R5,#55H NEXT 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: ...

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

  19. 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=0 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 CY A R5 R0

  20. Table 3-1: 8051 Conditional Jump Instructions

  21. Long Jump(LJMP) ROM address 0000 • 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 8051. 1120 LJMP Target 1123 CPL A Target: MOV R0,A A010 FFFF Target=A010 opcode=02A010 Bits 23 16 15 8 7 0 opcode = 02 target address

  22. LJMP • Jump to a new address LJMP 16-bit-target-addr. Line Addr. Opcode Mnemonic Operand 17 0015 020015 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.

  23. ROM address 0000 Opcode 8030H 1120 SJMP Target 1122 CPL A Target: MOV R0,A 1152 FFFF Bits 15 8 7 0 opcode = 80 relative address Short Jump(SJMP) Forward jump • A 2-byte instruction • The first byte is the opcode. • The second byte is the relative address. • The address is referred to as arelative addresssince the target address is relative to the PC. • It is a signed number displacement Assembling: PC=1122, target=1152 relative = target-PC=1152-1122=30H Running: PC=1122, relative=30 target=PC+relative=1122+30=1152H

  24. SJMP • Jump to a new address SJMP 8-bit-relative-address Line Addr. Opcode Mnemonic Operand 17 0015 80FE HERE: SJMP HERE 18 OO17 END Assembling The target label HERE has the value 0015H. PC=0017H. Relative address = Target address-PC =0015H-0017H=FFFEH Running Target address = PC + Relative address =0017H+FFFEH (The CARRY is dropped) = 0015H Backward jump

  25. SJMP to Itself Using $ Sign • Sometimes, there is nothing to do but we want to keep the microcontroller busy. • You can use Line PC Opcode Mnemonic Operand 17 0015 80FE HERE: SJMP HERE 18 OO17 END • We can use the following: SJMP $

  26. Relative Address • The target address must be within -128 to +127 bytes of the PC from the program counter. • Forward jump: 0 ~ 127 (0 ~ 7FH) • Backward jump: -1 ~ -128 (FFH ~ 80H) • Real target address = PC + relative address • Ex1: PC=1001H, relative address=40H target address=1001H+40H=1041H • Ex2: PC=1001H, relative address=FFFEH (-210)  target address=1001H+FFFEH=0FFFH

  27. AJMP • Absolute jump(AJMP) AJMP 11-bit-target-address • The target address must be within 2K bytes of program memory (from 0000H to 07FFH). • The opcode of AJMP are 01H, 21H,…,E1H (page 616) Bits 15 13 12 8 7 0 target opcode target address 00001

  28. Other Conditional Jumps • The usage of these instructions • See Appendix A.1, Table A-1(page 523), Tables 10&11 in Appendix H(page 612&616) • 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.

  29. Example 3-6 (1) Using the following list file, verify the jump forward address calculation. Line PC Opcode Mnemonic Operand 01 0000 ORG OOOO 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 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 50F2JNC AGAIN 17 0015 80FE HERE: SJMP HERE 18 OO17 END

  30. Example 3-6 (2) Solution: The target address > PC  jump forward JZ NEXT (6003H) Opcode=60; the target address=NEXT=0009H; PC=0006H The relative address = the target address-PC=0009-0006=0003 The target address=0006H+0003H=0009H JNC OVER (5005H) Opcode=50; the target address=OVER=0012H; PC=000DH The relative address = the target address-PC=0012-000D=0005 The target address=000DH+0005H=0012H Assembling Running Assembling Running

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

  32. Section 3.2Call Instructions

  33. 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)

  34. Figure 3-1. 8051 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

  35. The Flow of Control Involving a Procedure

  36. 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 8051. Bits 23 16 15 8 7 0 opcode = 12 target address

  37. LCALL • Jump to a new address LCALL 16-bit-target-addr. Line Addr. Opcode Mnemonic Operand 04 0004 120300 LCALL DELAY 05 0007 74AA MOV A,#0AAH ... 11 0300 ORG 300H 12 0300 7DFF DELAY: MOV R5,#0FFH ... 15 0304 22 RET • The opcode of LCALL is 12. • The target address is 0300. • The return address is 0007 return address target address subroutine DELAY

  38. LCALL and Memory Addresses ROM addr. 0000 DELAY ROM addr. MOV R5,#0FFH; 0300 LCALLDELAY;MOV A,#0AAH; 0004 0007 0009 RET; return address 0304

  39. 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

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

  41. 0A 09 00 08 07 SP = 09 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.

  42. The Process of Calling a Subroutine • After execution of the called subroutine, the 8051 must know where to come 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.

  43. Example 3-10 (1/3) 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 port1 04 0004 7C99 MOV R4,#99H 05 0006 7D67 MOV R5,#67H 06 0008 120300LCALL 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 120300LCALL DELAY 10 0012 80EC SJMP BACK ;keep doing this 11 0014 ; this is the delay subroutine 12 0300 ORG 300H 13 O300 C004 DELAY:PUSH 4 ;PUSH RAM 04H 14 O302 C005 PUSH 5 ;PUSH RAM 05H 15 O304 7CFF MOV R4,#0FFH;R4=FFH 16 O306 7DFF NEXT: MOV R5,#0FFH;R5=255 17 O308 DDFE AGAIN:DJNZ R5,AGAIN 18 030A DCFA DJNZ R4,NEXT 19 030C D005 POP 5 ;POP INTO RAM 05H 20 030E D004 POP 4 ;POP INTO RAM 04H 21 0310 22 RET ;return to caller 22 0311 END ;end of asm file

  44. 0B 0B 0B 67 R5 0A 0A 99 R4 0A 99 R4 09 00 PCH 09 00 PCH 09 00 PCH 08 0B PCL 08 0B PCL 08 0B PCL 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. After the first LCALL After PUSH 4 After PUSH 5 SP=09 SP=0A SP=0B

  45. 0B 0B 0B 0A 0A 99 R4 0A 09 00 PCH 09 00 PCH 09 08 0B PCL 08 0B PCL 08 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. After RET After the POP 5 After POP 4 SP=0A SP=09 SP=07

  46. Absolute Call(ACALL) • a 2-byte instruction • The target address must be within 2K bytes of program memory. • Using ACALL can save memory space than using LCALL. • The opcode of ACALL are 11H, 31H,…,F1H (page 616) Bits 15 13 12 8 7 0 target opcode target address 10001

  47. ACALL • Jump to a new address ACALL 11-bit-target-address Line PC Opcode Mnemonic Operand 04 0004 7100 ACALL DELAY 05 0006 74 AA MOV A,#0AAH ... 11 0300 ORG 300H 12 0300 7DFF DELAY: MOV R5,#0FFH ... 15 0304 22 RET • The opcode of ACALL is 10001B (5 bits). • The target address is 0300H=0000 0011 0000 0000B(11 bits). • The machine code is 0111 0001 0000 0000 B=7100H

  48. 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.

  49. Example 3-12 Rewrite Example 3-8 as efficiently as you can. Solution: ORG 0 MOV A,#55H ;A=01010101B=55H BACK:MOV P1,A ;put reg A to port 1 ACALL DELAY ;time delay CPL A ;A=10101010B=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

  50. Section 3.3Time Delay Generation and Calculation

More Related