Procedures and Calling Conventions in PowerPC Assembly, Study notes of Electrical and Electronics Engineering

The concept of procedures in assembly and high-level languages, focusing on powerpc architecture. It covers application binary interfaces, calling conventions, recursive calls, and examples. The document also discusses the powerpc isa features for procedure calls and the use of the stack.

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-fo9-1
koofers-user-fo9-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Procedures
Procedures are very important for writing
reusable and maintainable code in assembly
and high-level languages. How are they
implemented?
Application Binary Interfaces
Calling Conventions
Recursive Calls
Examples
Reference: PowerPC Embedded ABI
General Concepts
Caller: The calling procedure
Callee: The procedure called by the caller
int mult(x, y)
prod = mult (a, b)
return (x * y)
Caller and callee must agree on:
How to pass parameters
How to return the return value(s), if any
How to maintain relevant information across calls
PowerPC architecture does not define “agreement”. Instead,
common policies are defined by convention.
PowerPC Features
The PowerPC ISA provides the following features to support
procedure/function calls:
link register (p. 2-11)
bl: branch and link (p. 4-41)
blr: branch to link register (Table F-4)
A Very Simple Calling Convention
Passing arguments
Use GPRs r3 to r10 in order
Use stack in main memory if more than 8 arguments
Passing return value
Leave result in r3
pf3
pf4

Partial preview of the text

Download Procedures and Calling Conventions in PowerPC Assembly and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Procedures

Procedures are very important for writingreusable and maintainable code in assemblyand high-level languages. How are theyimplemented?• Application Binary Interfaces• Calling Conventions• Recursive Calls• ExamplesReference: PowerPC Embedded ABI

General Concepts

  • Caller: The calling procedureCallee: The procedure called by the caller…

int mult(x, y)

prod = mult (a, b)

return (x * y)

  • Caller and callee must agree on:
    • How to pass parameters• How to return the return value(s), if any• How to maintain relevant information across calls
      • PowerPC architecture does not define “agreement”. Instead,common policies are defined by convention.

PowerPC Features

The PowerPC ISA provides the following features to supportprocedure/function calls:• link register (p. 2-11)• bl: branch and link (p. 4-41)• blr: branch to link register (Table F-4)

A Very Simple Calling Convention

  • Passing arguments
    • Use GPRs r3 to r10 in order• Use stack in main memory if more than 8 arguments
      • Passing return value
        • Leave result in r

Example

int func(int a, int b){

return (a + b); } main{

…func(5,6);… }

Another Example

int func2(int a, int b){

return func(a , b); } main{

…func2(5,6);… }

The Stack

  • Information for each function invocation (e.g. link register) is savedon the

call stack

or simply

stack

  • Each function invocation has its own

stack frame

(a.k.a.

activation

record

func2func

stack framestack frame

stackpointer high address low address

Using the Stack

main

……bl

func …

func

……bl

func …??…

func

Describe the stack and LR contents• right before the first bl• right after the first bl• right after the second bl• after each procedure returns

EABI Stack Frame^ FPRs (optional, var size)GPRs (optional, var size)

CR (optional, 1 word)

local variables (optional, var size) function param’s (optional, var size) Padding (make size mult of 8 bytes)

Link Register^ Back Chain

frameheader

highestaddress lowestaddress