Lab 4 : MIPS Function Calls

Similar documents
Lab 4 Prelab: MIPS Function Calls

COMP2611: Computer Organization MIPS function and recursion

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

MIPS function continued

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Lab 1 : An Introduction to SPIM, the MIPS architecture simulator

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

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

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

MIPS Assembly (Functions)

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

Instruction Set Architectures (4)

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

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

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

ECE260: Fundamentals of Computer Engineering

CSE Lecture In Class Example Handout

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

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

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

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.

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

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

CS 2504 Intro Computer Organization Test 1

Lab 9 : ALU Control and Data Path

MIPS Assembly (FuncDons)

Lecture 5: Procedure Calls

ECE 30 Introduction to Computer Engineering

SPIM Procedure Calls

MIPS Assembly (FuncDons)

ECE232: Hardware Organization and Design

Chapter 2A Instructions: Language of the Computer

Computer Systems and Networks

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

ECE 30 Introduction to Computer Engineering

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

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

Course Administration

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

Functions in MIPS. Functions in MIPS 1

CS 316: Procedure Calls/Pipelining

Implementing Procedure Calls

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

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Shift and Rotate Instructions

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

MIPS Procedure Calls - Review

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

Today. Putting it all together

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

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.

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

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

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

EE 361 University of Hawaii Fall

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

Lecture 5: Procedure Calls

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

CSE Lecture In Class Example Handout

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

Control Instructions

COMP2421 COMPUTER ORGANZATION. Lab 3

MIPS Assembly Language Guide

Compiling Techniques

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

Chapter 2. Instructions:

ECE260: Fundamentals of Computer Engineering

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

EE 109 Unit 15 Subroutines and Stacks

COE608: Computer Organization and Architecture

CDA3100 Midterm Exam, Summer 2013

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers

2B 52 AB CA 3E A1 +29 A B C. CS120 Fall 2018 Final Prep and super secret quiz 9

6.004 Tutorial Problems L3 Procedures and Stacks

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

Common Problems on Homework

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

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

Lecture 7: Procedures and Program Execution Preview

MIPS Reference Guide

MIPS Procedure Calls. Lecture 6 CS301

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

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

MIPS Instruction Set

CS 61c: Great Ideas in Computer Architecture

MIPS Functions and the Runtime Stack

ECE260: Fundamentals of Computer Engineering

(a) Implement processor with the following instructions: addi, sw, lw, add, sub, and, andi, or, ori, nor, sll, srl, mul

instructions aligned is little-endian

Chapter 3. Instructions:

MIPS Assembly Language Programming

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.

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

MIPS Assembly Language Programming

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

QtSPIM and MARS : MIPS Simulators

Thomas Polzer Institut für Technische Informatik

Transcription:

Lab 4 : 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 objective of this lab is to make you comfortable with MIPS function calls. 2 Pre-requisite Before starting with this lab, you should be familiar with MIPS function calls as well as MARS. 3 The Stack As stated in the prelab, there are some situations in which a function does not need to save any data from registers into memory. When this is not the case, however, the most logical place to store the data for later use is on the stack. 1

Computer Architecture and Design, Lab 4 2 The stack is a last in, first out (LIFO) data structure. This makes it ideal for function calls because a function needs only to manage the top of the stack. When another function is called, that function will push more data on the stack. When a function has completed and is ready to return control back to its caller, it will pop data off the stack, leaving the stack in the same form as it was before the function call. This means that functions need to only manage their own section of the stack and do not need to concern themselves with how other functions modify the stack. To use the stack, simply push and pop data from it. The push operation can be done with the following instructions: 1 addi $sp, $sp, 4 #Decrement t h e s t a c k p o i n t e r by 4 to make space 2 sw $r3, 0( $sp ) #Store $r3 on t h e s t a c k. The above code example will push register $r3 onto the stack. The pop operation can be done with: 1 lw $r3, 0( $sp ) #Get t h e v a l u e from t h e s t a c k and p l a c e i t in $r3 2 addi $sp, $sp, 4 #Increment t h e s t a c k p o i n t e r by 4 to r e l e a s e t h e space The above code example will pop whatever value is on the stack into register $r3. When using the stack, you must match your push and pop operations. If you fail to do this, your function is likely to cause your program to crash. 3.1 Frame Pointer The Frame Pointer ($fp) is a compliment to the stack pointer. Often, the stack will have a variable quantity of data pushed onto it during the lifetime of a function call. However a function may need to access data that was pushed onto the stack when it first began execution. Keeping track of how much data has been pushed onto the stack can be tedious so the frame pointer helps manage this. Typically, a frame pointer keeps track of where the stack pointer was before the function started and does not move throughout the lifetime of the function. This means that if a function first saves registers to the stack and then pushes a random amount of data onto the stack, it can use the frame pointer to access its register data stored on the stack. Figure 1 shows an example of the stack. Naturally, if you chose to use the frame pointer, you must save its previous value before changing it.

Computer Architecture and Design, Lab 4 3... Previous Procedure $fp Arguments Saved Registers Local Variables Dynamic Area $sp Fig. 1: MIPS Stack Frame 4 Calling functions within functions Calling a function within a function is no more complex than calling a function under normal circumstances. The key is to utilize the stack to keep track of $ra. The calling function needs to save the $ra register before jumping and linking to the next function and must restore $ra before returning itself. Typically, if a function needs to call other functions, it will save the $ra at the start of execution and restore it at the end, making the management of $ra easier.

Computer Architecture and Design, Lab 4 4 5 Questions 1. Enter the following code into a file named lab4-1.s and run it with MARS. Use it to answer the questions below: 1. data 2 msg1 :. a s c i i z Enter the f i r s t number\n 3 msg2 :. a s c i i z Enter the second number\n 4 msg :. a s c i i z The product i s 5. text 6. globl main 7. globl my mul 8 9 main : 10 addi $sp, $sp, 8 #make room f o r $ra and $fp on t h e s t a c k 11 sw $ra, 4( $sp ) #push $ra 12 sw $fp, 0( $sp ) #push $fp 13 14 la $a0, msg1 #l o a d address o f msg1 i n t o $a0 15 l i $v0, 4 16 s y s c a l l #p r i n t msg1 17 l i $v0, 5 18 s y s c a l l #r e a d i n t 19 add, $t0, $v0, $0 #put in $t0 20 la $a0, msg2 #l o a d address o f msg2 i n t o $a0 21 l i $v0, 4 22 s y s c a l l #p r i n t msg2 23 l i $v0, 5 24 s y s c a l l #r e a d i n t 25 add $a1, $v0, $0 #put i n $a1 26 add $a0, $t0, $0 #put f i r s t number in $a0 27 add $fp, $sp, $0 #s e t f p to top o f s t a c k p r i o r 28 #to f u n c t i o n c a l l 29 j a l my mul #do mul, r e s u l t i s in $v0 30 add $t0, $v0, $0 #save t h e r e s u l t in $t0 31 la $a0, msg 32 l i $v0, 4 33 s y s c a l l #p r i n t msg 34 add $a0, $t0, $0 #put computation r e s u l t i n $a0 35 l i $v0, 1 36 s y s c a l l #p r i n t r e s u l t number 37 38 lw $fp, 0( $sp ) #r e s t o r e ( pop ) $fp 39 lw $ra, 4( $sp ) #r e s t o r e ( pop ) $ra 40 addi $sp, $sp, 8 #a d j u s t $sp 41 l i $v0, 10 # system c a l l f o r e x i t 42 s y s c a l l 43 44 my mul : #m u l t i p l y $a0 with $a1 45 #does not handle n e g a t i v e $a1!

Computer Architecture and Design, Lab 4 5 46 #Note : This i s an i n e f f i c i e n t way to multipy! 47 addi $sp, $sp, 4 #make room f o r $s0 on t h e s t a c k 48 sw $s0, 0( $sp ) #push $s0 49 50 add $s0, $a1, $0 #s e t $s0 e q u a l to $a1 51 add $v0, $0, $0 #s e t $v0 to 0 52 mult loop : 53 beq $s0, $0, m u l t e o l 54 55 add $v0, $v0, $a0 56 addi $s0, $s0, 1 57 j mult loop 58 59 m u l t e o l : 60 lw $s0, 0( $sp ) #pop $s0 61 j r $ra

Computer Architecture and Design, Lab 4 6 (a) The above code example uses a subroutine to compute a multiplication. It contains an error; what is it? You may wish to step through the program in MARS to find it. (b) After fixing the code, demonstrate its operation to the TA.. (c) What is the hexadecimal value of $fp before the my mult function call? What is its value during the my mult function call? What is the $fp after main returns? 2. (a) Write a MIPS program with the following specifications: Use the my mul function in question 1 to create a function which computes factorials: n! = n (n 1)... 3 2 1. Each function must save all variables it modifies. Save your file as lab4-2.s, and make sure you comment the code. Demonstrate your progress to the TA;. (b) Your function uses a sub-routine to multiply two numbers. Does the order of its arguments affect the speed at which it runs? If so, explain why. 3. Create a program with the same functionality as that of Question 2 but with the following additional specifications:

Computer Architecture and Design, Lab 4 7 Contains a function to read an integer from the user into $v0. All other registers must be the same when it completes. You may use the syscall. Contains a function to print a message addressed by $a0. All registers must be the same when it completes. You may use the syscall. Contains a function to present the user with a prompt to enter a number. The prompt (message) is contained in $a0 and the result should be stored in $v0. All other registers must be the same when it completes. You may use the previous two functions. Save your file as lab4-3.s, and make sure you comment the code. Demonstrate your progress to the TA;. 6 Deliverables Submit the following: A completed copy of this lab. All source code files created in this lab (with comments).