Procedures Procedures are very important for writing reusable and maintainable code in assembly and high-level languages. How are they implemented? Application.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
Lecture 9: MIPS Instruction Set
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
UEE072HM Linking HLL and ALP An example on ARM. Embedded and Real-Time Systems We will mainly look at embedded systems –Systems which have the computer.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
CS 536 Spring Code generation I Lecture 20.
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Run time vs. Compile time
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Chapter 8 :: Subroutines and Control Abstraction
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Activation Records (in Tiger) CS 471 October 24, 2007.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Procedures Procedures are very important for writing reusable and maintainable code in assembly and high-level languages. How are they implemented? Application.
Lecture 3 Translation.
Assembly function call convention
Lecture 7 Macro Review Stack Frames
Writing Functions in Assembly
Computer Architecture Instruction Set Architecture
Computer Architecture Instruction Set Architecture
Procedures 101: There and Back Again
The University of Adelaide, School of Computer Science
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Writing Functions in Assembly
The HP OpenVMS Itanium® Calling Standard
Chapter 9 :: Subroutines and Control Abstraction
ARM Assembly Programming
Application Binary Interface (ABI)
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Where is all the knowledge we lost with information? T. S. Eliot
Runtime Stack Activation record for hanoi;
Presentation transcript:

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

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst 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.

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst PowerPC Features The PowerPC ISA provides the following features to support procedure/function calls: link register (p. 2-12) bl: branch and link (p. 4-41) blr: branch to link register (Table F-4)

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst 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

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan ErnstExample int func(int a, int b) { return (a + b); } int func2(int a, int b) { return func(b, a); } main { … func2(5,6); … }

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst The Stack Information for each function invocation (e.g. link register) is saved on the call stack or simply stack. Each function invocation has its own stack frame (a.k.a. activation record ). func2 func stack frame stack pointer high address low address

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst What Goes into Stack Frame? link register passing parameters (optional) return value (optional) what else? stack pointer

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst Using the Stack main… … bl func2 … 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

Application Binary Interface (ABI) Application Binary Interface (ABI): Defines everything needed for a binary object file to run on a system (CPU and operating system), so that it can call system library routines call (and be called by) code generated by other people and other compilers The ABI specifies: file format rules for linker operation procedure calling conventions register usage conventions PowerPC has different but very similar ABIs for MacOS, AIX, embedded systems (EABI), Unix, Windows.

PowerPC Conventions Stack pointer is r1 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

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst Register Usage Convention Rules about who gets to use/save which registers Caller-save: r0, r3-r12, LR, CTR, CR0, CR1, CR5-7 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, r13

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst 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 frame header highest address lowest address

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst 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-r31 Entry Code (prologue)

CS 478: Microcontroller Systems University of Wisconsin-Eau Claire Dan Ernst 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)