Anda di halaman 1dari 27

Assembly Program Layout Function Call Overview Parameter and Result Passing Function Variables

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

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
directive for text (code) segment

.text
label

main:

mflr stw stwu

r0 r0,4(rsp) rsp,-8(rsp)

; ; ; ;

main function code move LR into r0 store LR into stack create 8-byte stack space
3

Hello World in Assembly bl LCD_Init ; call LCD_Init


lis addi bl li addi lwz mtlr blr r3,mystring@ha r3,r3,mystring@l LCD_PutString r3,0 rsp,rsp,8 r0,4(rsp) r0 ; ; ; ; ; ; ; ;

load mystring upper half load mystring lower half call LCD_PutString clear r3 restore stack pointer load original LR value into r0 move r0 value into LR end of main

.data

directive for data segment label

mystring:

.ascii "Hello World!\r"

Passing Parameters and Results


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 EABI: Embedded Application Binary Interface

Passing Parameters and Result


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

.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

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

What registers are used for x and y? Where is the returning result?
6

Passing Parameters and Result


(continuing) max3: mflr r0 stw r0,4(rsp) ; stwu rsp,-16(rsp) stw r31,12(rsp) mr r31,r5 bl max mr r4,r31 bl max lwz r31,12(rsp) addi rsp,rsp,16 lwz r0,4(rsp) ; mtlr r0; blr ; discuss later ; ; ; save z to r31; r31 is nonvolatile ; call max ; now r3=max(x,y), r4=z ; call max ; discuss later ; ; ; return; now r3 stores max(x, y, z)

Nested Function Calls


max3: mflr stw stwu stw mr bl mr bl lwz addi lwz mtlr blr r0 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 LR to r0 ; now LR saved into stack ; create 16 bytes in stack AND save old SP ; save nonvolatile r31 into stack ; ; call max ; ; call max ; restore r31 value ; release 16 byte from stack AND restore SP ; restore old LR value ; move to LR ; return
8

EABI stack usage

Nested Function Calls


High-end address int max(int x, Area int y) FPR Save Which (optional) areas are saved in GPR Save Area stack? (optional) CR Save Area (optional) int max3(int x, int y, int z) Local Variables Area Which (optional) areas are saved in stack? Padding to 8-byte boundary (optional) LR Save Word Exercise: Give the layout Back Chain (SP Save) Word of max3 stack Load-end usage address
9

Function Exercises add_array.c


void add_array (int X[], int Y[], int N) { int i; for (i = 0; i < N; i++) X[i] += Y[i]; } Write add_array.asm to replace this program

10

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
11

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

12

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

13

Parameters and Result Passing


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

14

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); }

15

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

16

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)

17

Stack Frame

Stack top grows downwards


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

18

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

19

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


20

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)

21

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

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

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

24

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

25

Function Prologue/Epilogue
max3: mflr stw stwu stw mr bl mr bl lwz addi lwz mtlr blr r0 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 ; move LR to 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

26

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

27

Anda mungkin juga menyukai