Title: Function Calls and EABI
1Function Calls and EABI
- Assembly Program Layout
- Function Call Overview
- Parameter and Result Passing
- Function Variables
2Recall 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
3Hello 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
4Hello 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
5Passing Parameters and Results
EABI Register Usage ConventionsEABI Embedded
Application Binary Interface
6Passing 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?
7Passing 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)
8Nested 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
9Nested 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
10Function 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
11Beyond 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