Date post: | 21-Dec-2015 |
Category: |
Documents |
View: | 220 times |
Download: | 5 times |
COMP3221 lec15-function-I.1 Saeid Nooshabadi
COMP 3221
Microprocessors and Embedded Systems
Lectures 6 : Functionsin C/ Assembly - I
http://www.cse.unsw.edu.au/~cs3221
April, 2004
Modified from notes by Saeid Nooshabadi
COMP3221 lec15-function-I.2 Saeid Nooshabadi
Overview° C functions° Bookkeeping for function call/return° Instruction support for functions° Nested function calls° C memory allocation: static, heap, stack° Conclusion
COMP3221 lec15-function-I.5 Saeid Nooshabadi
C functions
main(void) {int i,j,k,m;
i = mult(j,k); ... ;m = mult(i,i); ...}
int mult (int mcand, int mlier)
{int product = 0;while (mlier > 0) { product = product + mcand; mlier = mlier -1;
}return product;
}
What information mustcompiler/program keep track of?
COMP3221 lec15-function-I.6 Saeid Nooshabadi
Basics of Function Call
...(use regs)
set up args
jump to function
access args
... compute result ...
...(use regs)
set up return value
jump back to caller
CallerCallee
access result
...(use regs)
COMP3221 lec15-function-I.7 Saeid Nooshabadi
Function Call Bookkeeping
Registers for functions
lr = r14
a1, a2, a3, a4
a1
v1, v2,v3,v4,v5,v6,v7
° Procedure address
° Return address
° Arguments
° Return value
° Local variables
° Registers (conflicts)
=>ARM Procedure Call Standards (APCS) conventions for use of registers simplify bookkeeping
COMP3221 lec15-function-I.8 Saeid Nooshabadi
APCS Register Convention: Summaryregister name software name use and linkage
r0 – r3 a1 – a4 first 4 integer args
scratch registers
integer function results
r4 – r11 v1- v8 local variables
r9 sb static variable base
r10 sl stack limit
r11 fp frame pointer
r12 ip intra procedure-call scratch pointer
r13 sp stack pointer
r14 lr return address
r15 pc program counterRed are SW conventions for compilation, blue are HW
ARM Procedure Call Standard (APCS)
COMP3221 lec15-function-I.9 Saeid Nooshabadi
Instruction Support for Function Call? ... sum(a,b);... /* a,b:v1,v2 */
}int sum(int x, int y) {
return x+y;}
address1000 mov a1,v1 ; x = a1004 mov a2, v2 ; y = b 1008 mov lr,#1016 ; lr = 10161012 b sum ; jump to sum1016 ...
2000 sum: add a1,a1,a22004 mov pc, lr
C
ARM
Why mov pc, lr vs. b 1016 to return?; b 1016
COMP3221 lec15-function-I.10 Saeid Nooshabadi
b vs mov pc, lr : why jump register?°Consider a function foo that is called from several different places in your program
• each call is performed by a b instruction- b foo
• but the return jump goes to many placesmain:b bar...b foo...
bar:...b foo...
foo:...b foo...mov pc, lr
COMP3221 lec15-function-I.11 Saeid Nooshabadi
A Level of Indirection°Solves many problems in CS!
°How do you make a jump instruction behave differently each time it is executed?
• indirection•mov pc, lr returns control to last caller
- recorded in a register
°How do you make an instruction fetch a different element of an array each time though a loop?
• indirection• update index address register of ldr, and str
COMP3221 lec15-function-I.12 Saeid Nooshabadi
Accessing array elements => indirectionint sumarray(int arr[]) {int i, sum;
for(i=0;i<100;i=i+1) sum = sum + arr[i];
}
mov v1, #0 ; clear v0add a2,a1,#400 ; beyond end of arr[]
Loop: cmp a1,a2
bge Exitldr a3, [a1], #4 ; a3=arr[i], a1++add v1,v1,a3 ; v1= v1+ arr[i]b Loop
Exit: mov lr, pc
COMP3221 lec15-function-I.13 Saeid Nooshabadi
Instruction Support for Functions?° Single instruction to branch and save return
address: branch and link (bl):
° Before:
1008 mov lr, #1016 ;lr = 10161012 b sum ;goto sum
° After:
1012 bl sum ; lr = 1016,goto sum
° Why bl? Make the common case fast• and elegance
COMP3221 lec15-function-I.14 Saeid Nooshabadi
Nested Procedures (#1/2)
...sumSquare(a,b)...
int sumSquare(int x, int y) {return mult(x,x)+ y;
}
° Need to save sumSquare return address saved in lr by bl sumSquare instruction, before call to mult
• Otherwise bl mult overwrites lr
° One word per procedure in memory ?• e.g., str lr, [sp,sumSquareRA] ; sp = r13
° Recursive procedures could overwrite saved area => need safe area per function invocation => stack
COMP3221 lec15-function-I.15 Saeid Nooshabadi
Nested Procedures (#2/2)
° In general, may need to save some other info in addition to lr.
° When a C program is run, there are 3 important memory areas allocated:
• Static: Variables declared once per program, cease to exist only after execution completes
• Heap: Variables declared dynamically (such as counters in for loops, or by function malloc())
• Stack: Space to be used by procedure during execution; this is where we can save register values
COMP3221 lec15-function-I.17 Saeid Nooshabadi
C memory allocation seen by the Program
0
Address
Code Program
Static Variables declaredonce per program
HeapExplicitly created space, e.g., malloc(); C pointers
StackSpace for saved procedure informationsp
stackpointer
Static base sb
COMP3221 lec15-function-I.18 Saeid Nooshabadi
Stack Operations
° PUSH v1
° POP v1
sub sp,sp, #4 ; space on stack
str v1, [sp,#0] ; save x
ldr v1, [sp,#0] ; restore x
add sp, sp,#4 ; => stack space
SP
v1 61
61
75
...
What does sp point to?
De
creasin
g a
ddress
COMP3221 lec15-function-I.19 Saeid Nooshabadi
Stack Operations (Better Way)
° PUSH v1
° POP v1
str v1, [sp,#-4]! ; space on stack ; and save x
ldr v1, [sp], #4 ; restore x ; and reclaim stack space
SP
v1 61
61
75
...
What does sp point to?
De
creasin
g a
ddress
COMP3221 lec15-function-I.20 Saeid Nooshabadi
sp on entry Register Save Area
Local Variables
sp during
Arguments forCallees
Typical Structure of a Stack Frame
° compiler calculates total frame size (F)
° at start of call, subtract F from SP
° at end, add F to SP
° where are args from caller?
° where are args for callee
Caller Frame
Callee Frame
COMP3221 lec15-function-I.21 Saeid Nooshabadi
“And in Conclusion …” (#1/2)
° ARM Assembly language instructions• Unconditional branches: b, mov pc, rx , bl
° Operands• Registers (word = 32 bits); a1 - a3, v1 – v8, ls, sb, fp, sp, lr
COMP3221 lec15-function-I.22 Saeid Nooshabadi
“And in Conclusion …” (#2/2)° Functions, procedures one of main ways to give
a program structure, reuse code
°mov pc, Rn required instruction; most add bl (or equivalent) to make common case fast
° Registers make programs fast, but make procedure/function call/return tricky
° ARM SW convention divides registers for passing arguments, return address, return value, stack pointer
COMP3221 lec15-function-I.29 Saeid Nooshabadi
Review: Nested Procedures
° A caller function is itself called from another function.
° We need to store lr for the caller before it can call another function.
° In general, may need to save some other info in addition to lr.
° But; Where do we save these info?
COMP3221 lec15-function-I.30 Saeid Nooshabadi
Review: C memory allocation map
0
Address
Code Program
Static Variables declaredonce per program
HeapExplicitly created space, e.g., malloc(); C pointers
StackSpace for saved procedure informationsp
stackpointer
Static base sb
COMP3221 lec15-function-I.31 Saeid Nooshabadi
sp on entry Register Save Area
Local Variables
sp during
Arguments forCallees
Review: Typical Structure of a Stack Frame
° compiler calculates total frame size (F)
° at start of call, subtract F from SP
° at end, add F to SP
° where are args from caller?
° where are args for callee
Caller Frame
Callee Frame
COMP3221 lec15-function-I.32 Saeid Nooshabadi
Basic Structure of a Function
entry_label: sub sp,sp, #fsize ; create space on stack
str lr,[sp, #fsize-4]; save lr ; save other regs
...
;restore other regsldr lr, [sp,#fsize-4];restore lr add sp, sp, #fsize ;reclaim space on stack
mov pc, lr
Epilogue
Prologue
Bodylr
COMP3221 lec15-function-I.33 Saeid Nooshabadi
Compiling nested C func into ARMint sumSquare(int x, int y) {
return mult(x,x)+ y;}
sumSquare: sub sp,sp,#8 ; space on stack
str lr,[sp,#4] ; save ret addr str a2,[sp,#0] ; save y mov a2,a1 ; mult(x,x) bl mult ; call mult ldr a2,[sp,#0] ; restore y
add a1,a1,a2 ; mult()+y ldr lr,[sp,#4] ; get ret addr
add sp,sp,#8 ; => stack space mov pc, lr
C
Epilogue
Prologue
Body
ARM
COMP3221 lec15-function-I.34 Saeid Nooshabadi
Compiling nested C func into ARM (Better way)
int sumSquare(int x, int y) {return mult(x,x)+ y;
}
sumSquare:
str lr,[sp,#-4]!; save ret addr str a2,[sp,#-4]!; save y mov a2,a1 ; mult(x,x) bl mult ; call mult ldr a2,[sp,#4]! ; restore y
add a1,a1,a2 ; mult()+y ldr lr,[sp,#4]! ; get ret addr
; => stack space mov pc, lr
C
Epilogue
Prologue
Body
ARM
COMP3221 lec15-function-I.35 Saeid Nooshabadi
Function Call Bookkeeping: thus far
° Procedure address x
° Return address x lr
° Arguments x a1 – a4
° Return values x a1 – a4
° Local variables x v1 – v8
° Registers• what if they are reused?
• what if there aren’t enough?
COMP3221 lec15-function-I.36 Saeid Nooshabadi
Readings for Week #6° Steve Furber: ARM System On-Chip; 2nd Ed, Addison-
Wesley, 2000, ISBN: 0-201-67519-6. Chapters 6
° Experiment 3 Documentation
COMP3221 lec15-function-I.37 Saeid Nooshabadi
Exceeding limits of registers° Recall: assembly language has fixed
number of operands, HLL doesn’t
° Local variables: v1, ..., v8• What if more than 8 words of local variables?
° Arguments; a1, ..., a4• What if more than 4 words of arguments?
° Place extra variables and extra arguments onto stack (sp)
° Use scratch registers and data transfers to access these variables
COMP3221 lec15-function-I.38 Saeid Nooshabadi
Register Conflicts
° Procedure A calls Procedure B• A referred to as is “calling procedure” or “caller”
• B referred to as is “called procedure” or “callee”
° Both A and B want to use the 15 registers=> must cooperate
COMP3221 lec15-function-I.39 Saeid Nooshabadi
Register Conflicts: 2 options (A calls B)1) Called procedure/callee (B) leaves registers
the way it found them (except lr); its B’s job to save it before using it and then restore it: “callee saves”
• Since B only saves what it changes, more accurate is “callee saves (what it uses)”
° 2) B can use any register it wants; Calling procedure/caller A must save any register it wants to use after call of B: “caller saves”
• Since A knows what it needs after call, more accurate is “caller saves (if it wants to)”
° Is either optimal?
COMP3221 lec15-function-I.40 Saeid Nooshabadi
ARM Solution to Register Conflicts° Divide registers into groups
• Local variables / Callee Saved registers (v1 – v8)
• Scratch registers / Argument / Caller Save registers (a1 – a3)
• Some caller saved (if wants) and some callee saved (if used)
° Caller (A) save/restore scratch / argument (a1 – a4) if needs them after the call; also lr callee can use (a1 – a4) and lr
° Callee (B) must save/restore local variables / callee saved registers (v1 – v8) if it uses them caller can leave them unsaved
• Procedure that doesn’t call another tries to use only scratch / argument registers (a1 – a4)
COMP3221 lec15-function-I.41 Saeid Nooshabadi
Register Conventions (#1/5)
° Caller: the calling function
° Callee: the function being called
° When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged.
° Register Conventions: A set of generally accepted rules as to which registers will be unchanged after a procedure call (bl) and which may be changed.
COMP3221 lec15-function-I.42 Saeid Nooshabadi
Register Conventions (#2/5)
° Three views of registers a1 – a4
°a1 – a4 : Change. These are expected to contain new return values.
Or
°a1 – a4 : Change. These are volatile argument registers.
Or
°a1 – a4 : Change. They’re called scratch: any procedure may change them at any time.
COMP3221 lec15-function-I.43 Saeid Nooshabadi
Register Conventions (#3/5)
°v1 – v8 : No Change. Very important, that’s why they’re called callee saved registers / local variable. If the callee changes these in any way, it must restore the original values before returning.
°sp: No Change. The stack pointer must point to the same place before and after the bl call, or else the caller won’t be able to restore values from the stack.
• Grows downward, sp points to last full location
°lr: Change. The bl call itself will change this register.
°ip: Change. In most variants of APCS ip is a scratch register.
COMP3221 lec15-function-I.44 Saeid Nooshabadi
Register Conventions (#4/5)
° What do these conventions mean?• If function A calls function B, then function A
must save any scratch registers a1 – a4 that it may be using onto the stack before making a bl call.
• Remember: Caller needs to save only registers it is using, not all scratch registers.
• It also needs to store lr if A is in turn is called by another function.
COMP3221 lec15-function-I.45 Saeid Nooshabadi
Register Conventions (#5/5)
° Note that, even though the callee may not return with different values in the callee saved registers v1 – v8, it can use them by :
• save v1 – v8 on the stack
• use these eight registers
• restore v1 – v8 from the stack
° The difference is that, with the scratch registers a1 – a4, the callee doesn’t need to save them onto the stack.
COMP3221 lec15-function-I.46 Saeid Nooshabadi
sp on entry Register Save Area
Local Variables
sp during
Arguments forCallees
Recall: Typical Structure of a Stack Frame
° compiler calculates total frame size (F) for the caller
° at start of procedure, subtract F from SP
° at end, add F to SP
° where are args from caller?
° where are the args for callee?
Caller Frame
Callee Frame
COMP3221 lec15-function-I.47 Saeid Nooshabadi
Callees’ & Callers’ Rights (Summary)
° Callees’ Right• Right to use a1 – a4 registers freely• Right to assume args are passed correctly in
a1 – a4
° Callers’ Rights• Right to use v1 – v8 registers without fear of
being overwritten by Callee• Right to assume return values will be returned
correctly in a1 – a4
Keep this slide in mind for exam
COMP3221 lec15-function-I.48 Saeid Nooshabadi
Callee’s Responsibilities (Summary)1. If using v1 – v8 or big local structs, slide sp
down to reserve memory: e.g. sub sp, sp, #32
2. If using v1 – v8, save before using: e.g. str v1, [sp, #28]
3. Receive args in a1 – a4, additional args on stack
4. Run the procedure body
5. If not void, put return values in a1 – a4
6. If applicable, undo steps 2-1 e.g. ldr v1, [sp,#28] add sp, sp, #32
7. mov pc, lr
Keep this slide in mind for exam
COMP3221 lec15-function-I.49 Saeid Nooshabadi
Caller’s Responsibilities (Summary)1. Slide sp down to reserve memory: e.g.
sub sp, sp, #28
2. Save lr on stack before bl to callee clobbers it: e.g. str lr, [sp, #24]
3. If you still need their values after the function call, save a1 – a4 on stack or copy to v1 – v8 registers. Callee can overwrite “a” registers, but not “v” registers. e.g. str a1, [sp, #20]
4. Put first 4 words of args in a1 – a4, at most 1 arg per word, additional args go on stack: “arg 5” is [sp, #0]
5. bl to the desired function
6. Receive return values in a1 – a4
7. Undo steps 3-1: e.g. ldr a1, [sp, #20], ldr lr, [sp, #24], add sp, sp, #28
Keep this slide in mind for exam
COMP3221 lec15-function-I.50 Saeid Nooshabadi
Compile using pencil and paper Example 1 (#1/2)int Doh(int i, int j, int k, int l){
return i+j+l;
}
Doh: _______________
add a1, _________________ _________________
COMP3221 lec15-function-I.51 Saeid Nooshabadi
Compile using pencil and paper Example 1 (#2/2)
int Doh(int i, int j, int k, int l){
return i+j+l;
}
Doh: _______________
add a1, _______
_______________
add a1, a2, a1
a4, a1
mov pc lr
“a” RegsSafeFor
Callee
COMP3221 lec15-function-I.52 Saeid Nooshabadi
Compile using pencil and paper Example 2 (#1/2)int Doh(int i, int j, int k, int m, char c, int n){
return i+j+n;
}
Doh: _______________
add a1, _______ _______________ _______________
COMP3221 lec15-function-I.53 Saeid Nooshabadi
Compile using pencil and paper Example 2 (#2/2)int Doh(int i, int j, int k, int m, char c, int n){
return i+j+n;
}
Doh: _______________
add a1, ______
ldr ip,[sp,#4]
add a1, a1, ip
a1,a2
mov pc lr
6th argument
“a” & ip Regs
SafeFor
Callee
COMP3221 lec15-function-I.54 Saeid Nooshabadi
Hard Compilation Problem (#1/2)int Doh(int i, int j, int k, int m, char c, int n){return mult(i,j)+n;}
int mult(int m, int n);/* returns m n */
Doh: ______________
______________
______________
______________
bl ___________
add ___________
_______________
_______________
_______________
_______________
COMP3221 lec15-function-I.55 Saeid Nooshabadi
Slow-motion Replay (#1/2)int Doh(int i, int j, int k, int m, char c, int n){ return sum(i,j)+n;}
int mult(int m, int n);/* returns m n */
Doh: ______________
______________
______________
______________
bl ___________
add ___________
_______________
_______________
_______________
_______________
str lr, [sp]
sub sp, sp,#4
ldr lr, [sp]
add sp, sp, #4
mov pc, lrmult
ldr ,[sp,#8]v1
2.Saving lr => must move sp
1.Calling Sum => save lr, a Regs
3.Need n after funccall => v1
a1, a1, v1
COMP3221 lec15-function-I.56 Saeid Nooshabadi
Stack Memory Allocation Revisited° Caller frame
• Save caller-saved regs• Pass arguments (4 regs)•bl
° Callee frame• set fp@first word of frame
fp = Caller’s sp - 4 (fixed point)
• Save registers on stack and update sp as needed
• Accessing the local variables is always with reference to fp
• Return saved register from stack using fpCallee Saved
Registers
fp
low
high
Address
stackgrows
Local Variables
sp
...
Argument 5Argument 6
place for a1-a4
°GCC uses frame pointer, ARM compilers by default don’t
•(allocate extra temporary register (ip), less bookkeeping on procedure call)
COMP3221 lec15-function-I.67 Saeid Nooshabadi
Basic Structure of a Function
entry_label: sub sp,sp, #fsize ; create space on stack
str lr,[sp, #fsize-4]; save lr ; save other regs
...
;restore other regsldr lr, [sp,#fsize-4];restore lr add sp, sp, #fsize ;reclaim space on stack
mov pc, lr
Epilogue
Prologue
Bodylr
COMP3221 lec15-function-I.68 Saeid Nooshabadi
Example: Compile This (#1/5)main() {int i,j,k,m; /* i-m:v1–v4 */
i = mult(j,k); ... ;m = mult(i,i); ...
return 0
}
int mult (int mcand, int mlier){int product;
product = 0;while (mlier > 0) { product += mcand; mlier -= 1; }return product;
}
COMP3221 lec15-function-I.69 Saeid Nooshabadi
Example: Compile This (#2/5)
__start:
str lr, [sp,#-4]!; store return ; address
mov a1,v2 ; arg1 = jmov a2,v3 ; arg2 = k bl mult ; call multmov v1, a1 ; i = mult()...
mov a1, v1 ; arg1 = imov a2, v1 ; arg2 = i bl mult ; call multmov v4, a1 ; m = mult()...
ldr lr, [sp,#4]! ; restore return addressmov pc, lr
COMP3221 lec15-function-I.70 Saeid Nooshabadi
Example: Compile This (#3/5)
° Notes:•main function returns to O/S, so mov pc, lr, so there’s need to save lr onto stack
• all variables used in main function are callee saved registers (“v”), so there’s no need to save these onto stack
COMP3221 lec15-function-I.71 Saeid Nooshabadi
Example: Compile This (#4/5)
mult:mov a3, #0 ; prod=0
Loop:cmp a2,#0 ; mlier > 0?
beq Fin ; no=>Fin add a3,a3,a1 ; prod+=mcand sub a2,a2,#1 ; mlier-=1 b Loop ; goto Loop
Fin:mov a1,a3 ; a1=prod mov pc, lr ; return
COMP3221 lec15-function-I.72 Saeid Nooshabadi
Example: Compile This (#5/5)
° Notes:• no bl calls are made from mult and we don’t use
any callee saved (“v”) registers, so we don’t need to save anything onto stack
• Scratch registers a1 – a3 are used for intermediate calculations
•a2 is modified directly (instead of copying into a another scratch register) since we are free to change it
• result is put into a1 before returning
COMP3221 lec15-function-I.73 Saeid Nooshabadi
Fibonacci Rabbits
° Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on.
° How many pairs will there be in one year?Fibonacci’s Puzzle
Italian, mathematician Leonardo of Pisa (also known as Fibonacci) 1202.
COMP3221 lec15-function-I.74 Saeid Nooshabadi
Fibonacci Rabbits (Solution)
1. At the end of the first month, they mate, but there is still one only 1 pair.
2. At the end of the second month the female produces a new pair, so now there are 2 pairs of rabbits in the field.
3. At the end of the third month, the original female produces a second pair, making 3 pairs in all in the field.
4. At the end of the fourth month, the original female has produced yet another new pair, the female born two months ago produces her first pair also, making 5 pairs.
COMP3221 lec15-function-I.75 Saeid Nooshabadi
Fibonacci Rabbits (Solution Animated)
Month 0 1 2 3 4 5
Rabbit 0 1 1 2 3 5 8 Numbers
COMP3221 lec15-function-I.76 Saeid Nooshabadi
Fibonacci Rabbits (Solution in Picture)
The number of pairs of rabbits in the field at the start of each month is 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
COMP3221 lec15-function-I.77 Saeid Nooshabadi
Example: Fibonacci Numbers (#1/6)
° The Fibonacci numbers are defined as follows:
° F(n) = F(n – 1) + F(n – 2)
F(0) and F(1) are defined to be 1
° In C, this could be written:
int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2));
}
COMP3221 lec15-function-I.78 Saeid Nooshabadi
fib:
___________________
___________________
; Save the return address
; Save v1_& Push the
; stack frame___
str lr, [sp,#-4]!
str v1, [sp,#-4]!
° Now, let’s translate this to ARM!
° You will need space for three words on the stack
° The function will use v1
° Write the Prologue:
Example: Fibonacci Numbers (#2/6)
COMP3221 lec15-function-I.79 Saeid Nooshabadi
fin:
ldr v1, [sp,#4]!___
ldr lr, [sp,#4]!___
mov pc,lr__________
; Restore v1__________
; Restore return address
; Pop the stack frame___
; Return to caller_______
° Now write the Epilogue:
Example: Fibonacci Numbers (#3/6)
COMP3221 lec15-function-I.80 Saeid Nooshabadi
cmp a1, #0
cmpne a1, #1
moveq, a1, #1
beq fin_____
Continued on next slide. . .
° Finally, write the body. The C code is below. Start by translating the lines indicated in the comments
int fib(int n) { if(n == 0) { return 1; } /*Translate Me!*/ if(n == 1) { return 1; } /*Translate Me!*/ return (fib(n - 1) + fib(n - 2));
}; if (n == 0). . .
; if (n == 1). . .
; . . ._____________
; return 1__________
Example: Fibonacci Numbers (#4/6)
COMP3221 lec15-function-I.81 Saeid Nooshabadi
; Need a1 after bl
; a1 = n – 1_______
; fib(n – 1) ______
; Save return value
; Restore a1_______
; a1 = n – 2_______
str a1, [sp, #-4]!
sub a1, a1, #1_____
bl fib_____________
mov v1, a1_________
ldr a1, [sp, #4]!__
sub a1, a1, #2_____
Continued on next slide. . .
° Almost there, but be careful, this part is tricky!
int fib(int n) { . . .
return (fib(n - 1) + fib(n - 2));
}
Example: Fibonacci Numbers (#5/6)
COMP3221 lec15-function-I.82 Saeid Nooshabadi
bl fib__________
add a1, a1, v1__
;To the epilogue and beyond. . .
; fib(n-2)_______________
; a1 = fib(n-1) + fib(n-2)
° Remember that is v1 Callee Save and a1 is caller saved!
int fib(int n) { . . .
return (fib(n - 1) + fib(n - 2));
}
Example: Fibonacci Numbers (#6/6)
COMP3221 lec15-function-I.83 Saeid Nooshabadi
Stack Growth and Shrinkage
F(5)
splrv1a1
F(4)
splrv1a1
F(3)
spv1a1
lr
F(2)
spv1a1
lr
F(1)
sp v1lr
2F(1)
sp v1lr
+ 1
3
F(2)
spv1a1
lr
F(1)
sp v1lr
1 F(0)
sp v1lr
+ 1
+ 2
5
F(3)
spv1a1
lr
F(2)
spv1a1
lr
F(1)
sp v1lr
1 F(0)
sp v1lr
+ 11
F(0)
sp v1lr
+ 1
2F(1)
sp v1lr
+ 1
+ 3
8
sp
int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2);}
COMP3221 lec15-function-I.84 Saeid Nooshabadi
Instruction Support for Stack° Consider the following code:
str lr, [sp,#-4]!str fp, [sp,#-4]!str v3, [sp,#-4]!str v2, [sp,#-4]!str v1, [sp,#-4]!str a2, [sp,#-4]!str a1, [sp,#-4]!
Epilogue
Prologue
Body . . .
ldr a1, [sp,#4]! ldr a2, [sp,#4]! ldr v1, [sp,#4]! ldr v2, [sp,#4]!ldr v3, [sp,#4]!ldr fp, [sp,#4]!ldr lr, [sp,#4]!
stmfd sp!, {a1-a2,v1-v3,fp,lr}ldmfd sp!, {a1-a2,v1-v3,fp,lr}
Old SP
fp
v3
v2v1
a2SP
lr
a1
fp
v3
v2v1
a2
SPlr
a1
Store Multiple
Full Descending
Load Multiple
Full Descending
COMP3221 lec15-function-I.85 Saeid Nooshabadi
Block Copy via Stack Operation
° The contents of registers r0 to r6 need to be swapped around thus:• r0 moved into r3 • r1 moved into r4 • r2 moved into r6 • r3 moved into r5 • r4 moved into r0 • r5 moved into r1 • r6 moved into r2
° Write a segment of code that uses full descending stack operations to carry this out, and hence requires no use of any other registers for temporary storage.
COMP3221 lec15-function-I.86 Saeid Nooshabadi
Block Copy Sample Solution
ldmfd sp!,{r3,r4,r6}
r3 = r0r4 = r1r6 = r2
r5
r4sp
r6
r3
ldmfd sp!,{r5}
r5 = r3
r5sp
r6
r4
ldmfd sp!,{r0-r2}
r0 = r4r1 = r5r2 = r6
sp
stmfd sp!,{r0-r6}
Old sp
r5
r4
r3r2
r1sp
r6
r0
COMP3221 lec15-function-I.87 Saeid Nooshabadi
Direct functionality of Block Data Transfer
° When LDM / STM are not being used to implement stacks, it is clearer to specify exactly what functionality of the instruction is:
• i.e. specify whether to increment / decrement the base pointer, before or after the memory access.
° In order to do this, LDM / STM support a further syntax in addition to the stack one:
• STMIA / LDMIA : Increment After
• STMIB / LDMIB : Increment Before
• STMDA / LDMDA : Decrement After
• STMDB / LDMDB : Decrement Before
For details See Chapter 3, page 61 – 62 Steve Furber: ARM System On-Chip; 2nd Ed, Addison-Wesley, 2000, ISBN: 0-201-67519-6.
COMP3221 lec15-function-I.88 Saeid Nooshabadi
“And in Conclusion …’’
° ARM SW convention divides registers into those calling procedure save/restore and those called procedure save/restore• Assigns registers to arguments, return address, return value,
stack pointer
° Optional Frame pointer fp reduces bookkeeping on procedure call
° Use Stack Block copy Instructions stmfd & ldmfd to store and retrieve multiple registers to/from from stack.