Lab 4 Prelab: MIPS Function Calls

Similar documents
Lab 4 : MIPS Function Calls

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Lab 3 (Sections 300, 301 and 302) Prelab: MIPS-Control Instructions

Lab 3 (All Sections) Prelab: MIPS-Control Instructions

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

ECE232: Hardware Organization and Design

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

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

CS64 Week 5 Lecture 1. Kyle Dewey

Lecture 5: Procedure Calls

Functions and Procedures

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

ECE260: Fundamentals of Computer Engineering

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

Lab 8 (Sections 300, 301 and 302) Prelab: ALU and ALU Control

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.

Lecture 5: Procedure Calls

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

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

MIPS Procedure Calls. Lecture 6 CS301

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

Functions in MIPS. Functions in MIPS 1

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

MIPS Assembly (Functions)

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

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

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

Control Instructions

Compiling Code, Procedures and Stacks

Chapter 2A Instructions: Language of the Computer

SPIM Procedure Calls

Course Administration

CA Compiler Construction

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

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

MIPS%Assembly% E155%

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

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

Alexandria University

COMP2611: Computer Organization MIPS function and recursion

Implementing Procedure Calls

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

CSE Lecture In Class Example Handout

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

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

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

Chapter 2: Instructions:

ECE331: Hardware Organization and Design

Today. Putting it all together

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

LAB C Translating Utility Classes

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

ECE369. Chapter 2 ECE369

CS 61c: Great Ideas in Computer Architecture

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

COE608: Computer Organization and Architecture

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

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

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

CS 316: Procedure Calls/Pipelining

Computer Systems and Networks

ECE 30 Introduction to Computer Engineering

CS3350B Computer Architecture MIPS Introduction

Lab 7 (All Sections) Prelab: Verilog Review and ALU Datapath and Control

Computer Architecture

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

Procedures and Stacks

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

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Lab 7 (Sections 300, 301 and 302) Prelab: Introduction to Verilog

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

Chapter 3. Instructions:

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.

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

ECE 30 Introduction to Computer Engineering

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

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

CS 164, Midterm #2, Spring O, M, C - e1 : Bool O, M, C - e2 : t2 O, M, C - e2 : T3 O, M, C - if e1 then e2 else e3 fi : T2 _ T3

CS3350B Computer Architecture

MIPS Functions and Instruction Formats

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

CS61C : Machine Structures

MIPS Assembly (FuncDons)

Chapter 2. Instruction Set Architecture (ISA)

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

MIPS Functions and the Runtime Stack

Storage in Programs. largest. address. address

Code Generation. Lecture 19

CS/COE1541: Introduction to Computer Architecture

Common Problems on Homework

Compilers and computer architecture: A realistic compiler to MIPS

Instruction Set Architecture

Midterm II CS164, Spring 2006

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

Lab 7 (All Sections) Prelab: Introduction to Verilog

MIPS Assembly (FuncDons)

Lecture 6: Assembly Programs

ECE331: Hardware Organization and Design

Transcription:

Lab 4 Prelab: MIPS Function Calls Name: Sign the following statement: On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work 1 Objective The main objective of this lab is to understand function calls in MIPS. Before proceeding with this lab, you should be familiar with calling functions in MIPS (Chapter 2 of your textbook). 2 Function Calls In the previous lab, you learned how to do loops and if-then-else constructs. This allows you to create nearly every program you could conceive. However, often we have blocks of code that need to be used in multiple points throughout the program. Instead of replicating code multiple times, the better choice is to make it into a function. Then, whenever that block of code is needed, the program needs only to jump to the function, and then jump back when done. This is often referred to as a function call, and the block of code which issued the jump is often referred to as the caller. When you call a function, there must be a mechanism to return execution back to the caller. Without this mechanism, there would be no way for a function to know which block of code called it. MIPS provides an instruction which will save the PC before it jumps into a function. This is done through the jump-and-link instruction: jal Function #Jump and link 1

Computer Architecture and Design, PreLab 4 2 The jal instruction will immediately jump to the provided address like the j instruction, but unlike the j instruction, jal will also save the address of the instruction following the jal within $ra. Then, when the function is done doing its work, it can then jump to $ra register with jr. For example: 1 GetDimensions : #Get t h e dimensions o f a box from two corners. 2 #$a0 and $a1 are t h e c o o r d i n a t e s o f near corner ( x, y ) 3 #$a2 and $a3 are t h e c o o r d i n a t e s o f f a r corner ( x, y ) 4 sub $v0, $a2, $a0 #compute width 5 sub $v1, $a3, $a1 #compute h e i g h t 6 j r $ra 3 Register Usage MIPS has a convention of typical register usage in terms of function calls. You do not have to conform entirely to this convention, but it makes designing the calls much easier. Typically, the $a registers are used as function arguments for a function, $t registers are used for temporary values, and $s registers are used for saved values. Table 1 summarizes the typical convention. Tab. 1: MIPS Calling Convetion Name Usage Saved? $zero 0 N/A $v0-$v1 Return Values NO $a0-$a4 Arguments YES $t0-$t9 Temporary Values NO $s0-$s7 Saved Values YES $gp Global Pointer YES $sp Stack Pointer YES $fp Frame Pointer YES $ra Return Address YES This means that if a function does not need more than 10 temporary variables, nor calls any other functions, then it does not need to save any registers at all. It can simply use the temporary registers for its local variables and ignore the others. Conversely, if a block of code is going to make a function call, it cannot rely on the values of the temporary registers remaining the same after the call. In a pinch, the function can use the return value registers as temporary variables as well, so long as they contain the proper return value at the end.

Computer Architecture and Design, PreLab 4 3 4 Questions In the following questions do not overwrite any of the saved registers listed in Table 1. Use temporary and return registers to store values. 1. Write the MIPS instructions for the following c code. Assume that the arguments are stored in $a0 onward in the order they are presented in the function. int add ( int a, int b ) { return a + b ; } 2. Write the MIPS instructions for the following c code: int e v a l u a t i o n ( int a, int b, int c, int d ){ int sum = a + b + c + d ; int f i r s t = a b ; int l a s t = c d ; } return sum f i r s t + l a s t ; 3. Consider the instruction, jal Proc, which is located at address 0x00401024, what is the value of $ra upon entering the the function Proc?

Computer Architecture and Design, PreLab 4 4 4. Consider the following high-level recursive procedure: 1 int f ( int n, int k ) 2 { 3 int b ; 4 5 b=k+2; 6 i f ( n==0) b = 8 ; 7 else b = b + 4 n + f (n 1,k +1); 8 9 return b + k ; 10 } Translate the high-level procedure f into MIPS assembly language. Pay particular attention to properly saving and restoring registers across procedure calls; Use the MIPS preserved register convention; Clearly comment your code; Assume that the procedure starts at address 0x00400100; Keep local variable b in $s0 Step through your program by hand for the case of f(2,4). Draw a picture of the stack after the completion of the last recursive call. Write the register name and data value stored at each location in the stack. Assume that when f is called, $s0=0xabcd and $ra=0x400004. Use the last page of this document as a guideline. You may submit a hard copy of this part of the prelab. What is the final value of v0?

Computer Architecture and Design, PreLab 4 5 $%&'()*!"#