5.subroutines and Stacks PDF
5.subroutines and Stacks PDF
Subroutines
Parameter Passing
Stack Frame
Subroutines
• It is often necessary to perform a particular task
many times on different data values
• When a program branches to a subroutine, we say
that it is calling the subroutine
• The instruction that performs the branch is called a
call_subroutine instruction
• After a subroutine has been executed, the calling
program must resume execution: return
• Execution continues at the instruction immediately
after the instruction that called the subroutine
• The location where the calling program resumes
execution is the location pointed to by the PC while
the call_subroutine is being executed
2
Subroutines (cont.)
• The contents of the PC must be saved by the
call_subroutine instruction to enable correct
return to the calling program
• Call_subroutine is a special branch instruction
that performs the following operations
– store the contents of the PC in the link register (LR)
– branch to the target address specified by the
instruction
• The return from a subroutine branches to the
address contained in the link register
3
Subroutine Linkage
Memory Calling program Memory Subroutine
location location SUB
….
200 Call SUB 1000 first instruction
204 next instruction …..
…. …..
Return
1000
PC 204 Performed by
the processor
automatically
Link Register 204
Call Return Q?
4
Subroutine Nesting
• When one subroutine calls another
• The return address of the second call is also stored
in the link register
• It is essential to save the content of the link register
in another location before issuing a new call
• Subroutine nesting can be carried out to any depth
• A stack data structure can be used to store the
return addresses associated with subroutine calls
• Call-subroutine pushes the content of the PC onto
the stack and loads the subroutine address into the
PC
• The return instruction pops the return address from
the stack into the PC
5
Parameter Passing
• What do you know about parameter passing in C ?
int CallingFunction()
{
int test1;
int test2;
int *ptr1;
…
test1 = Procedure(test2, ptr);
if (test1 > 0) test2 = 0;
else test2 = Subroutine();
return test2;
}
if (subtest2 == 0) test3 = … ;
else test3 = … ;
return test3;
6 }
Parameter Passing
• When calling a subroutine, a program must provide the subroutine
with parameters to be used in the computation
• Later the subroutine may return other parameters resulting from the
computation
• The parameters may be placed in registers or in the stack (local
variables) or in fixed memory locations (global variables)
7
Parameter Passing Using Registers
Calling program
Move N, R1 R1 serves as a counter
Move #NUM1, R2 R2 points to the list
Call LISTADD Call subroutine
Move R0, SUM Save result
…
Subroutine
LISTADD Clear R0 Initialize sum to 0
LOOP Add (R2)+, R0 Add entry from list
Decrement R1
Branch > 0 LOOP
Return Return to calling program
the calling program may need to retain information in some registers for use after
returning from the subroutine
8
Registers vs. Stack
• What are registers?
– A small amount of very fast computer memory used to speed the
execution of computer programs by providing quick access to
commonly used values—typically, the values being calculated at a
given point in time.
– Most, but not all, modern computer architectures operate on the
principle of moving data from main memory into registers,
operating on them, then moving the result back into main
memory—a so-called load-store architecture.
– The top of the memory hierarchy, and provide the fastest way for
the system to access data. The term is often used to refer only to
the group of registers that can be directly indexed for input or
output of an instruction.
9
Registers vs. Stack
• What is the stack?
– A memory block used to temporarily
save values, beyond the amount of
data that registers can hold
– Push adds a given node to the top of
the stack leaving previous nodes
below.
– Pop removes and returns the
current top node of the stack.
– Typically grows towards descending
addresses
10
Parameter Passing Using the Stack
• Alternatively, parameters can be placed on the stack
– Using a stack is flexible
• No need to match which registers are using
– A stack can handle a large number of parameters
Example
• Top of stack is at level 1 when execution starts
Main program
Move #NUM1,-(SP) push parameters onto stack
Move N,-(SP)
Call LISTADD call subroutine
(top of stack at level 2)
Move 4(SP),SUM save result
Add #8,SP restore top stack
(top of stack at level 1)
11
Parameter Passing Using the Stack
Subroutine
LISTADD MoveMultiple R0-R2,-(SP) save registers to be used
(top of the stack at level 3)
SP
12
Parameter Passing Using the Stack
Subroutine
LISTADD MoveMultiple R0-R2,-(SP) save registers to be used
(top of the stack at level 3)
Move 16(SP), R1 initialize counter to n
Move 20(SP), R2 initialize pointer to the list
Clear R0 initialize sum to 0
LOOP Add (R2)+, R0 add entry from list
Decrement R1
Branch>0 LOOP
Move R0, 20(SP) put result on the stack
MoveMultiple (SP)+,R0-R2 restore registers
Return return to calling program
SP
SP+4
SP+8
SP+16
SP+20
13
Parameter Passing Using the Stack
• The subroutine uses three registers
• The contents of these registers are saved by
pushing them on the stack using the
move_multiple instruction
• Before return, they are restored
14
Parameter Passing
• Passing by reference
– Instead of passing the actual list entries, the
calling program passes a pointer, which is the
address of the list in the memory
• Passing by value
– The actual number of entries n is passed to the
subroutine
15
Layout of a Typical Stack Frame
SP saved [R1] • Stack management
– During the execution of the
saved [R0]
subroutine, 6 locations at the top of
localvar3 stack contain entries that are
needed by the subroutine
localvar2
stack – These locations are in a private work
localvar1 frame space for the subroutine created at
FP saved [FP] the time the subroutine is entered
for
(frame and freed up when the subroutine
pointer) Return address called returns
param1 subroutine • I.e., Local variables in high-level
languages are created in the
param2
stack; thus they are temporary
param3 – This work space is called a stack
param4
frame.
Old
TOS
16
Layout of a Typical Stack Frame
• Assuming the SP point to the old TOS
• Before the subroutine is called, the calling
program pushes the 4 parameters onto the
SP saved [R1] stack
• The call instruction is executed and the
saved [R0] return address is pushed onto the stack
localvar3 • The first two instructions executed by the
subroutine push the content of FP (for main)
onto the stack
localvar2
stack Move FP, -(SP)
localvar1 Move SP, FP
frame
• Now, both FP and SP point to the location of
FP saved [FP] for the saved FP
(frame • Space for the three local variable is
pointer) Return address called allocated
subroutine Subtract #12, SP
param1
• The contents of R0 and R1 are saved
param2 • The subroutine executes its task
• The saved R0 and R1 are popped back
param3 • The local variables are removed from the
stack frame
param4 Add #12,SP
Old
TOS
17
Layout of a Typical Stack Frame
The stack pointer moves during the
SP execution of the subroutine and
saved [R1] always points to the top of the stack
saved [R0] FP provides convenient access to
the parameters passed to the
localvar3 subroutine and to the local memory
localvar2 variables used by the subroutine
stack 4 parameters are passed to the
localvar1 frame subroutine
FP saved [FP] 3 local variables are being used
for
(frame within the subroutine
pointer) Return address called R0 and R1 are saved because they
subroutine will be used within the subroutine
param1
The parameters can be accessed by
param2 using 8(FP), 12(FP) , …
param3 The local variables can be accessed
by using –4(FP), -8(FP)
param4 The content of FP remains fixed
Old throughout the execution of the
TOS
18 subroutine
Layout of a Typical Stack Frame
SP saved [R1] • Returning the subroutine
saved [R0] • Restore R0 and R1
• The saved old value of FP is popped
localvar3
back into FP
localvar2 • SP points to the return address
localvar1 • The return instruction is executed
FP • The calling program is responsible for
saved [FP]
(frame removing the parameters from the
pointer) Return address stack.
param1 • Some of these parameters may be
results passed back to the calling
param2 program.
param3
param4
Old
TOS
19
Stack Frame
Top of stack after
procedure call
Return Address
Inputs to Procedure
First subroutine
2100 SUB1 Move FP, -(SP) save frame pointer from main
2104 Move SP, FP load the frame pointer for sub
2108 MoveMultiple R0-R3, -(SP) save registers
2112 Move 8(FP), R0 get first parameter
Move 12(FP), R1 get second parameter
….
Move PARAM3, -(SP) place parameter on stack
2160 call SUB2
2164 Move (SP)+, R2 Pop SUB2 result into R2
….
Move R3, 8(FP) place answer on the stack
MoveMultiple (SP)+, R0-R3 restore registers
Move (SP)+, FP restore frame pointer register
Return
21
Draw its stack frame
Stack Frame for nested subroutines
Second Subroutine
22