Procedures and Calling Conventions in PowerPC Assembly, Slides of Microcontrollers

The concept of procedures in assembly and high-level languages, focusing on powerpc architecture. It covers application binary interfaces, calling conventions, recursive calls, and the use of the stack. The document also provides examples and discusses the importance of agreement between caller and callee.

Typology: Slides

2012/2013

Uploaded on 04/24/2013

ballari
ballari 🇮🇳

4.6

(10)

117 documents

1 / 14

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
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Procedures and Calling Conventions in PowerPC Assembly and more Slides Microcontrollers in PDF only on Docsity!

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.

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

int func2(int a, int b) { return func(b , a); }

main { … func2(5,6); … }

What Goes into Stack Frame?

  • link register
  • passing parameters (optional)
  • return value (optional)
  • what else?

stack pointer

Using the Stack

main … … bl func … func2 … … 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

PowerPC Conventions

  • Stack pointer is r
  • Stack pointer is double-word aligned (8 bytes)
  • Stack “grows” down, toward address 0
  • r1 points to lowest stack address (bottom of current stack frame)
  • First item in stack frame (offset 0 from r1) is address of previous stack frame (a.k.a. frame pointer )
  • Second item (offset 4) is saved link register
  • Minimum stack frame is 8 bytes
  • Stack frame optional for leaf functions
  • Always use update addressing mode to allocate stack frame automatically

Register Usage Convention

Rules about who gets to use/save which registers

  • Caller-save: r0, r3-r12, LR, CTR, CR0, CR1, CR5-
  • Callee-save: r14-r31, CR2-4 (non-volatile registers whose values must be preserved across calls and must thus be restored by callee before returning to caller)

Dedicated: r1, r2, r

FuncX: mflr r0 ; Get Link register

stwu r1, -88(r1) ; Save Back chain and move SP stw r0, 92(r1) ; Save Link register stmw r28, 72(r1) ; Save 4 non-volatiles r28-r

Entry Code (prologue)

lwz r0, 92(r1) ; Get saved Link register mtlr r0 ; Restore Link register lmw r28, 72(r1) ; Restore non-volatiles addi r1, r1, 88 ; Remove frame from stack blr ; Return to calling function

Exit Code (epilogue)