Functions and Procedures

Similar documents
Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Storage in Programs. largest. address. address

Functions in MIPS. Functions in MIPS 1

CS64 Week 5 Lecture 1. Kyle Dewey

CSE Lecture In Class Example Handout

Instruction Set Architectures (4)

CA Compiler Construction

The Activation Record (AR)

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Lecture 5. Announcements: Today: Finish up functions in MIPS

Today. Putting it all together

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

ECE232: Hardware Organization and Design

Review of Activation Frames. FP of caller Y X Return value A B C

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

MIPS Procedure Calls. Lecture 6 CS301

LECTURE 19. Subroutines and Parameter Passing

ECE260: Fundamentals of Computer Engineering

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Run-time Environment

Implementing Subroutines. Outline [1]

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

COMP2611: Computer Organization MIPS function and recursion

Course Administration

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

ECE331: Hardware Organization and Design

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

CS153: Compilers Lecture 8: Compiling Calls

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

EE 361 University of Hawaii Fall

Implementing Procedure Calls

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

CSE Lecture In Class Example Handout

CS 61c: Great Ideas in Computer Architecture

Runtime Environments I. Basilio B. Fraguela

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Lab 4 Prelab: MIPS Function Calls

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Lecture 5: Procedure Calls

MIPS Functions and the Runtime Stack

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Instruction Set Architecture

MIPS%Assembly% E155%

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Lecture 5: Procedure Calls

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

Topic 3-a. Calling Convention 2/29/2008 1

Run-time Environments

Run-time Environments

Common Problems on Homework

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

CSC258: Computer Organization. Functions and the Compiler Tool Chain

Procedures and Stacks

Run-Time Environments

CSCE 5610: Computer Architecture

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Concepts Introduced in Chapter 7

Lecture 7: Procedures and Program Execution Preview

System Software Assignment 1 Runtime Support for Procedures

Programming Languages

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

CENG3420 Lecture 03 Review

Chapter 2A Instructions: Language of the Computer

Topic 7: Activation Records

CS61C : Machine Structures

Run-Time Data Structures

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Midterm II CS164, Spring 2006

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Compiling Code, Procedures and Stacks

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

6.004 Tutorial Problems L3 Procedures and Stacks

ECE 30 Introduction to Computer Engineering

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Procedure Call. Procedure Call CS 217. Involves following actions

Architecture II. Computer Systems Laboratory Sungkyunkwan University

MIPS Functions and Instruction Formats

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro

Transcription:

Functions and Procedures

Function or Procedure A separate piece of code Possibly separately compiled Located at some address in the memory used for code, away from main and other functions (main is itself a function called by the system) May return a value to caller Function may use provided by caller

Function call Store where function can find them Store address where control should return after call Go to function code Store information in registers that need to be used by function, so they can be restored Get Execute function Put result where it can be found by caller

Function call continued Restore information in registers Return control to caller at appropriate address Caller get results

Function call -- issues How to save? How to provide for function? How to save information stored in registers? How to store return value of function so it is available to caller after function terminates

Solution 1 Use fixed location in memory for each function Nested calls of different functions can work Recursive calls, direct or mutually recursive, cannot work No longer used

Solution 2 Use a portion of memory as a stack allows recursive functions Need to keep a pointer to top of stack a register is usually used Information which can be stored on the stack (stored by caller) (stored by caller) register values needed after call (stored by caller or function) local variables for function (stored by function)

Runtime stack code memory stack memory main function 1 function 2

Runtime stack code memory stack memory main function 1 function 2 local variables return value

Runtime stack -- second call code memory main stack memory local variables function 1 function 2 return value local variables return value

Runtime stack -- red calls yellow code memory main stack memory function 1 function 2 local variables return value local variables return value local variables return value

After red - yellow call returns code memory main stack memory function 1 function 2 local variables return value local variables return value still there no longer used

After yellow- red call completes code memory main stack memory function 1 local variables still there no longer used still there no longer used function 2 return value

Solution 3 Store all values which need to be saved in registers Needs many registers Faster than using memory Eventually cannot handle recursive calls which go too deep Deep stack in registers used in Sun Sparc architecture

MIPS solution Mixed: keep as much as possible in registers Spill register contents to memory stack when needed Single function calls often use only registers Deeper function calls always use stack how much depends on register usage by functions

MIPS Register Usage ra (31) used for function sp (29) used as stack pointer v0, v1 function return value(s) a0, a1, a2, a3 function s registers guaranteed to hold same values across function calls (e.g. value after call is the same as it was before the call) t registers values may be changed by function call without being restored after

MIPS function call sequence 1. Caller places parameter values in registers $a0 - $a3 if more than four, place extra on stack 2. Caller saves values of a and t registers on stack, if needed later adjust stack pointer accordingly 3. Jump to function start address and place in register $ra jal instruction does this -- is + 4 4. Allocate memory needed for stack frame by subtracting frame size from $sp (stack pointer register). 5. Save callee-saved registers any s registers which the function will use the $ra registers if this function calls another Execute function

MIPS function call sequence - 2 11. Caller uses return value(s) which is (are) in register(s) $v0, $v1 10. Caller restores values of a and t registers from stack, if needed readjust stack pointer accordingly 9. Return to which is in $ra jr $ra instruction does this 8. Deallocate memory needed for stack frame by adding frame size to $sp (stack pointer register). 7. Restore callee-saved registers any s registers which the function used the $ra registers if this function called another 6. Place return value(s) in registers $v0-$v1