CALL
INSTRUCTIONS
􀂉 Call instruction is used to call subroutine
􀂾 Subroutines are often used to perform tasks
that need to be performed frequently
􀂾 This makes a program more structured in
addition to saving memory space
LCALL (long call)
􀂾 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 (absolute call)
􀂾 2-byte instruction
􀂃 11 bits are used for address within 2K-byte range

CALL
INSTRUCTIONS
LCALL
􀂉 When a subroutine is called, control is
transferred to that subroutine, the
processor
􀂾 Saves on the stack the 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

CALL
INSTRUCTIONS
LCALL
(cont’)
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 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
Upon executing “LCALL DELAY”,
the address of instruction below it,
“MOV A,#0AAH” is pushed onto
stack, and the 8051 starts to execute
at 300H.
The counter R5 is set to
FFH; so loop is repeated
255 times.
When R5 becomes 0, control falls to the
RET which pops the address from the stack
into the PC and resumes executing the
instructions after the CALL.
The amount of time delay depends
on the frequency of the 8051

CALL
INSTRUCTIONS
CALL
Instruction and
Stack
001 0000 ORG 0
002 0000 7455 BACK: MOV A,#55H ;load A with 55H
003 0002 F590 MOV P1,A ;send 55H to p1
004 0004 120300 LCALL DELAY ;time delay
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
008 000E 80F0 SJMP BACK ;keep doing this
009 0010
010 0010 ;-------this is the delay subroutine------
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
08 07
SP = 09
09 00
0A
Stack frame after the first LCALL
Low byte goes first
and high byte is last

CALL
INSTRUCTIONS
Use PUSH/POP
in Subroutine
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
04 0004 7C99 MOV R4,#99H
05 0006 7D67 MOV R5,#67H
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
09 000F 120300 LCALL DELAY
10 0012 80EC SJMP BACK ;keeping doing
this
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
15 0304 7CFF MOV R4,#0FFH;R4=FFH
16 0306 7DFF NEXT: MOV R5,#0FFH;R5=FFH
17 0308 DDFE AGAIN: DJNZ R5,AGAIN
18 030A DCFA DJNZ R4,NEXT
19 030C D005 POP 5 ;POP into R5
20 030E D004 POP 4 ;POP into R4
21 0310 22 RET ;return to caller
22 0311 END ;end of asm file
Normally, the
number of PUSH
and POP
instructions must
always match in any
called subroutine
08 0B PCL 08 0B PCL 08 0B PCL
09 00 PCH 09 00 PCH 09 00 PCH
0A 0A 99 R4 0A 99 R4
0B 0B 0B 67 R5
After first LCALL After PUSH 4 After PUSH 5

CALL
INSTRUCTIONS
Calling
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 subroutine1
SUBR_2: ...
...
RET
;-----------end of subroutine2
SUBR_3: ...
...
RET
;-----------end of subroutine3
END ;end of the asm file
It is common to have one
main program and many
subroutines that are called
from the main program
This allows you to make
each subroutine into a
separate module
- Each module can be
tested separately and then
brought together with
main program
- In a large program, the
module can be assigned to
different programmers

CALL
INSTRUCTIONS
ACALL
􀂉 The only difference between ACALL
and LCALL is
􀂾 The target address for LCALL can be
anywhere 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

Post a Comment

0 Comments

Close Menu