Sei sulla pagina 1di 18

1

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
2

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

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

Parameters and Result Passing


Register R3-R10: Parameters Register R3-R4: Results
Why not use stack only? When to use stack?

Parameters and Result Passing


int max(int x, int y) { int max; if (x > y) max = x; else max = y; return max; }

int max3(int x, int y, int z) { return max(max(x, y), z); }


6

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

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 r3=max(x,y), r4=z bl max ; call max ; epilogue (see later)

Stack Frame

Stack top grows downwards


Stack frame created during function prologue Stack frame Released during function epilogue

Stack Frame
High-end address 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

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

10

Stack Frame
R0 R1 R2 R3 -R4 R5 - R10 R11 - R12 R13 R14 R31 F0 F1 F2 F8 F9 F13 F14 F31 Fields CR2 CR4 Other CR Fields LR, XER, CTR, FPSCR

Volatile Dedicated Dedicated Volatile Volatile Volatile Dedicated Nonvolatile Volatile Volatile Volatile Volatile Nonvolatile Nonvolatile Volatile Volatile

Language Specific Stack Pointer (SP) Read-only small data area anchor Parameter Passing / return values Parameter Passing Read-write small data area anchor Language specific Parameter passing/return values Parameter passing

EABI Register Usage Conventions


11

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 r3=max(x,y), r4=z bl max ; call max ; epilogue (see later)

12

Stack Frame
Stack Frame of Max3
the frame of max3s caller 4(rsp) 12(rsp) 8(rsp) 4(rsp) 0(rsp) LR Save Word Back Chain (SP Save) Word r31 save Padding LR Save Word (not used) Back Chain (SP Save) Word

old SP (rsp) max3 frame

SP Note: A function uses its callers LR save word to save the return address.
13

Function Prologue/Epilogue
Prologue for max3
max3: mflr r0 stw r0,4(rsp) stwu rsp,-16(rsp) stw r31,12(rsp) ; r31 will be used ; save LR to r0 ; then to stack ; create frame ; save r31 to hold z

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 ; function body ; epilogue
15

Function Prologue/Epilogue
Epilogue for max3
max3: ; prologue ; function body lwz r31,12(rsp) addi rsp,rsp,16 lwz r0,4(rsp) mtlr r0 blr

; ; ; ; ;

restore release get old move to return

r31 frame LR value LR

16

max3: Function Prologue/Epilogue mflr r0 ; move LR to r0

stw stwu stw mr bl mr bl lwz addi lwz mtlr blr

r0,4(rsp) rsp,-16(rsp) r31,12(rsp) r31,r5 max r4,r31 max r31,12(rsp) rsp,rsp,16 r0,4(rsp) r0

; save to current frame ; create a new frame ; save r31 ; put z into 31 (nonvolatile) ; call max ; put z into r4 (second parameter) ; call max ; restore old r31 ; release the frame ; get the old LR value ; move back to LR ; return

17

Beyond Assembly
Assembly programming => understanding of machinelevel execution Future uses
Mixed C/assembly programming for embedded systems Computer organization and architecture Compiler code generation and optimization Operating Systems

Security

18

Potrebbero piacerti anche