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

Similar documents
COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

ECE260: Fundamentals of Computer Engineering

CA Compiler Construction

Implementing Procedure Calls

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

Control Instructions

CSE Lecture In Class Example Handout

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

Run-time Environment

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

Course Administration

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

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

Functions in MIPS. Functions in MIPS 1

MIPS Procedure Calls. Lecture 6 CS301

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

ECE232: Hardware Organization and Design

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

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

Lecture 5: Procedure Calls

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

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Procedure Call and Return Procedure call

Storage in Programs. largest. address. address

Stacks and Procedures

Lecture 5: Procedure Calls

Today. Putting it all together

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

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

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

Chapter 2A Instructions: Language of the Computer

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

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

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

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

CSE Lecture In Class Example Handout

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

ECE369. Chapter 2 ECE369

Instruction Set Architectures (4)

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

Stacks and Procedures

SPIM Procedure Calls

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

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

Chapter 3. Instructions:

Chapter 2. Instructions:

Functions and Procedures

CENG3420 Lecture 03 Review

MIPS Assembly (Functions)

Lecture 7: Procedures and Program Execution Preview

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

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

More C functions and Big Picture [MIPSc Notes]

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

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

Compiling Code, Procedures and Stacks

CS61C : Machine Structures

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

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

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

Compilers and computer architecture: A realistic compiler to MIPS

Lecture 7: Procedures

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

MIPS Functions and the Runtime Stack

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

CS 316: Procedure Calls/Pipelining

ECE331: Hardware Organization and Design

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.

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

Instructions: Assembly Language

Quiz for Chapter 2 Instructions: Language of the Computer3.10

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

Architecture II. Computer Systems Laboratory Sungkyunkwan University

MIPS Functions and Instruction Formats

Stacks and Procedures

We will study the MIPS assembly language as an exemplar of the concept.

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

CS3350B Computer Architecture MIPS Introduction

CS3350B Computer Architecture

CS 61c: Great Ideas in Computer Architecture

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

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

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

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

CS64 Week 5 Lecture 1. Kyle Dewey

Thomas Polzer Institut für Technische Informatik

MIPS%Assembly% E155%

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

Stacks and Procedures

Anne Bracy CS 3410 Computer Science Cornell University

MIPS Assembly (FuncDons)

Q1: /14 Q2: /12 Q3: /8 Q4: /8. Total: /42

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

COMPUTER ORGANIZATION AND DESIGN

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.

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

Lecture 4: MIPS Instruction Set

Transcription:

MIPS Calling Convention Chapter 2.7 Appendix A.6 Procedure Calls Main Procedure Call Procedure Call Procedure Procedure Calls Procedure must from any call Procedure uses that main was using We need a convention to Name $zero $v0-$v1 $a0-$a3 $t0-$t7 $s0-$s7 $t8-$t9 $gp 0 2-3 4-7 8-15 16-23 24-25 28 29 30 31 MIPS-specific info Reg Number Usage Saved Page 140, Figure 3.13 The constant 0 Function results Function Arguments Temporaries More temporaries Global pointer Stack pointer Frame pointer Return address Preserved across call? MIPS-specific info who cares? Preserved Value is same after call Caller Procedure t preserved guarantees Caller Procedure 1

Steps for caller 1. Store away any temporary registers we want 2. Pass function parameters to procedure 3. Transfer control to procedure 4. (then procedure executes) 5. Get return value 6. Restore any temp regs we saved away Steps for procedure 1. Allocate stack space 2. Store preserved regs we may use 3. Perform task 4. Place result in proper location for caller 5. Restore preserved regs we may have used 6. Transfer control back to caller Write the caller & procedure code for the following function: int MyFunc(int g, int h) { return (g + h);} Caller: Assume: g,h are in $s2,$s3. We want return value in $s0. Caller wants to preserve $t0 across function call. Callee: How do we know how much space until we know how many regs to store? Steps for procedure How do we know what we ll use until we write task code? 1. Allocate stack space 2. Store preserved regs we may use 3. Perform task 4. Place result in proper location for caller 5. Restore preserved regs we may have used 6. Transfer control back to caller Definitions Leaf function Makes no function calls n-leaf function Contains a function call 2

Allocating stack space How much stack space? Minimum allocation: 24 bytes Only allocate once per function!!!! contains address of bottom of stack. What operation on allocates space? Stack space for this function Minimum allocation 24 bytes, even if unused. 4 words for $a0-$a3 in case we need it Preserved registers that we destroy (i.e. $s0) Local variables declared in our function Any other outgoing arguments (non-leaf) Total bytes must be divisible by 8 (aligned for floating-point numbers) What actually goes in stack Assume it needs 2 saved registers Example before call P*4 bytes for preserved regs ($s0-$s7) L*4 bytes for local data A*4 bytes for outgoing args Extra Arguments preserved registers (and $a0-$a3) padding local data Extra outgoing arguments int foo(int arg1, int arg2) { int myarray[64]; myarray[3] = 5; } bar(a1, a2, a3, a4, a5); return (myarray[3]); Local 256- byte array n-leaf function, 5 outgoing args addi,, - (1+64+1+2+6)*4 sw, 73*4() sw, 72*4() sw $s1, 67*4() sw $s0, 66*4() before call addi $t0, $zero,5 # $t0 = 5 sw $t0, (1+3)*4 ()# myarray[3] = 5 lw, 73*4() lw, 72*4() lw $s1, 67*4() lw $s0, 66*4() addi,, (1+64+1+2+6)*4 jr Caller s Stack $a0 $a1 $a2 $a3 $s1 $s0 padding myarray outgoing arg 5 3

Recursive call: SumToN int SumToN(int N) { if (N < 2) return 1; else return (SumToN(N-1) + N); } Both the caller and the callee!!! addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(2) addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(2) addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(2) addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(2) addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(1) 4

addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(1) $v0 = 1 addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(1) $v0 = 1 addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(2) addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(2) addi,, -24 sw, 20 () sw, 16 () addi,, 20 result: sw $a0, 0 () lw $a0, 0 () end: lw, 20 () lw, 16 () addi,, 24 jr SumToN(2) What about the garbage in the stack? MIPS instructions Rule: Destination register always comes first Exception: Rule: Any instruction involving constants has i at the end of instruction(add vs addi) Exception: 5