Function Calls and EABI - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Function Calls and EABI

Description:

Stack frame created during function prologue. Stack frame Released during function epilogue ... prologue (see later) mr r31,r5 ; save z to r31 (nonvolatile) bl ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 28
Provided by: dianet4
Category:

less

Transcript and Presenter's Notes

Title: Function Calls and EABI


1
Function Calls and EABI
  • Assembly Program Layout
  • Function Call Overview
  • Parameter and Result Passing
  • Function Variables

2
Recall Hello World Program
  • include "QTerm.h" // Accessing the QTerm-J10
    Terminal
  • include "serial.h" // Accessing the serial ports
  • include "PPC_Support.h" // msleep
    miscellaneous functions
  • main ()
  • LCD_Init()
  • LCD_PutString("Hello World!\r")
  • return 0

3
Hello World in Assembly
  • .export main Tell the linker other files may
    need to
  • see this function
  • .extern LCD_Init Tell the linker to import
    functions
  • .extern LCD_PutString in other files
  • .text
  • main main function code
  • mflr r0 move LR into r0
  • stw r0,4(rsp) store LR into stack
  • stwu rsp,-8(rsp) create 8-byte stack space

directive for text (code) segment
label
4
Hello World in Assembly
  • bl LCD_Init call LCD_Init
  • lis r3,mystring_at_ha load mystring upper half
  • addi r3,r3,mystring_at_l load mystring lower
    half
  • bl LCD_PutString call LCD_PutString
  • li r3,0 clear r3
  • addi rsp,rsp,8 restore stack pointer
  • lwz r0,4(rsp) load original LR value into r0
  • mtlr r0 move r0 value into LR
  • blr end of main
  • .data
  • mystring .ascii "Hello World!\r"

directive for data segment
label
5
Passing Parameters and Results
EABI Register Usage ConventionsEABI Embedded
Application Binary Interface
6
Passing Parameters and Result
  • 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)
  • .text
  • max
  • cmpw r3, r4 x is r3, y is r4
  • ble y_greater
  • b x_greater
  • y_greater
  • mr r3,r4 max is r3 y
  • x_greater
  • blr max is r3 x
  • mr move register (pseudo)
  • mr r3, r4 ? addi r3, r4, 0

What registers are used for x and y? Where is the
returning result?
7
Passing Parameters and Result
  • (continuing)
  • max3
  • mflr r0 discuss later
  • stw r0,4(rsp)
  • stwu rsp,-16(rsp)
  • stw r31,12(rsp)
  • mr r31,r5 save z to r31 r31 is
    nonvolatile
  • bl max call max
  • mr r4,r31 now r3max(x,y), r4z
  • bl max call max
  • lwz r31,12(rsp) discuss later
  • addi rsp,rsp,16
  • lwz r0,4(rsp)
  • mtlr r0
  • blr return now r3 stores max(x, y, z)

8
Nested Function Calls
  • max3
  • mflr r0 save LR to r0
  • stw r0,4(rsp) now LR saved into stack
  • stwu rsp,-16(rsp) create 16 bytes in stack
    AND save old SP
  • stw r31,12(rsp) save nonvolatile r31 into
    stack
  • mr r31,r5
  • bl max call max
  • mr r4,r31
  • bl max call max
  • lwz r31,12(rsp) restore r31 value
  • addi rsp,rsp,16 release 16 byte from stack
    AND restore SP
  • lwz r0,4(rsp) restore old LR value
  • mtlr r0 move to LR
  • blr return

9
Nested Function Calls
EABI stack usage
  • int max(int x, int y)
  • Which areas are saved in stack?
  • int max3(int x, int y, int z)
  • Which areas are saved in stack?
  • Exercise Give the layout of max3 stack usage

High-end address
FPR Save Area (optional)
GPR Save Area (optional)
CR Save Area (optional)
Local Variables Area (optional)
Padding to 8-byte boundary (optional)
LR Save Word
Back Chain (SP Save) Word
Load-end address
10
Function Exercises
add_array.c
  • void add_array (int X, int Y, int N)
  • int i
  • for (i 0 i lt N i)
  • Xi Yi

Write add_array.asm to replace this program
11
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

12
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

13
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

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

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

16
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

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

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

19
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
20
Stack Frame
EABI Register Usage Conventions
21
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)

22
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.
23
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

24
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

25
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

26
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

27
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