Title: PowerPC Assembly Language
1PowerPC Assembly Language
- Zhao Zhang
- EABI Convention
2Function Call and Return
- Control flow transfer
- caller_func // caller address
-
- bl caller_func // call callee_func
-
- blr // return
- callee_func
-
- blr // return to caller
3Function Call and Return
- Control flow transfer
- LR Link register, saving the return address
- bl func_label Jump to func_label, saving the
return address in LR - blr Jump to the return address in LR
4Function Call and Return
- Many other issues
- Nested function call/return
- Parameter and result passing
- Register usage
- Stack usage and frame format
- PowerPC EABI Embedded Application Binary
Interface
5Parameters and Result Passing
- Register R3-R10 Parameters
- Register R3-R4 Results
- Why not use stack only?
- When to use stack?
6Parameters and Result Passing
- int max(int x, int y)
-
- int max
- if (x gt y)
- max x
- else
- max y
- return max
-
- int max3(int x, int y, int z)
-
- return max(max(x, y), z)
-
7Parameters and Result Passing
- .text
- reg usage r3 x, r4 y
- max
- cmpw r3, r4 x is r3, y is r4
- ble x_greater
- y_greater
- mr r3,r4 max is r3 y
- x_greater
- blr max is r3 x
8Parameters and Result Passing
- max3
- prologue (see later)
- reg usage x r3, y r4, z r5
- mr r31,r5 save z to r31 (nonvolatile)
- bl max call max
- mr r4,r31 now r3max(x,y), r4z
- bl max call max
- epilogue (see later)
9Stack Frame
- Stack top grows downwards
- Stack frame created during function prologue
- Stack frame Released during function epilogue
10Stack Frame
High-end address
- EABI Stack usage
- To save nonvolatile registers
- To store local variables
- To pass extra parameters and return values
- To store return address and old stack top
FPR Save Area (optional)
GPR Save Area (optional)
CR Save Area (optional)
Local Variables Area (optional)
Function parameters (optional)
Padding to 8-byte boundary (optional)
LR Save Word
Back Chain (SP Save) Word
Load-end address
11Stack Frame
EABI Register Usage Conventions
12Stack Frame
- What is the stack frame for this function body?
- prologue (see later)
- mr r31,r5 save z to r31 (nonvolatile)
- bl max call max
- mr r4,r31 now r3max(x,y), r4z
- bl max call max
- epilogue (see later)
13Stack Frame
the frame of max3s caller
4(rsp)
LR Save Word
Back Chain (SP Save) Word
12(rsp)
r31 save
old SP (rsp)
8(rsp)
Padding
max3 frame
4(rsp)
LR Save Word (not used)
0(rsp)
Back Chain (SP Save) Word
SP
Note A function uses its callers LR save word
to save the return address.
14Function Prologue/Epilogue
- Prologue for max3
- max3
- mflr r0 save LR to r0
- stw r0,4(rsp) then to stack
- stwu rsp,-16(rsp) create frame
- stw r31,12(rsp) save r31
- r31 will be used to hold z
15Function Prologue/Epilogue
- Prologue for max3
- max3
- mflr r0 save LR to r0
- stw r0,4(rsp) then to stack
- stwu rsp,-16(rsp) create frame
- stw r31,12(rsp) save r31
- r31 will be used to hold z
- function body
- epilogue
16Function Prologue/Epilogue
- Epilogue for max3
- max3
- prologue
- function body
- lwz r31,12(rsp) restore r31
- addi rsp,rsp,16 release frame
- lwz r0,4(rsp) get old LR value
- mtlr r0 move to LR
- blr return
17Function Prologue/Epilogue
- max3
- mflr r0 move LR to r0
- stw r0,4(rsp) save to current frame
- stwu rsp,-16(rsp) create a new frame
- stw r31,12(rsp) save r31
- mr r31,r5 put z into 31 (nonvolatile)
- bl max call max
- mr r4,r31 put z into r4 (second
parameter) - bl max call max
- lwz r31,12(rsp) restore old r31
- addi rsp,rsp,16 release the frame
- lwz r0,4(rsp) get the old LR value
- mtlr r0 move back to LR
- blr return
18Beyond Assembly
- Assembly programming gt understanding of
machine-level execution - Future uses
- Mixed C/assembly programming for embedded systems
- Computer organization and architecture
- Compiler code generation and optimization
- Operating Systems
- Security