.text
label
main:
r0 r0,4(rsp) rsp,-8(rsp)
; ; ; ;
main function code move LR into r0 store LR into stack create 8-byte stack space
3
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
mystring:
.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?
6
10
// return to caller
11
12
13
14
}
int max3(int x, int y, int z) { return max(max(x, y), z); }
15
16
17
Stack Frame
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
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
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
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
27