Download Procedures, Lecture Slide - Computer Science and more Slides Introduction to Computers in PDF only on Docsity!
Machine-Level Programming III:
Procedures
Sept. 15, 1998
Topics
- Stack-based languages
- Stack frames
- Register saving conventions
- Optimizing to avoid stack frame
- Creating pointers to local variables
class07.ppt
“The course that gives CMU its Zip!”
Procedure Control Flow
Maintain Return Address in Designated Register ( $26 )
Procedure call:
- bsr $26, label Save return addr in $26, branch to label
- jsr $26, (Ra) Save return addr in $26, jump to address in Ra
Return Address Value
- Address of calling instruction + 4 0x100: bsr $26, label 0x104: addq $0, $16, $0 # Following instruction
- Example bsr will set $26 to 0x
Procedure return:
- ret $31, ($26) Jump to address in $
Stack-Based Languages
Languages that Support Recursion
- e.g., C, Pascal, Java
- Code must be “ Reentrant ”
- Multiple simultaneous instantiations of single procedure
- Need some place to store state of each instantiation
- Arguments
- Local variables
- Return pointer
Stack Discipline
- State for given procedure needed for limited time
- From when called to when return
- Callee returns before caller does
Stack Allocated in Frames
- state for single procedure instantiation
Call Chain Example
Code Structure
yoo(…) {
who();
}
who(…) {
amI();
}
amI(…) {
amI();
}
yoo
who
amI
amI
amI
Call Chain
Alpha Stack Frame
Stack Frame (“Top” to
Bottom)
- Parameters to called functions
- Only if too many for registers
- Saved register context
- Storage for local variables
Size Requirements
- Saved state area must be multiple of 16 bytes
- Local area must be multiple of 16 bytes
Stack Pointer ( $sp )
(Virtual) Frame Pointer ( $fp ) Locals
Saved Registers
Argument Build
Caller Frame
Alpha Register Convention
General Purpose Registers
- 32 total
- Store integers and pointers
- Fast access: 2 reads, 1 write in single cycle
Usage Conventions
- Established as part of architecture
- Used by all compilers, programs, and libraries
v t t t t t t t t s s s s s s s6,fp
Return value from integer functions
Temporaries (not preserved across procedure calls)
Callee saved
Frame pointer, or callee saved
Register Saving Conventions
When procedure yoo calls who :
- yoo is the caller , who is the callee
Can Register be Used for Temporary Storage?
- Contents of registers $1 and $26 overwritten by who
Conventions
- “Caller Save”
- Caller saves temporary in its frame before calling
- “Callee Save”
- Callee saves temporary in its frame before using
yoo: bis $31, 17, $ bsr $26, who addq $1, $0, $ ret $31, ($26)
who: bis $31, 255, $ s4addq $1, $1, $ ret $31, ($26)
Caller Save Version
“Caller Save” Temporary
Registers:
- not guaranteed to be preserved across procedure calls
- can be immediately overwritten by a procedure without first saving
- if yoo wants to preserve a caller-save register across a call to who : - save it on the stack before calling who - restore after who returns
yoo:
- • • # make frame bis $31, 17, $
save $26, $
stq $26, 0($sp) stq $1, 8($sp) bsr $26, who
restore $26, $
ldq $1, 8($sp) ldq $26, 0($sp) addq $1, $0, $
- • • # remove frame ret $31, ($26)
who: bis $31, 255, $ s4addq $1, $1, $ ret $31, ($26)
Callee Save Version
Callee Save
- Must be preserved across procedure calls
- If who wants to use a callee- save register - Save it on stack upon procedure entry - Restore when returning
Stack Frame for
Procedure who
- 16 bytes total
- To meet size requirements
who:
make frame
lda $sp, -16($sp)
save $
stq $9, 0($sp) bis $31, 255, $ s4addq $9, $9, $
restore $
ldq $9, 0($sp)
remove frame
addq $sp, 16, $sp ret $31, ($26)
Unused $30^ Saved^ $
Callee Save Version (Cont.)
Stack Frame for
Procedure yoo
- 16 bytes total
- Must save $
- Must save $
- Will get overwritten by bsr instruction
yoo:
make frame
lda $sp, 16($sp)
save $9, $
stq $26, 0($sp) stq $9, 8($sp) bis $31, 17, $ bsr $26, who addq $9, $0, $
restore $9, $
ldq $26, 0($sp) ldq $9, 8($sp)
remove frame
addq $sp, 16($sp) ret $31, ($26)
Saved $ $30 Saved^ $
Procedure Categories
Leaf procedures that do not use stack
- Do not call other procedures
- Can fit all temporaries in caller-save registers
Leaf procedures that use stack
- Do not call other procedures
- Need stack for temporaries
Non-leaf procedures
- Must use stack
- at the very least, to save the return address $
- Also when need to store array
long int rfact (long int x) { long int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; }
.align 3 .globl rfact .ent rfact rfact: ldgp $29,0($27) rfact..ng: lda $30,-16($30) .frame $30,16,$26, stq $26,0($30) stq $9,8($30) .mask 0x4000200,- .prologue 1 bis $16,$16,$ cmple $9,1,$ bne $1,$ subq $9,1,$ bsr $26,rfact..ng mulq $0,$9,$ br $31,$ .align 4 $34: bis $31,1,$ $35: ldq $26,0($30) ldq $9,8($30) addq $30,16,$ ret $31,($26), .end rfact
Recursive Factorial
Complete Assembly
- Assembler directives
- Lines beginning with “.”
- Not of concern to us
- Labels
- Actual instructions
Rfact Stack
Stack Frame
- Return pointer $
- Callee save
Global Pointer
- Set on first entry to rfact
- Recursive calls don’t need to change - Enter at rfact..ng
rfact: ldgp $29,0($27) rfact..ng: lda $30,-16($30) stq $26,0($30) stq $9,8($30)
ldq $26,0($30) ldq $9,8($30) addq $30,16,$ ret $31,($26),
Saved $ $30 Saved^ $
Prologue: Set Up Frame
Epilogue: Undo Frame
Rfact Body
Registers
- rval from recursive call
- Return value from this call $
- Initially argument x
- Must set to x-1 when making call $
- Hold copy of x
- To use after recursive call returns
- • • # Prologue bis $16,$16,$9 # $9 = x cmple $9,1,$1 # if (x <= 1) bne $1,$34 # then goto R subq $9,1,$16 # $16 = x- bsr $26,rfact..ng # rfact(x-1) mulq $0,$9,$0 # rval * x br $31,$35 # goto Epilogue $34: # R1: bis $31,1,$0 # Return 1 $35: # Epilogue:
long int rfact (long int x) { long int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; }