JUMP, LOOP AND CALL
INSTRUCTIONS
The 8051 Microcontroller and Embedded
Systems: Using Assembly and C
Mazidi, Mazidi and McKinlay
Repeating a sequence of instructions a
LOOP AND certain number of times is called a
JUMP loop
INSTRUCTIONS
Loop action is performed by
DJNZ reg, Label
Looping
The register is decremented
If it is not zero, it jumps to the target address
Referred to by the label
A loop can be repeated a Prior to the start of loop the register is loaded
maximum of 255 times, if
with the counter for the number of repetitions
R2 is FFH
Counter can be R0– R7 or RAM location
;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
2
If we want to repeat an action more
LOOP AND
JUMP
times than 256, we use a loop inside a
INSTRUCTIONS loop, which is called nested loop
We use multiple registers to hold the
NestedLoop count
Write a program to (a) load the accumulator with the value 55H, and
(b) complement the ACC 700 times
MOV A,#55H ;A=55H
MOV R3,#10 ;R3=10, outer loop count
NEXT: MOV R2,#70 ;R2=70, inner loop count
AGAIN: CPL A ;complement A register
DJNZ R2,AGAIN ;repeat it 70 times
DJNZ R3,NEXT
3
Jump only if a certain condition is met
LOOP AND JZ label ;jump if A=0
JUMP MOV A,R0 ;A=R0
INSTRUCTIONS JZ OVER ;jump if A = 0
MOV A,R1 ;A=R1
JZ OVER ;jump if A = 0
Conditional ...
Jumps OVER: Can be used only for register A,
not any other register
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: ...
4
(cont’)
LOOP AND JNC label ;jump if no carry, CY=0
JUMP If CY = 0, the CPU starts to fetch and execute
INSTRUCTIONS instruction from the address of the label
If CY = 1, it will not jump but will execute the next
Conditional instruction below JNC
Jumps Find the sum of the values 79H, F5H, E2H. Put the sum in registers
(cont’) R0 (low byte) and R5 (high byte).
MOV R5,#0
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
5
8051 conditional jump instructions
LOOP AND Instructions Actions
JUMP JZ Jump if A = 0
INSTRUCTIONS JNZ Jump if A ≠ 0
DJNZ Decrement and Jump if A ≠ 0
CJNE A,byte Jump if A ≠ byte
Conditional
CJNE reg,#data Jump if byte ≠ #data
Jumps
JC Jump if CY = 1
(cont’)
JNC Jump if CY = 0
JB Jump if bit = 1
JNB Jump if bit = 0
JBC Jump if bit = 1andclearbit
All conditional jumps are short jumps
The address of the target must within -
128 to +127 bytes of the contents of PC
6
The unconditional jump is a jump in
LOOP AND which control is transferred
JUMP unconditionally to the target location
INSTRUCTIONS
LJMP (longjump)
Unconditional 3-byte instruction
Jumps First byte is the opcode
Second and third bytes represent the 16-bit
target address
– Any memory location from 0000 to FFFFH
SJMP (shortjump)
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)
7
To calculate the target address of a
LOOP AND
JUMP
short jump(SJMP, JNC, JZ, DJNZ, etc.)
INSTRUCTIONS The second byte is added to the PC of the
instruction immediately below the jump
Calculating If the target address is more than -128
Short Jump to +127 bytes from the address below
Address
the short jump instruction
The assembler will generate an error
stating the jump is out of range
8
Line PC Opcode Mnemonic Operand
LOOP AND 01 0000 ORG 0000
02 0000 7800 MOV R0,#0
JUMP
03 0002 7455 MOV A,#55H
INSTRUCTIONS 04 0004 6003 JZ NEXT
05 0006 08 INC R0
Calculating 06 0007 04 AGAIN: INC A
07 0008 04 + INC A
Short Jump 08 0009 2477 NEXT: ADD A,#77H
Address 09 000B 5005 JNC OVER
(cont’) 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
9
Call instruction is used to call subroutine
CALL
Subroutines are often used to perform tasks
INSTRUCTIONS that need to be performed frequently
This makes a program more structured in
addition to saving memory space
LCALL (longcall)
3-byte instruction
First byte is the opcode
Second and third bytes are used for address of
target subroutine
– Subroutine is located anywhere within 64K
byte address space
ACALL (absolutecall)
2-byte instruction
11 bits are used for address within 2K-byte
range
10
When a subroutine is called, control is
CALL
INSTRUCTIONS
transferred to that subroutine, the
processor
LCALL Saves on the stack the address of the
instruction immediately below the LCALL
Begins to fetch instructions form the new
location
After finishing execution of the
subroutine
The instruction RET transfers control back
to the caller
Every subroutine needs RET as the last
instruction
11
ORG 0
BACK: MOV A,#55H ;load A with 55H
CALL MOV P1,A ;send 55H to port 1
LCALL DELAY ;time delay
INSTRUCTIONS MOV A,#0AAH ;load A with AA (in hex)
MOV P1,A ;send AAH to port 1
LCALL DELAY
LCALL SJMP BACK ;keep doing this indefinitely
(cont’) Upon executing “LCALL DELAY”,
the address of instruction below it,
The counter R5 is set to “MOV A,#0AAH” is pushed onto
FFH; so loop is repeated stack, and the 8051 starts to execute
255 times. at 300H.
;---------- this is delay subroutine ------------
ORG 300H ;put DELAY at address 300H
DELAY: MOV R5,#0FFH ;R5=255 (FF in hex), counter
AGAIN: DJNZ R5,AGAIN ;stay here until R5 become 0
RET ;return to caller (when R5 =0)
END ;end of asm file
When R5 becomes 0, control falls to the
The amount of time delay depends RET which pops the address from the stack
on the frequency of the 8051 into the PC and resumes executing the
instructions after the CALL.
12
001 0000 ORG 0
002 0000 7455 BACK: MOV A,#55H ;load A with 55H
CALL 003 0002 F590 MOV P1,A ;send 55H to p1
004 0004 120300 LCALL DELAY ;time delay
INSTRUCTIONS 005 0007 74AA MOV A,#0AAH ;load A with AAH
006 0009 F590 MOV P1,A ;send AAH to p1
007 000B 120300 LCALL DELAY
CALL 008 000E 80F0 SJMP BACK ;keep doing this
Instruction and 009 0010
010 0010 ;-------this is the delay subroutine------
Stack 011 0300 ORG 300H
012 0300 DELAY:
013 0300 7DFF MOV R5,#0FFH ;R5=255
014 0302 DDFE AGAIN: DJNZ R5,AGAIN ;stay here
015 0304 22 RET ;return to caller
016 0305 END ;end of asm file
Stack frame after the first LCALL
0A
09 00 Low byte goes first
08 07
and high byte is last
SP=09
13
01 0000 ORG 0
02 0000 7455 BACK: MOV A,#55H ;load A with 55H
03 0002 F590 MOV P1,A ;send 55H to p1
CALL 04
05
0004
0006
7C99
7D67
MOV R4,#99H
MOV R5,#67H
INSTRUCTIONS 06 0008 120300 LCALL DELAY ;time delay
07 000B 74AA MOV A,#0AAH ;load A with AA
08 000D F590 MOV P1,A ;send AAH to p1
Use PUSH/ 09 000F 120300 LCALL DELAY
10 0012 80EC SJMP BACK ;keeping doing
POP in this
Subroutine 11 0014 ;-------this is the delay subroutine------
12 0300 ORG 300H
13 0300 C004 DELAY: PUSH 4 ;push R4
14 0302 C005 PUSH 5 ;push R5
Normally, the 15 0304 7CFF MOV R4,#0FFH;R4=FFH
number of PUSH 16 0306 7DFF NEXT: MOV R5,#0FFH;R5=FFH
and POP 17 0308 DDFE AGAIN: DJNZ R5,AGAIN
18 030A DCFA DJNZ R4,NEXT
instructions must 19 030C D005 POP 5 ;POP into R5
always match in any
20 030E D004 POP 4 ;POP into R4
called subroutine 21 0310 22 RET ;return to call er
22 03 11 AfterfirstLCALL END
AfterPUSH4 ;end of asm fil e
AfterPUSH5
0B 0B 0B67R5
0A 0A99R40A99R4
0900PCH0900PCHPCH 0900
Department of Computer Science and Information Engineering
080BPCL080BPCL080BPCL
HANEL 14
Cheng Kung University, TAIWAN
;MAIN program calling subroutines
ORG 0
CALL MAIN: LCALL SUBR_1 It is common to have one
LCALL SUBR_2 main program and many
INSTRUCTIONS LCALL SUBR_3 subroutines that are called
from the main program
HERE: SJMP HERE
Calling ;-----------end of MAIN
Subroutines SUBR_1: ...
... This allows you to make
RET each subroutine into a
;-----------end of subroutine1 separate module
SUBR_2: ... -Each module can be
... tested separately and then
RET brought together with
;-----------end of subroutine2 main program
SUBR_3: ...
-In a large program, the
... module can be assigned to
RET different programmers
;-----------end of subroutine3
END ;end of the asm file
15
The only difference between ACALL
CALL And LCALL is
INSTRUCTIONS
The target address for LCALL can be
ACALL any where within the 64K byte
address
The target address of ACALL must be
within a 2K-byte range
The use of ACALL instead of LCALL
can save a number of bytes of
program ROM space
16
ORG 0
BACK: MOV A,#55H ;load A with 55H
CALL MOV P1,A ;send 55H to port 1
INSTRUCTIONS LCALL DELAY ;time delay
MOV A,#0AAH ;load A with AA (in hex)
MOV P1,A ;send AAH to port 1
ACALL LCALL DELAY
(cont’) SJMP BACK ;keep doing this indefinitely
...
END ;end of asm file
A rewritten program which is more efficiently
ORG 0
MOV A,#55H ;load A with 55H
BACK: MOV P1,A ;send 55H to port 1
ACALL DELAY ;time delay
CPL A ;complement reg A
SJMP BACK ;keep doing this indefinitely
...
END ;end of asm file
17
CPU executing an instruction takes a
TIME DELAY
FOR VARIOUS
certain number of clock cycles
8051CHIPS These are referred as to as machine cycles
The length of machine cycle depends
on the frequency of the crystal
oscillator connected to 8051
In original 8051, one machine cycle
lasts 12 oscillator periods
Find the period of the machine cycle for 11.0592 MHz crystal
frequency
Solution:
11.0592/12 = 921.6 kHz;
machine cycle is 1/921.6 kHz = 1.085μs
18
For 8051 system of 11.0592 MHz, find how long it takes to execute
TIME DELAY each instruction.
(a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target
FOR VARIOUS (d) LJMP (e) SJMP (f) NOP (g) MUL AB
8051 CHIPS
(cont’) Solution:
Machine cycles Time to execute
(a) 1 1x1.085μ s = 1.085μ s
(b) 1 1x1.085μ s = 1.085μ s
(c) 2 2x1.085μ s = 2.17μs
(d) 2 2x1.085μ s = 2.17μs
(e) 2 2x1.085μ s = 2.17μs
(f) 1 1x1.085μ s = 1.085μ s
(g) 4 4x1.085μ s = 4.34μs
19
Find the size of the delay in following program, if the crystal
TIME DELAY frequency is 11.0592MHz.
FOR VARIOUS
MOV A,#55H
8051CHIPS AGAIN: MOV P1,A
ACALL DELAY
Delay CPL A
SJMP AGAIN A simple way to short jump
Calculation ;---time delay------- to itself in order to keep the
DELAY: MOV R3,#200 microcontroller busy
HERE: DJNZ R3,HERE HERE: SJMP HERE
RET We can use the following:
SJMP $
Solution:
Machine cycle
DELAY: MOV R3,#200 1
HERE: DJNZ R3,HERE 2
RET 2
Therefore, [(200x2)+1+2]x1.085μs = 436.255μs.
20
Find the size of the delay in following program, if the crystal
TIME DELAY frequency is 11.0592MHz.
FOR VARIOUS
Machine Cycle
8051CHIPS
DELAY: MOV R3,#250 1
HERE: NOP 1
Increasing NOP 1
NOP 1
DelayUsing
NOP 1
NOP DJNZ R3,HERE 2
RET 2
Solution:
The time delay inside HERE loop is
[250(1+1+1+1+2)]x1.085μs = 1627.5μs.
Adding the two instructions outside loop we
have 1627.5μs + 3 x 1.085μs = 1630.755μs
21
Find the size of the delay in following program, if the crystal
TIME DELAY frequency is 11.0592MHz.
FOR VARIOUS Machine Cycle
8051CHIPS DELAY: MOV R2,#200 1
Notice in nested loop,
AGAIN: MOV R3,#250 1
HERE: NOP 1 as in all other time
Large Delay NOP 1 delay loops, the time
UsingNested DJNZ R3,HERE 2 is approximate since
we have ignored the
Loop DJNZ R2,AGAIN 2
first and last
RET 2
instructions in the
Solution: subroutine.
For HERE loop, we have (4x250)x1.085μs=1085μs.
For AGAIN loop repeats HERE loop 200 times, so
we have 200x1085μs=217000μs. But “MOV
R3,#250” and “DJNZ R2,AGAIN” at the start and
end of the AGAIN loop add (3x200x1.805)=651μs.
As a result we have 217000+651=217651μs.
22
Two factors can affect the accuracy of
TIMEDELAYthe delay
FORVARIOUS
Crystal frequency
8051CHIPS
The duration of the clock period of the machine
cycle is a function of this crystal frequency
Delay 8051design
Calculationfor
The original machine cycle duration was set at 12 clocks
Other8051 Advances in both IC technology and CPU
design in recent years have made the 1-clock
machine cycle a common feature
Clocks per machine cycle for various 8051 versions
Chip/Maker Clocks per Machine Cycle
AT89C51Atmel 12
P89C54X2Philips 6
DS5000DallasSemi 4
DS89C420/30/40/50DallasSemi 1
23
Find the period of the machine cycle (MC) for various versions of
TIME DELAY 8051, if XTAL=11.0592 MHz.
(a) AT89C51 (b) P89C54X2 (c) DS5000 (d) DS89C4x0
FOR VARIOUS
Solution:
8051CHIPS (a) 11.0592MHz/12 = 921.6kHz;
MC is 1/921.6kHz = 1.085μs = 1085ns
Delay (b) 11.0592MHz/6 = 1.8432MHz;
MC is 1/1.8432MHz = 0.5425μs = 542ns
Calculationfor (c) 11.0592MHz/4 = 2.7648MHz ;
Other 8051 MC is 1/2.7648MHz = 0.36μs = 360ns
(d) 11.0592MHz/1 = 11.0592MHz;
(cont’) MC is 1/11.0592MHz = 0.0904μs = 90ns
24
Instruction 8051 DSC89C4x0
MOV R3,#55 1 2
TIME DELAY DEC R3 1 1
DJNZ R2 target 2 4
FOR VARIOUS LJMP 2 3
8051CHIPS SJMP 2 3
NOP 1 1
Delay MUL AB 4 9
Calculationfor For an AT8051 and DSC89C4x0 system of 11.0592 MHz, find how
Other 8051 long it takes to execute each instruction.
(a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target
(cont’) (d) LJMP (e) SJMP (f) NOP (g) MUL AB
Solution:
AT8051 DS89C4x0
(a) 1×1085ns = 1085ns 2×90ns = 180ns
(b) 1×1085ns = 1085ns 1×90ns = 90ns
(c) 2×1085ns = 2170ns 4×90ns = 360ns
(d) 2×1085ns = 2170ns 3×90ns = 270ns
(e) 2×1085ns = 2170ns 3×90ns = 270ns
(f) 1×1085ns = 1085ns 1×90ns = 90ns
(g) 4×1085ns = 4340ns 9×90ns = 810ns
25