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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Instruction Set Architectures (4)

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

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

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

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

MIPS Assembly (Functions)

ECE 30 Introduction to Computer Engineering

COMP2611: Computer Organization MIPS function and recursion

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

Functions in MIPS. Functions in MIPS 1

CS 316: Procedure Calls/Pipelining

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

MIPS function continued

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

Lecture 5: Procedure Calls

Today. Putting it all together

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

Procedures and Stacks

Compiling Code, Procedures and Stacks

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

Functions and Procedures

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

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

Common Problems on Homework

Course Administration

MIPS Procedure Calls. Lecture 6 CS301

CSE Lecture In Class Example Handout

CS64 Week 5 Lecture 1. Kyle Dewey

Implementing Procedure Calls

ECE232: Hardware Organization and Design

Procedure Call and Return Procedure call

Lec 10: Assembler. Announcements

MIPS Assembly (FuncDons)

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

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

ECE260: Fundamentals of Computer Engineering

CA Compiler Construction

6.004 Tutorial Problems L3 Procedures and Stacks

Lecture 5: Procedure Calls

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Lecture 6: Assembly Programs

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

Implement a C language function that computes the factorial of a number, recursively, according to the following prototype:

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.

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

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

MIPS Assembly Language Guide

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

MIPS Assembly (FuncDons)

Lecture 7: MIPS Functions Part 2. Nested Function Calls. Lecture 7: Character and String Operations. SPIM Syscalls. Recursive Functions

MIPS Functions and the Runtime Stack

ECE331: Hardware Organization and Design

SPIM Instruction Set

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

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

SPIM Procedure Calls

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

ECE 250 / CS 250 Computer Architecture. Procedures

CS153: Compilers Lecture 8: Compiling Calls

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

Homework 9: Stack Frame

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

Control Instructions

EE 361 University of Hawaii Fall

Midterm II CS164, Spring 2006

Computer Systems and Networks

Code Generation. Lecture 19

Homework 4 - Solutions (Floating point representation, Performance, Recursion and Stacks) Maximum points: 80 points

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

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

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Compilers and computer architecture: A realistic compiler to MIPS

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

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

Lab 4 : MIPS Function Calls

MIPS Assembly Language Programming

The Activation Record (AR)

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

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

MIPS Assembly Language Programming

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Instruction Set Architecture

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

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Lecture 10: Recursive Functions. Computer System and programming in C 1

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

Chapter 2A Instructions: Language of the Computer

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

Code Generation. Lecture 12

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

MIPS Functions and Instruction Formats

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

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

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

Transcription:

Function Calls Tom Kelliher, CS 220 Oct. 24, 2011 1 Administrivia Announcements Assignment SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how. From Last Time Outline 1. Function calls: stack execution model, memory use, call/return convention, example. Coming Up Continuation of the function call mechanism. 2 Function Calls Consider the following code fragment: 1

int min(int, int); int factorial(int); void main(void) int i = 3; int j = 5; int k; k = min(i + 3, j); /* Current values of i, j, k? */ k = factorial(5); int min(int a, int b) int i; if (a < b) i = a; else i = b; b = 0; return i; int factorial(int n) if (n <= 1) return 1; else return n * factorial(n - 1); Notes: 1. Caller/callee. 2. Call by value parameters. Formal/actual. Other call mechanisms: address, reference, 2

name, etc. 3. How are the values passed? How is the return value returned? How is memory allocated? 4. How are the recursive invocations of factorial() managed? 2.1 Stack Execution Model 1. Maintain a stack of active function invocations: High memory main() min()... main() factorial(5) factorial(4) factorial(3) $sp (a) Frame stack. (b) Contents of a frame: local variables, saved registers, return address, frame pointer of caller, actual parameters. Scratch-pad storage on top of the stack. (c) Frame pointer and stack pointer. (d) Caller vs. callee save. 2. More detail: Suppose main calls factorial, using an on-stack parameter (n) pass. factorial uses $s0 and has a local copy of n (inefficient). 3

High memory main() s frame $sp n $ra $s0 n factorial() s frame Example code snippets: lw $s0, 4() sw $s0, -12() sw $s0, 0($sp) sub $sp, $sp, 4 3. Register usage convention: (a) $v[0-1] function results. (b) $a[0-3] function arguments. (c) $t[0-9] caller save registers. (Except for two global pointers, ignore these registers.) (d) $s[0-7] callee save registers. (e) $sp top of stack pointer, pointing to first free location on execution stack. (f) frame pointer, pointing to base of current frame. (g) $ra return address. 4

2.2 Memory Use 1. Upon entry to main, the frame stack is ready to go. Program parameters. 2. main pushes its frame and gets to work. 3. Upon exit, main restores $ra (among others) and executes jr $ra. 2.3 Call/Return Conventions Use this sequence. Assume caller is calling callee. Caller: 1. Store any caller save registers. 2. Store any register parameters. 3. Push any on-stack parameters (caller and callee must agree on order). (C: last pushed first.) 4. Execute jal callee Callee: 1. Push stack frame by subtracting size of frame from $sp. Number of words needed: (a) One each for and $ra. (b) One for each $sn register used. (c) (Optional) One for each parameter and each local variable. 5

2. Save registers (, $ra, callee save), using $sp as base register. 3. Set by adding frame size to $sp and storing. 4. (Optional) Copy parameters into frame. 5. Body of callee executes. Frame references should use as base register. 6. Store return value in appropriate $vn registers. 7. Restore saved registers, using $sp as base register. 8. Pop frame from stack by adding frame size to $sp. 9. Execute jr $ra Caller: 1. Collect and store return values. 2.4 Example Code 1. Consider the following C++ code which recursively computes the factorial: #include <stdio.h> int getnum(void); int factorial(int n); int main() int value; printf("enter 0 to exit.\n"); value = getnum(); while (value!= 0) 6

printf("factorial: %d\n", factorial(value)); value = getnum(); return 0; int getnum(void) int num; printf("number: "); scanf("%d", &num); return num; int factorial(int n) if (n <= 1) return 1; return n * factorial(n - 1); 2. Write a MIPS program which passes parameters in registers. 3. Frame maps: main $ra $s0 value getnum $ra num factorial $ra $s0 n 4. Here is the corresponding SPIM program: # factorial.s --- A recursive SPIM program. Demonstrates function # call and return. 7

.data prompt:.asciiz "Number: " nl:.asciiz "\n" instr:.asciiz "Enter 0 to exit.\n" response:.asciiz "Factorial: " ###################################################################### # main ###################################################################### main:.text.globl main sub $sp, $sp, 16 sw, 16($sp) sw $ra, 12($sp) sw $s0, 8($sp) add, $sp, 16 li $v0, 4 la $a0, instr jal getnum sw $v0, -12() move $s0, $v0 # Push frame & save registers. # Print instruction. # Get a number from keyboard. # Store locally. while1: beqz $s0, endwhile1 # Compute until 0 entered. li $v0, 4 la $a0, response move $a0, $s0 jal factorial move $a0, $v0 li $v0, 1 li $v0, 4 # Print response prompt. # Pass argument # Call # Copy return value to print it # Prepare to read next number. 8

la $a0, nl jal getnum sw $v0, -12() move $s0, $v0 # Store number just read. endwhile1: b while1 lw $s0, 8($sp) lw $ra, 12($sp) lw, 16($sp) add $sp, $sp, 16 li $v0, 0 jr $ra # Restore registers and pop frame. # Exit. ###################################################################### # getnum --- returns integer read from keyboard through $v0. ###################################################################### getnum:.text sub $sp, $sp, 12 sw, 12($sp) sw $ra, 8($sp) add, $sp, 12 li $v0, 4 la $a0, prompt li $v0, 5 sw $v0, -8() lw $ra, 8($sp) lw, 12($sp) add $sp, $sp, 12 jr $ra # push frame & save registers. # Value read left in $v0. # restore registers & pop frame. 9

###################################################################### # factorial --- compute factorial of $a0 and return result through # $v0 ###################################################################### factorial:.text sub $sp, $sp, 16 sw, 16($sp) sw $ra, 12($sp) sw $s0, 8($sp) add, $sp, 16 sw $a0, -12() # Our value n. move $s0, $a0 bgt $s0, 1, recurse # Base case. Return 1 li $v0, 1 lw $s0, 8($sp) lw $ra, 12($sp) lw, 16($sp) add $sp, $sp, 16 jr $ra recurse: sub $a0, $a0, 1 # Recursive call. Compute (n-1)! jal factorial mul $v0, $v0, $s0 # n * (n-1)! lw $s0, 8($sp) lw $ra, 12($sp) lw, 16($sp) add $sp, $sp, 16 jr $ra 5. Class exercise to complete the gcd() function for gcd.c/gcd.s. Notes for exercise: (a) The parameters i and j are in $a0 and $a1 as gcd() begins execution. How long can you leave them there? (b) What must you do before using $s0 and $s1? Why? 10