Control Transfer Instructions Jump, Loop, and Call 1
Jump Instructions JZ label ; Jump if A=0 JNZ label ; Jump if A!=0 DJNZ reg, label ; Decrement and Jump if A (or reg.)!=0 CJNE A, byte ; Compare and Jump if A!=byte CJNE reg, #data ; Compare and Jump if byte!=data JC ; Jump if Carry=1 JNC ; Jump if Carry=0 JB ; Jump if bit =1 JNB ; Jump if bit =0 JBC ; Jump if bit=1 and clear bit 2
DJNZ Decrement and Jump if not zero (DJNZ) DJNZ register, label Ex1: Count from 1 to 20 and sent the count to P1. Ex2: Add 3 to the ACC ten times. Ex3: What is the maximum number of times that the loop in the Ex2 can be repeated? Ex4: Write a program to (a) add the ACC with the value 55H, and (b) complement the ACC 700 times. 3
JZ JZ: Jump if A=0 JZ label Ex1: MOV A, R0 JZ Over ; jump if A=0 MOV A, R1 JZ Over ; jump if A=0 Over: 4
JNZ, JNC, and JC JNZ: Jump if A!=0 JNZ label Ex1: Write a program to determine if R5 contains the value of 0. If so, put 55H in it. JNC: Jump if C=0 JNC label JC: Jump if C=1 JC label 5
JNC Examples JNC: Jump if C=0 JNC label Ex1: Find the sum of the values 79H, F5H, and E2H. Put the sum in registers R0 (low byte) and R5 (high byte) 6
LJMP: 3-byte instruction Uncondition Jump 0000 64KB ROM FFFF 7
Uncondition Jump SJMP: 2-byte instruction (Range: -128 (backward) ~ +127 (forward) bytes) 00 256B ROM FF 8
0006+03 Jump Forward and Backward Ex1: Using the following list file, verify the jump forward address calculation. lines 1 2 3 4 5 6 7 8 9 PC 0000 0000 0002 0004 0006 0007 0008 0009 000B Opcode 7800 7455 6003 08 04 04 2477 5005 Instructions ORG 0 MOV R0, #0 JZ Next INC R0 Again: INC A MOV A, #55H =0009 Jump forward! INC A Next: ADD A, #77H JNC Over Jump forward! 9
Jump Forward and Backward 000D+05 =0012 lines PC Opcode Instructions 10 000D E4 CLR A 11 000E F8 MOV R0, A 12 000F F9 MOV R1, A 13 0010 FA MOV R2, A 14 0011 FB MOV R3, A 0015+F2 =0107 15 16 0012 0013 2B 50F2 Over: ADD A, R3 JNC Again Jump backward! 0017+FE =0115 17 18 0015 0017 80FE Here: SJMP Here END Jump backward! 10
LCALL (Long Call) LCALL: 3-byte instruction When subroutine is called Control is transferred to that subroutine Processor saves the PC onto the Stack and begins to fetch instructions from new location. RET (return to caller) POP from Stack to PC 0000 FFFF 64KB ROM Ex1: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 is used to test the ports of the 8051) 11
Main Program and Calls ORG 0 Main: LCALL Sub_1 LCALL Sub_2 LCALL Sub_3 Here: SJMP Here ; end of main Sub_1:. RET : end of Sub_1 Sub_2:. RET : end of Sub_2 Sub_3:. RET : end of Sub_3 END 12
ACALL (Absolute Call) ACALL: 2-byte instruction ACALL s target address must be with in a 2KB range 0000 2KB ROM =2*2 10 =2 11 07FF 13
Time Delay Generation and Calculation Crystal frequency (XTAL): 4MHz ~ 30MHz Using 11.0592MHz to make the 8051 compatible with the serial port of the IBM PC. Ex1: Find the period of the machine cycle (a) 11.0592MHz (b) 16MHz (c) 20MHz Ex2: For an 8051 system of 11.0592 MHz, find how long it takes to execute each of the following instructions MOV R3, #55H 1 DEC R3 1 DJNZ R2, target 2 LJMP 2 SJMP 2 NOP 1 MUL AB 4 14
Delay Calculation Ex3: Find the size of the delay in the following program if XTAL =11.0592MHz MOV A, #55H Again: MOV P1, A ACALL Delay CPL A SJMP Again Delay: MOV R3, #200 Here: DJNZ R3, Here RET 15
NOP NOP is used for increasing the delay Ex4: Find the time delay for the following subroutine, XTAL=11.0592MHz Delay: MOV R3, #250 Here: NOP NOP NOP NOP DJNZ R3, Here RET 16
Nested Loop Ex5: Find the time delay for the following subroutine Delay: MOV R2, #200 Again: MOV R3, #250 Here: NOP NOP DJNZ R3, Here DJNZ R2, Again RET 17