Function calls.ppt

Embed Size (px)

Citation preview

  • 7/27/2019 Function calls.ppt

    1/27

    Assembly Program Layout

    Function Call Overview

    Parameter and Result Passing

    Function Variables

    1

  • 7/27/2019 Function calls.ppt

    2/27

    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;

    }

    2

  • 7/27/2019 Function calls.ppt

    3/27

    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

    3

    directive for text (code) segment

    label

  • 7/27/2019 Function calls.ppt

    4/27

    Hello World in Assemblybl LCD_Init ; call LCD_Initlis r3,mystring@ha ; load mystring upper halfaddi r3,r3,mystring@l ; load mystring lower halfbl LCD_PutString ; call LCD_PutString

    li r3,0 ; clear r3addi rsp,rsp,8 ; restore stack pointerlwz r0,4(rsp) ; load original LR value into r0mtlr r0 ; move r0 value into LRblr ; end of main

    .data

    mystring: .ascii "Hello World!\r"

    4

    label

    directive for data segment

  • 7/27/2019 Function calls.ppt

    5/27

    Passing Parameters and ResultsR0 Volatile Language SpecificR1 Dedicated Stack Pointer (SP)

    R2 Dedicated Read-only small data area anchor

    R3 -R4 Volatile Parameter Passing / return values

    R5 - R10 Volatile Parameter Passing

    R11 - R12 Volatile Read-write small data area anchorR13 Dedicated

    R14 R31 Nonvolatile Language specific

    F0 Volatile Parameter passing/return values

    F1 Volatile Parameter passing

    F2 F8 Volatile

    F9 F13 Volatile

    F14 F31 Nonvolatile

    Fields CR2 CR4 Nonvolatile

    Other CR Fields Volatile

    LR, XER, CTR,

    FPSCRVolatile

    5

    EABI Register Usage Conventions

    EABI: Embedded Application Binary Interface

  • 7/27/2019 Function calls.ppt

    6/27

    Passing Parameters and Resultint max(int x, int y){

    int max;

    if(x > y)max = x;

    elsemax = y;

    return max;}

    intmax3(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_greaterb 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

    6

    What registers are used for x and y?

    Where is the returning result?

  • 7/27/2019 Function calls.ppt

    7/27

    Passing Parameters and Result(continuing)max3:

    mflr r0 ; discuss laterstw r0,4(rsp) ;stwu rsp,-16(rsp) ;

    stw r31,12(rsp) ;mr r31,r5 ; save z to r31; r31 is nonvolatilebl max ; call maxmr r4,r31 ; now r3=max(x,y), r4=zbl max ; call maxlwz r31,12(rsp) ; discuss later

    addi rsp,rsp,16 ;lwz r0,4(rsp) ;mtlr r0; ;blr ; return; now r3 stores max(x, y, z)

    7

  • 7/27/2019 Function calls.ppt

    8/27

    Nested Function Callsmax3:mflr r0 ; save LR to r0stw r0,4(rsp) ; now LR saved into stackstwu rsp,-16(rsp) ; create 16 bytes in stack AND save old SP

    stw r31,12(rsp) ; save nonvolatile r31 into stackmr r31,r5 ;bl max ; call maxmr r4,r31 ;bl max ; call max

    lwz r31,12(rsp) ; restore r31 valueaddi rsp,rsp,16 ; release 16 byte from stack AND restore SPlwz r0,4(rsp) ; restore old LR valuemtlr r0 ; move to LRblr ; return

    8

  • 7/27/2019 Function calls.ppt

    9/27

    Nested Function Callsint max(int x, int y)

    Which areas are saved instack?

    int max3(int x, int y, int z)

    Which areas are saved instack?

    Exercise: Give the layout ofmax3 stack usage

    9

    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

    EABI stack usageHigh-end address

    Load-end address

  • 7/27/2019 Function calls.ppt

    10/27

    Function Exercisesvoid add_array (int X[], int Y[], int N)

    {

    int i;for (i = 0; i < N; i++)

    X[i] += Y[i];

    }

    10

    add_array.c

    Write add_array.asm to replace this program

  • 7/27/2019 Function calls.ppt

    11/27

    Function Call and ReturnControl flow transfercaller_func: // caller address

    bl caller_func // call callee_funcblr // return

    callee_func:blr // return to caller

    11

  • 7/27/2019 Function calls.ppt

    12/27

    Function Call and ReturnControl flow transfer

    LR Link register, saving the return address

    bl func_label Jump to func_label, saving the returnaddress in LR

    blr Jump to the return address in LR

    12

  • 7/27/2019 Function calls.ppt

    13/27

    Function Call and ReturnMany other issues:

    Nested function call/return

    Parameter and result passing Register usage

    Stack usage and frame format

    PowerPC EABI

    Embedded Application BinaryInterface

    13

  • 7/27/2019 Function calls.ppt

    14/27

    Parameters and Result PassingRegister R3-R10: Parameters

    Register R3-R4: Results

    Why not use stack only?

    When to use stack?

    14

  • 7/27/2019 Function calls.ppt

    15/27

    Parameters and Result Passingint max(int x, int y){int max;

    if (x > y)max = x;

    elsemax = y;

    return max;}

    int max3(int x, int y, int z){

    return max(max(x, y), z);} 15

  • 7/27/2019 Function calls.ppt

    16/27

    Parameters and Result Passing.text; reg usage: r3 x, r4 ymax:

    cmpw r3, r4 ; x is r3, y is r4ble x_greatery_greater:mr r3,r4 ; max is r3 = yx_greater:blr ; max is r3 = x

    16

  • 7/27/2019 Function calls.ppt

    17/27

    Parameters and Result Passingmax3:; 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)

    17

  • 7/27/2019 Function calls.ppt

    18/27

    Stack Frame

    Stack top grows downwards Stack frame created during function prologue

    Stack frame Released during function epilogue

    18

  • 7/27/2019 Function calls.ppt

    19/27

    Stack Frame EABI Stack usage To save nonvolatile

    registers

    To store local variables

    To pass extra parametersand return values

    To store return addressand old stack top

    19

    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

    High-end address

    Load-end address

    Function parameters

    (optional)

  • 7/27/2019 Function calls.ppt

    20/27

    Stack FrameR0 Volatile Language Specific

    R1 Dedicated Stack Pointer (SP)

    R2 Dedicated Read-only small data area anchor

    R3 -R4 Volatile Parameter Passing / return values

    R5 - R10 Volatile Parameter Passing

    R11 - R12 Volatile Read-write small data area anchor

    R13 Dedicated

    R14 R31 Nonvolatile Language specificF0 Volatile Parameter passing/return values

    F1 Volatile Parameter passing

    F2 F8 Volatile

    F9 F13 Volatile

    F14 F31 Nonvolatile

    Fields CR2 CR4 NonvolatileOther CR Fields Volatile

    LR, XER, CTR, FPSCR Volatile

    20

    EABI Register Usage Conventions

  • 7/27/2019 Function calls.ppt

    21/27

    Stack FrameWhat 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)

    21

  • 7/27/2019 Function calls.ppt

    22/27

    Stack FrameStack Frame of Max3

    22

    r31 save

    LR Save Word (not used)

    Back Chain (SP Save) Word

    SP

    0(rsp)

    4(rsp)

    12(rsp)

    Padding8(rsp)

    LR Save Word

    Back Chain (SP Save) Word

    old SP (rsp)

    max3 frame

    the frame of

    max3s caller

    Note: A function uses its callers LR save word to

    save the return address.

    4(rsp)

  • 7/27/2019 Function calls.ppt

    23/27

    Function Prologue/EpiloguePrologue for max3

    max3:

    mflr r0 ; save LR to r0stw 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

    23

  • 7/27/2019 Function calls.ppt

    24/27

    Function Prologue/EpiloguePrologue for max3

    max3:

    mflr r0 ; save LR to r0stw 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

    24

  • 7/27/2019 Function calls.ppt

    25/27

    Function Prologue/EpilogueEpilogue 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 valuemtlr r0 ; move to LR

    blr ; return

    25

  • 7/27/2019 Function calls.ppt

    26/27

    Function Prologue/Epiloguemax3:mflr r0 ; move LR to r0stw r0,4(rsp) ; save to current framestwu rsp,-16(rsp) ; create a new framestw r31,12(rsp) ; save r31mr r31,r5 ; put z into 31 (nonvolatile)

    bl max ; call maxmr r4,r31 ; put z into r4 (second parameter)bl max ; call maxlwz r31,12(rsp) ; restore old r31addi rsp,rsp,16 ; release the frame

    lwz r0,4(rsp) ; get the old LR valuemtlr r0 ; move back to LRblr ; return

    26

  • 7/27/2019 Function calls.ppt

    27/27

    Beyond AssemblyAssembly programming => 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

    27