Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
They are not the ONLY method, but they are the This implies that each call-site must
have a common protocol for passing parameters
most common method
store live variables at the time of the subroutine
invocation
Briefly discuss 3 motivations before mechanics to have a common protocol for returning values from
subroutine
help motivate the understanding
A Stack (and associated usage protocol) is a
Break from thinking about linear execution common method to accomplish these
towards depth-first tree-shaped execution model requirements
EE3170/CC/Lecture#12 - Part I 3 EE3170/CC/Lecture#12 - Part I 4
1
Motivation 2: Recursion Motivation 3: Interrupt Service Routines (ISR)
Recursion – a subroutine which calls itself Interrupts (we will learn about them later) enable
code (service routine) to be invoked by an outside
A variable “n” may have multiple instances at signal (i.e. service keyboard interrupt)
any point in time
Similar problem to that of Subroutines – but
Fibonacci (example) programmer does not determine call-site
fib( n ) = if ( n = 0 ) then 0
if ( n = 1 ) then 1
else fib( n-1 ) + fib( n-2 ) Because programmer can not constrain call-site,
all registers used in a ISR must be saved at the
Multiple instances implies that static variable beginning of an ISR and loaded/restored at the
addresses are not sufficient end of an ISR
2
Stack Operation & Management (added) Stacks
Stack grows from higher addresses to lower Stack Access Operations
the top of the stack grows upwards
Note: the text shows memory upside-down in its stack
Push – Put new item on top of the stack
examples so that the stack grows downward Pop – Remove the top item from stack
The bottom of the stack is often located at the highest
available memory address. Microprocessor Interpretation
Stack is a contiguous set of memory
The stack allows dynamic memory allocation
don’t have to decide when you write the program: how many
Push – Copy data from a register into the next
bytes to reserve, just push stuff on when you need to save it. available word above current top
New word becomes new top of stack
Push too much without popping Pop – Move data from top word in stack into a register
you can overwrite your program! It’s your responsibility not to Next word down in the stack becomes new top
do this.
Value of previous top is lost to the stack
3
Stack Management Stack Management
Result: code and stack to not “collide” Stack Operations
until there is no memory space left Push
Put data on stack
known as stack overflow Decrement SP (after)
overflow occurs with infinite recursion PUSH Reg
MEM[SP] = Reg
PC
SP = SP-1
Pull
Remove data from stack
Increment SP (before)
POP Reg
SP SP = SP+1
Reg= MEM[SP]
4
68HC11 Stack Management Stack Example
ORG $E100
Stack Pointer Manipulation Instructions * INITIALIZE STACK
LDS & STS START LDS #$B7FF • SP
Load and Store SP * CREATE SAMPLE DATA B7FC • • •
needed to initialize SP LDAA #$22 B7FD • A
LDAB #$33 B7FE • •
INS & DES * STORE DATA IN STACK B7FF • B
Increment and Decrement SP PSHA • •
NOT needed during normal PSHx & PULx instructions PSHB
* GET DATA FROM STACK
TSX, TXS, TSY, TYS PULA
Transfer (copy) between SP and X or Y regs PULB
SWI
5
Stack Example Stack Example
ORG $E100 ORG $E100
* INITIALIZE STACK * INITIALIZE STACK
• SP
START LDS #$B7FF • SP START LDS #$B7FF
* CREATE SAMPLE DATA B7FC • B7 FD
B7FC • B7 FE * CREATE SAMPLE DATA
LDAA #$22 B7FD • A
B7FD • A LDAA #$22
LDAB #$33 B7FE 33 22
B7FE • 22 LDAB #$33
* STORE DATA IN STACK * STORE DATA IN STACK B7FF 22 B
B7FF 22 B
PSHA • 33
• 33 PSHA
PSHB PSHB
* GET DATA FROM STACK * GET DATA FROM STACK
PULA PULA
PULB PULB
SWI SWI
6
Stack Instructions What is a Subroutine?
LDS PSHX, PSHY Subroutine (a.k.a. Procedure or Function) =
Load SP Push index register on
Creates a stack stack (2 bytes) A reusable program module
Write it once
PSHA, PSHB PULX, PULY
Store it once
Push accumulator on Pull from stack to index invoke it from several places in the main program
stack register (2 bytes) when finished it returns to the main program
Decrement SP (after) TSX, TXS, TSY, TYS
PULA, PULB Transfer the SP to index A sequence of instructions invoked so that it
register looks like a single instruction in the main
Pull from stack to source-code
accumulator Transfer index register
to SP e.g. “call” label-of-first-instruction-in-subroutine
Increment SP (before)
7
Subroutines Desirable Properties
It is desirable to have a subroutine which can be
invoked from multiple call-sites Independence
Subr. & main program may be written independently
Subr makes no references to addresses or labels defined in
This implies that each call-site must the main
have a common protocol for passing parameters and vice-versa (except for the first instruction)
store live variables at the time of the subroutine
invocation
have a common protocol for returning values from Subr. & main can be edited without affecting
subroutine each other
A Stack (and associated usage protocol) is a
common method to accomplish these Subr & main can be assembled independently
requirements and linked at loading time
EE3170/CC/Lecture#12 - Part I 29 EE3170/CC/Lecture#12 - Part I 30
8
Call and Return Subroutine Definitions
Subr. can be called from many locations in main
Always returns to the instr. immediately after the call Nesting – One subroutine calls another
subroutine
9
Return Address Linkage 68HC11 Call Instructions
Where do we store the return address?
Do not want to use a register Two “Call” Instructions
inhibits nesting and recursion BSR addr = Branch to subroutine starting at “addr”
Do not want to use a “fixed” static memory location relative addressing (short range)
inhibits nesting and recursion JSR addr = Jump to subroutine starting at “addr”
subr. would have to know a parent’s label direct, extended, or indexed addressing
It is temporary Actions:
only needed until return
Push current PC onto stack ( = return address)
would like to “liberate” that word when done
PC = “addr” (actually the calculated effective address)
Actions:
Pop stack to PC ( = return address)
PC = previously pushed return address
next instruction fetched will be instruction after the Call
Caution:
The subr. could use the stack while running
must be careful to pop everything it pushes
then return address is at “top” for RTS instruction
10
Subroutine Return Instructions Subroutine Example (added)
The only condoned way of Every subroutine should
returning from a be documented with a
subroutine is
to retrieve the return
brief “header”
address from the stack sets it off visually,
11
3 Types of Calling Conventions Register-Based Calling
Types are based on how data is Parent
passed between parent and child Stores parameters in registers
1. Register-based: passes data in registers Calls child
2. Memory-based: passes data in a fixed
memory area Child
usually uses mem space immediately following Uses parameters stored in registers
the call Stores return values in registers
management gets a little tricky Returns to parent
3. Stack-based: passes data in the stack
everybody knows where it is (SP tells them) Parent
expands and shrinks with demand Uses return values in registers
12
Register-Based Calling Conventions Memory-Based Calling Conventions
Parent
Advantages Store parameters in Data Link Area in memory
Very fast (no extra load or store) Usually the memory words right after the call
Pretty easy too. Call child
Disadvantages Child
May have more parameters than available registers get return address from the stack
Given that we have a whopping 4 registers in the HC11, this is
extremely likely. access parameters using offset from return address
Nested Calls Store any return values using offset
Require more registers Return to parent
Must coordinate register usage between subroutines
Recursion Parent
Child wants same registers as parent
Load return values from Data Link Area
Parent must move parameters before calling child
13
Stack-Based Calling Conventions Stack-Based Calling Convention
Parent Parent
Pushes parameters onto stack …
ldab #N
Calls child pshb
No address passing - address is in SP jsr adds
pula
Child …
Pops return address to safe place Child
Usually the X or Y register
Pops parameters off stack
adds pulx * save RA
clra * sum = 0
pulb * get N
Pushes return values on stack loop beq return * while b != 0
Re-pushes return address on stack aba * sum = sum + i
decb
Returns to parent bra loop
return psha * save sum
Parent pshx * push RA
Pops returned values off stack rts
14
Summary of Stacks & Subroutines
The stack is a dynamic data structure mostly maintained
by the hardware.
You need to initialize the stack pretty much all the time.
Pushing puts data on the top of the stack.
Pulling takes data off the top of the stack.
Subroutines are called by jsr or bsr and returned from by
rts.
Three different calling conventions:
Register-based
Memory-based
Stack-based
EE3170/CC/Lecture#12 - Part I 57
15