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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Instruction Set Architectures (4)

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

COMP2611: Computer Organization MIPS function and recursion

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

ECE 30 Introduction to Computer Engineering

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

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

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

CS 316: Procedure Calls/Pipelining

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

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

MIPS Assembly (Functions)

Course Administration

Lecture 5: Procedure Calls

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

Today. Putting it all together

ECE260: Fundamentals of Computer Engineering

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

Procedures and Stacks

Functions and Procedures

MIPS function continued

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

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

MIPS Procedure Calls. Lecture 6 CS301

CS64 Week 5 Lecture 1. Kyle Dewey

Compiling Code, Procedures and Stacks

Implementing Procedure Calls

ECE232: Hardware Organization and Design

Lecture 6: Assembly Programs

Procedure Call and Return Procedure call

Lec 10: Assembler. Announcements

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

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

Common Problems on Homework

CSE Lecture In Class Example Handout

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

MIPS Assembly (FuncDons)

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

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

ECE 250 / CS 250 Computer Architecture. Procedures

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

EE 361 University of Hawaii Fall

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

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

Control Instructions

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

Computer Systems and Networks

MIPS Functions and the Runtime Stack

ECE331: Hardware Organization and Design

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

SPIM Instruction Set

Code Generation. Lecture 19

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

Lab 4 : MIPS Function Calls

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

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.

SPIM Procedure Calls

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

MIPS Assembly Language Guide

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

MIPS Assembly (FuncDons)

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

Midterm II CS164, Spring 2006

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

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

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

MIPS Assembly Language Programming

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

MIPS Assembly Language Programming

LAB C Translating Utility Classes

Storage in Programs. largest. address. address

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

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

CS153: Compilers Lecture 8: Compiling Calls

Compilers and computer architecture: A realistic compiler to MIPS

Chapter 2A Instructions: Language of the Computer

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

MIPS Functions and Instruction Formats

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

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

Homework 9: Stack Frame

Computer Architecture

The Activation Record (AR)

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

Instruction Set Architecture

S Computer Architecture. Computer architecture assignment E1 A subroutine, which searches for given byte pattern from an byte array.

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

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

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

Code Generation. Lecture 12

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

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

Transcription:

Function Calls Tom Kelliher, CS 240 Feb. 13, 2002 1 Administrivia Announcements Collect homework. Assignment Read 3.7 9. From Last Time SPIM lab. Outline 1. Function calls: stack execution model, memory use, call/return convention, example. Coming Up Operand addressing, program life cycles. 1

2 Function Calls Consider the following code fragment: 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); 2

Notes: 1. Caller/callee. 2. Call by value parameters. Formal/actual. Other call mechanisms: address, reference, 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) Stack frame. (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. 3

factorial uses $s0 and has a local copy of n (inefficient). High memory bar() 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 $sx register used. (c) 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 $ax 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 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.spim --- 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: while1:.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 beqz $s0, endwhile1 li $v0, 4 la $a0, response move $a0, $s0 jal factorial move $a0, $v0 li $v0, 1 li $v0, 4 # Push frame & save registers. # Print instruction. # Get a number from keyboard. # Store locally. # Compute until 0 entered. # 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. b while1 endwhile1: lw $s0, 8($sp) # Restore registers and pop frame. lw $ra, 12($sp) lw, 16($sp) add $sp, $sp, 16 li $v0, 0 jr $ra # 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() # push frame & save registers. # Value read left in $v0. lw $ra, 8($sp) # restore registers & pop frame. lw, 12($sp) add $sp, $sp, 12 jr $ra 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 10