MIPS function continued

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

Common Problems on Homework

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

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

CDA3100 Midterm Exam, Summer 2013

MIPS Coding Continued

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

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

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

Compiling Techniques

Lecture 5: Procedure Calls

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

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

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

ECE260: Fundamentals of Computer Engineering

Lab 4 : MIPS Function Calls

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

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

Control Instructions

Thomas Polzer Institut für Technische Informatik

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

COMP2611: Computer Organization MIPS function and recursion

Chapter 2. Instructions:

MIPS Assembly Language Programming

Architecture II. Computer Systems Laboratory Sungkyunkwan University

ECE232: Hardware Organization and Design

Chapter 3. Instructions:

SPIM Instruction Set

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

Chapter 2A Instructions: Language of the Computer

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

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

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

MIPS Assembly Language Programming

MIPS Assembly (Functions)

Instruction Set Architectures (4)

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

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

Machine Instructions - II. Hwansoo Han

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

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Chapter 2: Instructions:

COMPUTER ORGANIZATION AND DESIGN

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

QtSPIM and MARS : MIPS Simulators

ECE 30 Introduction to Computer Engineering

CS 316: Procedure Calls/Pipelining

Computer Systems and Networks

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

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

CENG3420 Lecture 03 Review

Course Administration

CS/COE1541: Introduction to Computer Architecture

CSc 256 Midterm 1 Fall 2011

ECE331: Hardware Organization and Design

NAME: 1. (2) What is a transistor?

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

COE608: Computer Organization and Architecture

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam One 4 February Your Name (please print clearly)

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

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

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

Introduction to MIPS Processor

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

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

MIPS Functions and the Runtime Stack

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Assignment 1: Pipelining Implementation at SPIM Simulator

MIPS Instruction Set Architecture (2)

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

Lecture 6: Assembly Programs

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

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

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

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

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.

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

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

MIPS Assembly (FuncDons)

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

ECE 30 Introduction to Computer Engineering

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

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Chapter 2. Instructions: Language of the Computer

Computer Systems and Architecture

CS152 Computer Architecture and Engineering

Lec 10: Assembler. Announcements

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

MIPS Architecture and Assembly Language Overview

EE 361 University of Hawaii Fall

MIPS Assembly Language Guide

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

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

Computer Organization Lab #3.

CS3350B Computer Architecture MIPS Procedures and Compilation

EEC 581 Computer Architecture Lecture 1 Review MIPS

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

Transcription:

MIPS function continued

Review Functions Series of related instructions one after another in memory Called through the jal instruction Pointed to by a label like any other Returns by calling Stack Top pointed to by $sp Used to store registers between function calls Typically $ra and $s0-7; but will need to store $a0-3, $v0-1, and $t0-9 in the event of a series of nested calls Can be used to store other things too Such as characters in a string when reversing the string

Review Characters Use one byte of memory Can be used like integers and stored within integers Can use constants through the use of single quotes, e.g. li $t0, a Passed into and out of memory through sb / lb Strings An array of characters in memory Used like other arrays except Use.asciiz for a null-character terminated (C styled) string Use.ascii for a non null-character terminated string (not recommended) Enclose the array in double quotes

In Class Exercise 3.data A:.word 0,1,2,3,4,5,6,7,8,9 NF:.asciiz "not found" FL:.asciiz "found in lower" FU:.asciiz "found in upper".text.globl main main: la $a0, A li $a1, 10 li $a2, 6 li $a3, 5 jal findelements la $a0, NF blt $v0, $0, print la $a0, FL beq $v0, $0, print la $a0, FU print: li $v0, 4 syscall findelements: # $a0 = array # $a1 = length # $a2 = value 1 # $a3 = value 2 # $v0 = found value status addi $sp, $sp, -4 # Push ra onto stack sw $ra, 0($sp) li $v0, -1 # Set return to default li $v1, 0 # i loop: bge $v1, $a1, find_done lw $t0, 0($a0) # A[i] beq $t0, $a2, first_value beq $t0, $a3, second_value addi $v1, $v1, 1 # update i addi $a0, $a0, 4 # update array slot j loop first_value: ori $v0, $0, 0 # Set return to 0 j find_done second_value: ori $v0, $0, 1 # Set return to 1 done: li $v0, 10 syscall # exit find_done : lw $ra, 0($sp) addi $sp, $sp, 4 # Pop ra off of stack # Return

Implementing a Recursive Function Suppose we want to implement this in MIPS: It is a recursive function a function that calls itself. It will keep on calling itself, with different parameters, until a terminating condition is met.

The Recursive Function What happens if we call fact(4)? First time call fact, compare 4 with 1, not less than 1, call fact again fact(3). Second time call fact, compare 3 with 1, not less than 1, call fact again fact(2). Third time call fact, compare 2 with 1, not less than 1, call fact again fact(1). Fourth time call fact, compare 1 with 1, not less than 1, call fact again fact(0). Fifth time call fact, compare 0 with 1, less than 1, return 1. Return to the time when fact(0) was called (during the call of fact(1)). Multiply 1 with 1, return 1. Return to the time when fact(1) was called (during the call of fact(2)). Multiply 2 with 1, return 2. Return to the time when fact(2) was called (during the call of fact(3)). Multiply 3 with 2, return 6. Return to the time when fact(3) was called (during the call of fact(4)). Multiply 4 with 6, return 24.

The Recursive Function In MIPS, we say calling a function as going to the function. So we go to the function over and over again, until the terminating condition is met. Here, the function is called fact, so we will have a line of code inside the fact function: fact:

The Recursive Function The parameter should be passed in $a0. In the C function, every time we call fact, we call with n-1. So, in the MIPS function, before we do, we should have addi $a0, $a0,-1. fact: addi $a0, $a0, -1

The Recursive Function After calling fact, we multiply the return result with n, so, need to add multiplications. fact: addi $a0, $a0, -1 mul $v0, $v0, $a0

The Recursive Function After multiplying, we return. fact: addi $a0, $a0, -1 mul $v0, $v0, $a0

The Recursive Function So, one if else branch is done. The other branch is to compare $a0 with 1, and should call fact again if less than 1 and otherwise return 1. fact: slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 L1: addi $a0, $a0, -1 mul $v0, $v0, $a0 Any problems?

The Recursive Function The problem is that the function will call itself, as we have expected, but it will not return correctly! We need to save $ra, because we made another function call inside the function. We should always do so. Is this enough? fact: addi $sp, $sp, -4 sw $ra, 0($sp) slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 lw $ra, 0($sp) addi $sp, $sp, 4 L1: addi $a0, $a0, -1 mul $v0, $v0, $a0 lw $ra, 0($sp) addi $sp, $sp, 4

The Recursive Function So now we can return to the main function, but the return result is 0, why? A call to fact modifies $a0. But when we return from a call, we multiply it with $a0! So, should also save $a0! Restore it before using it again. fact: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 addi $sp, $sp, 8 L1: addi $a0, $a0, -1 lw $ra, 4($sp) lw $a0, 0($sp) mul $v0, $v0, $a0 addi $sp, $sp, 8

.text.globl main Main: li $a0, 4 done: li $v0,10 syscall fact: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 addi $sp, $sp, 8 L1: addi $a0, $a0, -1 lw $ra, 4($sp) lw $a0, 0($sp) mul $v0, $v0, $a0 addi $sp, $sp, 8

The Stack During Recursion 6/9/2013 week04-3.ppt 15

Two other MIPS pointers $fp: When you call a C function, the function may declare an array of size 100 like int A[100]. It is on the stack. You would want to access it, but the stack pointer may keep changing, so you need a fixed reference. $fp is the frame pointer, which should always point to the first word that is used by this function. $gp: the global pointer. A reference to access the static data.