LOOP AND
JUMP
INSTRUCTIONS
Conditional
Jumps
MOV A,R0 ;A=R0
JZ OVER ;jump if A = 0
MOV A,R1 ;A=R1
JZ OVER ;jump if A = 0
...
OVER:
􀂉 Jump only if a certain condition is met
JZ label ;jump if A=0
Determine if R5 contains the value 0. If so, put 55H in it.
MOV A,R5 ;copy R5 to A
JNZ NEXT ;jump if A is not zero
MOV R5,#55H
NEXT: ...
Can be used only for register A,
not any other register

LOOP AND
JUMP
INSTRUCTIONS
Conditional
Jumps
(cont’)
􀂉 (cont’)
JNC label ;jump if no carry, CY=0
􀂾 If CY = 0, the CPU starts to fetch and execute
instruction from the address of the label
􀂾 If CY = 1, it will not jump but will execute the next
instruction below JNC
Find the sum of the values 79H, F5H, E2H. Put the sum in registers
R0 (low byte) and R5 (high byte).
MOV A,#0 ;A=0
MOV R5,A ;clear R5
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
JNC N_2 ;jump if CY=0
INC R5 ;if CY=1,increment R5 (R5=1)
N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1
JNC OVER ;jump if CY=0
INC R5 ;if CY=1, increment 5
OVER: MOV R0,A ;now R0=50H, and R5=02
MOV R5,#0

LOOP AND
JUMP
INSTRUCTIONS
Conditional
Jumps
(cont’)
􀂉 All conditional jumps are short jumps
􀂾 The address of the target must within
-128 to +127 bytes of the contents of PC
JBC Jump if bit = 1 and clear bit
JNB Jump if bit = 0
JB Jump if bit = 1
JNC Jump if CY = 0
JC Jump if CY = 1
CJNE reg,#data Jump if byte ≠ #data
CJNE A,byte Jump if A ≠ byte
DJNZ Decrement and Jump if A ≠ 0
JNZ Jump if A ≠ 0
JZ Jump if A = 0
Instructions Actions
8051 conditional jump instructions

LOOP AND
JUMP
INSTRUCTIONS
Unconditional
Jumps
􀂉 The unconditional jump is a jump in
which control is transferred
unconditionally to the target location
LJMP (long jump)
􀂾 3-byte instruction
􀂃 First byte is the opcode
􀂃 Second and third bytes represent the 16-bit
target address
– Any memory location from 0000 to FFFFH
SJMP (short jump)
􀂾 2-byte instruction
􀂃 First byte is the opcode
􀂃 Second byte is the relative target address
– 00 to FFH (forward +127 and backward
-128 bytes from the current PC)

LOOP AND
JUMP
INSTRUCTIONS
Calculating
Short Jump
Address
􀂉 To calculate the target address of a
short jump (SJMP, JNC, JZ, DJNZ, etc.)
􀂾 The second byte is added to the PC of the
instruction immediately below the jump
􀂉 If the target address is more than -128
to +127 bytes from the address below
the short jump instruction
􀂾 The assembler will generate an error
stating the jump is out of range

LOOP AND
JUMP
INSTRUCTIONS
Calculating
Short Jump
Address
(cont’)
Line PC Opcode Mnemonic Operand
01 0000 ORG 0000
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 000F F9 MOV R1,A
13 0010 FA MOV R2,A
14 0011 FB MOV R3,A
15 0012 2B OVER: ADD A,R3
16 0013 50F2 JNC AGAIN
17 0015 80FE HERE: SJMP HERE
18 0017 END

Post a Comment

0 Comments

Close Menu