PowerPC Assembly Language - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

PowerPC Assembly Language

Description:

Stack frame Released during function epilogue. 10. Stack Frame. EABI Stack usage ... Function Prologue/Epilogue. Prologue for max3. max3: mflr r0 ; save LR to r0 ... – PowerPoint PPT presentation

Number of Views:204
Avg rating:3.0/5.0
Slides: 19
Provided by: dianet4
Category:

less

Transcript and Presenter's Notes

Title: PowerPC Assembly Language


1
PowerPC Assembly Language
  • Zhao Zhang
  • EABI Convention

2
Function Call and Return
  • Control flow transfer
  • caller_func // caller address
  • bl caller_func // call callee_func
  • blr // return
  • callee_func
  • blr // return to caller

3
Function 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

4
Function 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

5
Parameters and Result Passing
  • Register R3-R10 Parameters
  • Register R3-R4 Results
  • Why not use stack only?
  • When to use stack?

6
Parameters 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)

7
Parameters 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

8
Parameters 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)

9
Stack Frame
  • Stack top grows downwards
  • Stack frame created during function prologue
  • Stack frame Released during function epilogue

10
Stack 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
11
Stack Frame
EABI Register Usage Conventions
12
Stack 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)

13
Stack Frame
  • Stack Frame of Max3

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.
14
Function 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

15
Function 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

16
Function 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

17
Function 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

18
Beyond 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
Write a Comment
User Comments (0)
About PowerShow.com