-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
1/12
3.4 Subroutine and Stack
1
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
2/12
Subroutine
• A subroutine is a sequence of
instructions that can be called from
different places in a program.
• Two reasons for creating subroutines: – The problem is too big: Easier to divide the
problem into smaller subproblems
–
There are several places in a program thatneed to perform the same operation.
2
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
3/12
!alling subroutine: "s it #ust branching$
• !alling subroutine involves the %#umping& part' which is the same as branching.
• (ut we need to get back to the main program after the subroutine returns.
• Thus' we need a location in which the return address can be stored.
3Image courtesy of S. Katzen, The essential PIC18 Microcontroller, Sringer
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
4/12
)eturn Address Stack
• The program counter is pushed to the top ofthe return address stack when calling a
subroutine.
•
3*wordb+,*bit memor+• -bit stack pointer STKPTR / pointing to the
top of stack TOS /
!
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
5/12
)eturn Address Stack 0ointer ST10T)/
• ST10T) is 2 when powered up• ST10T) is * upon the first subroutine
call.
• Thus' the stack has 3* spaces foraddress storage. – seful for nested subroutine calls.
• The TS address is readable andwritable through three registers: TS'
TS5' TS6.
"
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
6/12
7emonstration: Subroutine7emo.asm
• 8ou can run this demonstration +ourself at homeb+ following these instructions: – 7ownload the Subroutine7emo.asm file available at
(lackboard.
–
!reate a new pro#ect in 906A(. or instructions onhow to do this' refer to Tutorial ;eek 3/.
– Step through the program and stop when the green
arrow is pointing to the call instruction.
– pen the 5ardware Stack window b+ selecting
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
7/12
call instruction
• Encodes subroutineaddress b+ absolute
address similar to
goto/.
• "ncrement stack
pointer ST10T)/
• Top of )eturn
Address Stack TS/= address of ne>t
instruction following
the call instruction.$
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
8/12
return instruction
• 0ut Top of )eturnAddress Stack TS/
to 0rogram !ounter
0!/.
• 7ecrement stack
pointer ST10T)/
8
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
9/12
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
10/12
?ested subroutines
;hat happens if " call another subroutineS),/ within a subroutine S)*/$
1&
Image courtesy of S. Katzen, The essential PIC18 Microcontroller,
Sringer
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
11/12
A demonstration on nested subroutines
• ;rite a program to calculate the value off2>2@/ where – f>/ = g>2>*B/ 2>2-
– g>/ = >2>*,
• )eferring to the Subroutine7emo.asm
file uploaded to (lackboard' determine
the status of the stack and the values ofTS' and ST10T) at 0oints *4.
11
-
8/19/2019 Chapter 3B PIC18AssemblyLanguageProgrammingPartB
12/12
8ou should be able to ....
• !ode 0"! subroutine.
• 7escribe the stack and its use in
subroutine.
• 7iscuss how branching and subroutine
calling affect the pipelining mechanism.
• 7escribe the difference of the call and
rcall instructions.
12