1 chapter 3 jump, loop, and call instructions. 2 sections 3.1 loop and jump instructions 3.2 call...

57
1 Chapter 3 Jump, Loop, and Call Instructions

Upload: jagger-overy

Post on 14-Dec-2015

261 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: 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

1

Chapter 3 Jump, Loop, and Call Instructions

Page 2: 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

2

Sections

3.1 Loop and Jump Instructions

3.2 Call Instructions

3.3 Time Delay Generation and Calculation

Page 3: 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

Section 3.1Loop and Jump Instructions

Page 4: 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

4

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 conditionJump

condition is true Not Jump

condition is false

Label

Page 5: 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

5

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, TestJump if

R2≠0Not Jump if R2 = 0

HERE

Page 6: 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

6

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 )

Page 7: 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

7

Example 3-1Write a program to (a) clear ACC, then(b) add 3 to the accumulator ten times.

Solution:;This program adds value 3 to the ACC tentimes

MOV A,#0 ;A=0, clear ACC MOV R2,#10 ;load counter R2=10AGAIN: ADD A,#03 ;add 03 to ACC DJNZ R2,AGAIN ;repeat until R2=0(10 times) MOV R5,A ;save A in R5

Page 8: 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

8

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.

Page 9: 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

9

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=512inner loop

outer loop

Page 10: 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

10

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

Page 11: 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

11

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

Page 12: 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

12

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

Page 13: 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

13

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,#55HNEXT: ...

MOV R5,#55H

Test

Jump if A= 0

Not Jump if A ≠0

NEXT

MOV A, R5

Page 14: 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

14

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

Page 15: 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

15

Example 3-5Find 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 R5N_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 R5N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1(R5=1) JNC OVER ;jump if CY=0 INC R5 ;CY=1, increment 5OVER:MOV R0,A ;Now R0=A=50H,and R5=02

R5 R0

ACY

Page 16: 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

16

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

Page 17: 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

17

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

Bits 23 16 15 8 7 0

opcode = 02 target address

Page 18: 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

18

LJMP

• Jump to a new address LJMP 16-bit-target-addr.

Line PC Opcode Mnemonic Operand 17 0015 020015 HERE: LJMP HERE18 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.

Page 19: 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

19

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 15 8 7 0

opcode = 80 relative address

Page 20: 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

20

SJMP

• Jump to a new address SJMP 8-bit-relative-address

Line PC Opcode Mnemonic Operand 17 0015 80FE HERE: SJMP HERE18 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)

Page 21: 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

21

Example 3-6 (1)Using the following list file, verify the jump forward address

calculation.Line PC Opcode Mnemonic Operand01 0000 ORG OOOO02 0000 7800 MOV R0,#003 0002 7455 MOV A,#55H04 0004 6003 JZ NEXT05 0006 08 INC R006 0007 04 AGAIN: INC A07 0008 04 INC A08 0009 2477 NEXT: ADD A,#77H09 000B 5005 JNC OVER10 000D E4 CLR A11 000E F8 MOV R0,A12 OOOF F9 MOV R1,A13 OO1O FA MOV R2,A14 OO11 FB MOV R3,A15 OO12 2B OVER: ADD A,R316 OO13 50F2 JNC AGAIN17 0015 80FE HERE: SJMP HERE18 OO17 END

Page 22: 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

22

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

Page 23: 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

23

Example 3-7

Verify the calculation of backward jumps in Example 3-6.Solution:The target address < PC jump backwardJNC 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

Page 24: 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

24

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.

Page 25: 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

25

Instruction Action

JZ ( Jump Zero ) Jump if A=0JNZ ( Jump no zero ) Jump if A≠0

DJNZ Rn,target Decrement and jump if byte≠0

CJNE A,byteCompare A with byte and jump if not equal

(A≠byte)

CJNE reg,#dataCompare 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

Page 26: 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

26

Other Unconditional Jump

• JMP @A+DPTR– discuss later

• Absolute jump ( AJMP ) AJMP 11-bit-target-address

– The target address must be within 2K bytes of program memory.

Bits 15 13 12 8 7 0

target opcode target address

Page 27: 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

27

Section 3.2Call Instructions

Page 28: 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

28

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 )

Page 29: 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

29

The Flow of Control Involving a Procedure

Page 30: 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

30

Figure 3-1. 8051 Assembly Main Program That Calls Subroutines

;MAIN program calling subroutines ORG 0MAIN: LCALL SUBR_1 LCALL SUBR_2 LCALL SUBR_3HERE: SJMP HERE;---- end of MAINSUBR_1: .... .... RET;---- end of subroutine 1SUBR_2: .... .... RET;---- end of subroutine 2SUBR_3: .... .... RET; end of subroutine 3 END ;end of the asm file

Page 31: 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

31

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 = 02 target address

Page 32: 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

32

LCALL

• Jump to a new address LCALL 16-bit-target-addr.

Line PC Opcode Mnemonic Operand 04 0004 120300 LCALL DELAY05 OO07 74AA MOV A,#0AAH ... 11 0300 ORG 300H12 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

subroutine DELAY

return address target address

Page 33: 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

33

LCALL and Memory Addresses

LCALL DELAY;

MOV A#0AAH;

DELAYRAM addr.

0004

0300

0304

00070009

MOV R5,#0FFH;

RET;

0000

RAM addr.

return address

Page 34: 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

34

Example 3-8Write a program to toggle all the bits of port 1 by sending to itthe values 55H and AAH continuously. Put a time delay inbetween each issuing of data to port 1. This program will be usedto test the ports of the 8051 in the next chapter.Solution: ORG 0BACK: 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 300HDELAY:MOV R5,#OFFH ;R5=255(FF in hex), the counterAGAIN:DJNZ R5,AGAIN ;stay here until R5 becomes 0 RET ;return to caller (when R5=0) END ;end of asm file

Page 35: 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

35

Example 3-9 (1/2)Analyze the stack contents after the execution of the first LCALL inthe following.Solution: (a)001 0000 ORG 0002 0000 7455 BACK: MOV A,#55H ;load A with 55H003 0002 F590 MOV P1,A ;send 55H to port1004 0004 120300 LCALL DELAY ;time delay005 0007 74AA MOV A,#0AAH ;load A with AAH006 0009 F590 MOV P1,A ;send AAH to port1007 000B 120300 LCALL DELAY 008 000E 80F0 SJMP BACK ;keep doing this009 0010 010 0010 ;--- this is the delay subroutine011 0300 ORG 300H012 O300 DELAY:013 O300 7DFF MOV R5,#OFFH ;R5=255014 O302 DDFE AGAIN: DJNZ R5,AGAIN ;stay here015 O304 22 RET ;return to caller016 O305 END ;end of asm file

Page 36: 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

36

SP = 09

0708

0009

0A

Example 3-9 (2/2)

Solution: (b)When the first LCALL is executed, theaddress of the instruction “MOV A,#0AAH”is saved on the stack. Notice that the low bytegoes first and the high byte is last. The lastInstruction of the called subroutine must be aRET instruction which directs the CPU toPOP the top bytes of the stack into the PCand resume executing at address 07. Thediagram shows the stack frame after the firstLCALL.

Page 37: 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

37

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.

Page 38: 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

38

Example 3-10 (1/3)Analyze the stack for the first LCALL instruction in the following program.01 0000 ORG 002 0000 7455 BACK: MOV A,#55H ;load A with 55H03 0002 F590 MOV P1,A ;send 55H to port104 0004 7C99 MOV R4,#99H05 0006 7D67 MOV R5,#67H 06 0008 120300 LCALL DELAY ;time delay07 000B 74AA MOV A,#0AAH ;Load A with AA 08 000D F590 MOV P1,A ;send AAH to port 109 000F 120300 LCALL DELAY 10 0012 80EC SJMP BACK ;keep doing this11 0014 ; this is the delay subroutine12 0300 ORG 300H 13 O300 CO04 DELAY:PUSH 4 ;PUSH R414 O302 C005 PUSH 5 ;PUSH R515 O304 7CFF MOV R4,#0FFH;R4=FFH16 O306 7DFF NEXT: MOV R5,#0FFH;R5=25517 O308 DDFE AGAIN:DJNZ R5,AGAIN18 030A DCFA DJNZ R4,NEXT19 030C D005 POP 5 ;POP INTO R520 030E D004 POP 4 ;POP INTO R421 0310 22 RET ;return to caller22 0311 END ;end of asm file

Page 39: 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

39

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.

PCL0B08

PCH0009

0A

0B

PCL0B08

PCH0009

R4990A

0B

PCL0B08

PCH0009

R4990A

R5670B

After the first LCALL After PUSH 4 After PUSH 5

SP=0A SP=0BSP=09

Page 40: 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

40

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.

PCL0B08

PCH0009

0A

0B

PCL0B08

PCH0009

R4990A

0B

08

09

0A

0B

After the POP 5 After POP 4 After RET

SP=09 SP=07SP=0A

Page 41: 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

41

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 15 11 10 8 7 0

opcode target address

Page 42: 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

42

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 300H12 0300 7DFF DELAY: MOV R5,#0FFH ... 15 0304 22 RET – The opcode of ACALL is 01110B (5 bits).– The target address is 0300 (11 bits).– The return address is 0006

Page 43: 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

43

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.

Page 44: 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

44

Example 3-12Rewrite Example 3-8 as efficiently as you can.Solution: ORG 0 MOV A,#55H ;A=01010101B=55HBACK: 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 subroutineDELAY: MOV R5,#OFFH ;R5=255, the counterAGAIN: DJNZ R5,AGAIN ;jump if R5 becomes 0 RET ;return to caller END ;end of asm file

Page 45: 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

45

Section 3.3Time Delay Generation and Calculation

Page 46: 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

46

Time Delay

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

Page 47: 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

47

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.

Page 48: 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

48

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 11.0592MHz crystal oscillator is used to make the 8051-based system compatible with the serial port of the IBM PC ( See Chapter 10 ) .

Page 49: 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

49

Example 3-13

The following shows crystal frequency for three different 8051-based systems. Find the period of the machine cycle in each case.(a) 11.0592 MHz (b) 16 MHz (C) 20 MHz

Solution:

(a) 11.0592/12 = 921.6 KHz machine cycle is 1/921.6 KHz = 1.085 s (microsecond)(b) oscillator period = 1/16 MHz = 0.625 s machine cycle (MC) = 0.625 s ×12 = 0.75 s (c) 20 MHz/12 = 1.66 MHz MC = 1/1.66 MHz = 0.60 s

Page 50: 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

50

Example 3-14For an 8051 system of 11.0592 MHz, find how long it takes toexecute each of the following instructions.(a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2, target(d) LJMP (e) SJMP (f) NOP (g) MUL ABSolution:The machine cycle for a system of 11.0952 MHz is 1.085 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 = 1.085 s(b) DEC R3 1 1×1.085 s = 1.085 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 = 1.085 s(g) MUL AB 4 4×1.085 s = 4.34 s

Page 51: 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

51

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

Page 52: 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

52

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 )

Page 53: 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

53

Example 3-16Find the time delay for the following subroutine, assuming acrystal frequency of 11.0592 MHz. Machine CycleDELAY: MOV R3,#250 1

HERE: NOP 1 NOP 1 NOP 1 NOP 1 DJNZ R3,HERE 2 RET 2Solution: the HERE loop & the two instructions outside the loop { [250 (1+1+1+1+2)] + 3 } × 1.085 s = (1500+2) × 1.085 s = 1629.67 s.

Page 54: 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

54

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

Page 55: 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

55

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

Page 56: 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

56

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

Page 57: 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

57

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.