4.2: MIPS function calls

Size: px
Start display at page:

Download "4.2: MIPS function calls"

Transcription

1 4.2: MIPS function calls Topics: Nested function calls: poly() and pow() Saving the return address ($31) Saving temporary registers Looking at activation records on the stack Introduction: In CSc 256 lectures, we discussed operations involving the stack. By using the stack with procedure calls, we will have all of the function call capability available to us in high level languages such as C and C++. We saw some examples of function calls and stack frames in the previous exercise. Recall some guidelines about MIPS register usage from CSc 256 lectures: $a0 - $a3 are used for arguments. These are not preserved across function calls. What does "not preserved across function calls" mean? Consider the following example: li $a0, 4 jal func1 move $t0,$a0 First $a0 is loaded with the constant 4. Then we call func1. Since $a0 is not preserved across function calls, after the jal func1, there is no guarantee that $a0 will still contain the constant 4! Hence, to be safe, we should assume that $a0 contains some other value, and we are moving some random value into $t0. $v0 and $v1 are used by functions as output registers to return results. Since $v0 and $v1 contain results, they are also not preserved across function calls. $t0 - $t9 are used as temporary registers (for temporary variables, loop counters, etc). These are also not preserved across function calls. $s0 - $s8 are also used as temporary registers. However, these are preserved across function calls. Consider the following example: li $s0, 4 jal func1 move $t0,$s0 Since, according to MIPS register usage conventions, $s0 is preserved across function calls, we CSc 256 Lab Manual 4.2.1

2 know that $s0 will contain the constant 4 both before and after the jal func1. So we will move a 4 into $t0. Remember that registers do not magically get preserved across function calls!! In the code for a function, if you're using $s0 as a temporary register, you will first have to save it on the stack, and then restore it later. This makes sure that the old value of $s0 is preserved. In the last exercise, we looked at stack frames or activation records in C++. This is a data structure that's associated with a function call that's active, and is stored on the stack. When the function is called (i.e., there is an active instance of that function), a stack frame/activation record is created on the stack. Recall from the last exercise, in Example 4.2, the main program calls the function poly(); a stack frame for poly() is created on the stack. Then poly() makes calls to the function pow(); an stack frame for pow() is created on the stack, on top of the activation record for poly(). In MIPS, pow() is a leaf procedure which does not call other functions; hence, it doesn t need a stack frame or activation record. The activation records for poly() contains the saved values of temporary registers, and the return address for poly(). Remember that the return address of a function call is saved in $31 by the jal instruction. Hence, when the main program calls poly(), the return address for poly() is saved in $31. When poly() calls pow(), pow() will also try to save its return address in $31! Hence, to prevent pow()'s return address from overwriting poly()'s return address, poly() has to save its return address in its activation record. The exact position in the activation record where the return address is saved depends on the procedure call convention of the operating system and compiler that we are working with. Obviously, before we return from poly(), we'll need to restore its return address from the stack (simply load it from the stack into $31). Now on to the exercise. Again, you may use either spim or xspim for the exercise; we ll only show the spim version to save space. Steps: Earlier, we copied the file ~whsu/csc256/labs/progs/4.2 into our directory. This is essentially identical to Example 4.2 that we discussed in 256 lectures, but with a few extra global labels (for breakpoints). First we look at the main program (some lines omitted for clarity): # # CSc 256 Example 4.2: Poly function # Name: William Hsu # Date: 6/22/2010 # Description: Computes x^4 + x^3 + 1, x from 2 to 4.data CSc 256 Lab Manual 4.2.2

3 endl:.asciiz "\n" # i $s0 # 4 $s2.text.globl bk1.globl end_pow main: li $s1, 4 li $s0, 0 # for (i=2; i<=4; i++) { m_for: move $a0, $s0 # result = poly(i); bk1: jal poly move $a0, $v0 # cout << result << endl; li $v0, 1 syscall la $a0, endl li $v0, 4 syscall addi $s0, $s0, 1 # } ble $s0, $s1, m_for li $v0, 10 #} syscall Fairly straightforward; the main program calls poly(i) (the argument for poly() is passed in $a0), repeatedly. Let's take a look at the function poly(): # int poly(int arg) # arg $a0 # return result in $v0 # computes arg^4 + arg^3 + 1 # copy of arg $s0 # temp1 $s1 First we save the return address and registers $s0 and $s1 used by poly(): poly: addi $sp, $sp, -12 sw $s1, ($sp) sw $s0, 4($sp) sw $ra, 8($sp) Remember from CSc 256 lectures that $s0 and $s1 have to be saved because the temporary registers $s0 through $s8 are preserved across function calls. Hence, we must make sure we do CSc 256 Lab Manual 4.2.3

4 not mess them up for the main program. However, if we used $t0 through $t9 in poly(), we won't have to save them, because $t0 through $t9 are not preserved across function calls, and the main program expects any function calls to change them. Next we prepare for the call to pow(arg,4). We load the arguments, and save arg (currently in $a0) in $s0. This is because $a0 through $a3 are not preserved across function calls. We need to make sure we have a copy of arg for later. (Actually pow() doesn't change $a0, but we don't always know that a function will not change its input registers.) move $s0,$a0 # temp1 = pow(arg, 4); li $a1,4 jal pow Notice that here we use $s0 (instead of $t0, $t1 etc). This is because we want the contents of $s0 to remain unchanged after the jal. After the jal pow, the result of pow(arg,4) is in $v0. We save a copy of this in $s1, then call pow(arg,3): move $s1,$v0 move $a0,$s0 # result = pow(arg, 3); li $a1,3 jal pow Finally, we compute the result by adding up our partial results. To wrap up, we move the stack pointer, restore all temporaries and the return address, and return to the main program: poly() calls pow(): add $v0, $v0, $s1 # result = temp1 + result + 1; addi $v0, $v0, 1 lw $ra, 8($sp) lw $s0, 4($sp) lw $s1, ($sp) addi $sp, $sp, 12 # return result; jr $ra #} # int pow(int arg0, int arg1) # arg0 $a0 # arg1 $a1 # return result in $v0 # Computes arg0 to the arg1-th power # i $t0 # product $t1 CSc 256 Lab Manual 4.2.4

5 pow: li $t1, 1 # int product = 1; li $t0, 0 # for (int i=0; i<arg1; i++) { bge $t0, $a1, endpow for: mul $t1, $t1, $a0 # product *= arg0; addi $t0, $t0, 1 # } blt $t0, $a1, for endpow: move $v0, $t1 # return product; jr $ra #} Now we're ready to run poly.s. We'll set some breakpoints, and start the program: lo "4.2" br bk1 br endpow run Breakpoint encountered at 0x We're stopped at the first breakpoint bk1 (at 0x400030): bk1: jal poly Let's check some registers: print $a0 Reg 4 = 0x (2) pr $31 Reg 31 = 0x ( ) arg for poly(2) return address (not loaded yet) When we step through the jal instruction, we should jump to the label poly, and put the return address for poly() (0x40002c) in $31. step [0x ] 0x0c jal 0x [poly] poly pr $31 Reg 31 = 0x ( ) pr $sp Reg 29 = 0x7ffffac0 ( ) pr $s0 Reg 16 = 0x (0) pr $s1 ; 21: jal CSc 256 Lab Manual 4.2.5

6 Reg 17 = 0x (4) The jal instruction is at 0x400030, and the return address is 0x The next few instructions save $31, and the temporaries $s0 and $s1 on the stack. step [0x ] 0x23bdfff4 addi $29, $29, -12 ; 45: addi $sp, $sp, -12 [0x ] 0xafb10000 sw $17, 0($29) ; 46: sw $s1, ($sp) [0x ] 0xafb00004 sw $16, 4($29) ; 47: sw $s0, 4($sp) [0x c] 0xafbf0008 sw $31, 8($29) ; 48: sw $ra, 8($sp) [0x ] 0x addu $16, $0, $4 ; 50: move $s0, $a0 pr $sp Reg 29 = 0x7ffffab4 ( ) pr 0x7ffffab4 Stack 0x7ffffab4 ( ) = 0x (4) Stack 0x7ffffab8 ( ) = 0x (0) Stack 0x7ffffabc ( ) = 0x ( ) So the top of the stack looks like this: address description 0x7ffffab4 old $s1 0x7ffffab8 old $s0 0x7fffabc return address for poly(2) The return address for poly(2) and temporary registers have been saved on the stack. Now we'll save a copy of the argument for poly(2), load arguments for pow(2,4) and call pow(2,4). When we execute the jal pow instruction, we'll jump to the label pow, and save the return address for pow(2,4) in $31: step CSc 256 Lab Manual 4.2.6

7 [0x ] 0x23bdfff4 addi $29, $29, -12 ; 45: addi $sp, $sp, -12 [0x ] 0x ori $5, $0, 4 ; 51: li $a1, 4 # temp1 = pow(arg, 4); [0x ] 0x0c10002a jal 0x004000a8 [pow] ; 52: jal pow pr $31 Reg 31 = 0x c ( ) (The address of pow is 0x4000a8. The address of the instruction jal pow is 0x400078, and the return address for pow(0,4) is 0x40007c, which is now in $31.) When we continue, we skip past the entire loop of pow(2,4), and stop at endpow (a breakpoint). We ve calculated pow(2,4) = 16, now in $v0. Finally, we return to the code of poly() which performed the call, with the jr $31 (remember that $31 contains the return address 0x40007c). cont Breakpoint encountered at 0x004000c8 pr $31 Reg 31 = 0x c ( ) step [0x004000c8] 0x addu $2, $0, $9 ; 84: move $v0, $t1 # return product; [0x004000cc] 0x03e00008 jr $31 ; 85: jr $ra #} [0x c] 0x addu $17, $0, $2 ; 53: move $s1, $v0 pr $v0 Reg 2 = 0x (16) We're back in the code for poly(), right after the first jal pow (the move $s1, $v0 instruction at 0x40007c): move li jal move $s0,$a0 $a1,4 pow $s1,$v0 CSc 256 Lab Manual 4.2.7

8 We'll step through a few more instructions, save the result of pow(2,4), load the arguments, and call pow(2,3). After we step through the jal pow instruction, PC becomes 0x4000a8, the address of pow, and $31 gets the new return address, 0x40008c. (Remember that the jal pow instruction is at 0x ) step [0x c] 0x addu $17, $0, $2 ; 53: move $s1, $v0 [0x ] 0x addu $4, $0, $16 ; 55: move $a0, $s0 # result = pow(arg, 3); [0x ] 0x ori $5, $0, 3 ; 56: li $a1, 3 [0x ] 0x0c10002a jal 0x004000a8 [pow] ; 57: jal pow pr PC PC = 0x004000a8 ( ) pr $31 Reg 31 = 0x c ( ) Now we continue past the loop for pow(2,3), stopping at endpow. We'll check that $v0 contains the result (2^3 = 8), $31 contains the return address 0x40008c, then return to poly(2) by executing jr $31: cont Breakpoint encountered at 0x004000c8 step [0x004000c8] 0x addu $2, $0, $9 ; 84: move $v0, $t1 # return product; pr $v0 Reg 2 = 0x (8) pr $31 Reg 31 = 0x c ( ) step [0x004000cc] 0x03e00008 jr $31 ; 85: jr $ra #} pr PC PC = 0x c ( ) Back in the code for poly(2), we've already calculated pow(2,4) and pow(2,3). Now we accumulate the partial result, to get the result for poly(2) (2^4 + 2^3 + 1 = 25): CSc 256 Lab Manual 4.2.8

9 step [0x c] 0x add $2, $2, $17 ; 59: add $v0, $v0, $s1 # result = temp1 + result + 1; [0x ] 0x addi $2, $2, 1 ; 60: addi $v0, $v0, 1 pr $v0 Reg 2 = 0x (25) Now remember that the activation record for poly(2) is still on the stack. Let's check this: pr $sp Reg 29 = 0x7ffffab4 ( ) pr 0x7ffffab4 Stack 0x7ffffab4 ( ) = 0x (0) Stack 0x7ffffab8 ( ) = 0x (0) Stack 0x7ffffabc ( ) = 0x ( ) We need to restore all the temporary registers we saved, restore the return address of poly(2) to $31, and "destroy" the activation record by updating $sp: step [0x ] 0x8fbf0008 lw $31, 8($29) ; 62: lw $ra, 8($sp) [0x ] 0x8fb00004 lw $16, 4($29) ; 63: lw $s0, 4($sp) [0x c] 0x8fb10000 lw $17, 0($29) ; 64: lw $s1, ($sp) [0x004000a0] 0x23bd000c addi $29, $29, 12 ; 65: addi $sp, $sp, 12 # return result; [0x004000a4] 0x03e00008 jr $31 ; 66: jr $ra #} pr PC PC = 0x ( ) 0x is the address of the move $a0, $v0 instruction preparing to print the result: CSc 256 Lab Manual 4.2.9

10 pr 0x [0x ] 0x addu $4, $0, $2 ; 23: move $a0, $v0 # cout << result << endl; So we're back in the main program! Now a continue will print out the result of 2^4 + 2^3 + 1: cont 25 Breakpoint encountered at 0x We've just traced through a single call to poly(2). The main program contains calls to poly(3), poly(4), etc. If you feel that you need additional practice, you should trace through the second call yourself. It should be fairly similar to the first. If you're lazy, just hit continue a few times and get the result for the rest: cont [more blank lines here...] 109 cont [more blank lines here...] 321 etc etc Let's review what we saw in this exercise: i. When a function/procedure is called, an activation record is created on the stack. ii. When we return from that function call, its activation record is destroyed. iii. The activation record contains saved registers and the saved return address. iv. Activation records (or stack frames) keep track of what function calls are active. CSc 256 Lab Manual

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

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring 2009 Topic Notes: MIPS Programming We spent some time looking at the MIPS Instruction Set Architecture. We will now consider

More information

MIPS Assembly (Functions)

MIPS Assembly (Functions) ECPE 170 Jeff Shafer University of the Pacific MIPS Assembly (Functions) 2 Lab Schedule This Week Activities Lab work time MIPS functions MIPS Random Number Generator Lab 11 Assignments Due Due by Apr

More information

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

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS Lectures 5 Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS 1 OOPS - What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; } 2

More information

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

More information

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

Lecture 5. Announcements: Today: Finish up functions in MIPS Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A

More information

Instruction Set Architectures (4)

Instruction Set Architectures (4) Computer Architecture Week 06 Instruction Set Architectures (4) College of Information Science and Engineering Ritsumeikan University subroutines functions, procedures remember the next instruction s address

More information

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

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4 EN164: Design of Computing Systems Lecture 11: Processor / ISA 4 Professor Sherief Reda http://scale.engin.brown.edu Electrical Sciences and Computer Engineering School of Engineering Brown University

More information

Functions in MIPS. Functions in MIPS 1

Functions in MIPS. Functions in MIPS 1 Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture MIPS Functions July 1, 2014 Review I RISC Design Principles Smaller is faster: 32 registers, fewer instructions Keep it simple: rigid syntax, fixed instruction length MIPS Registers: $s0-$s7,$t0-$t9, $0

More information

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

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont ) Chapter 2 Computer Abstractions and Technology Lesson 4: MIPS (cont ) Logical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left >>> srl Bitwise

More information

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

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions Procedure Calls A procedure of a subroutine is like an agent which needs certain information to perform a

More information

MIPS Procedure Calls. Lecture 6 CS301

MIPS Procedure Calls. Lecture 6 CS301 MIPS Procedure Calls Lecture 6 CS301 Function Call Steps Place parameters in accessible location Transfer control to function Acquire storage for procedure variables Perform calculations in function Place

More information

Compiling Techniques

Compiling Techniques Lecture 10: An Introduction to MIPS assembly 18 October 2016 Table of contents 1 Overview 2 3 Assembly program template.data Data segment: constant and variable definitions go here (including statically

More information

2.2: Bitwise Logical Operations

2.2: Bitwise Logical Operations 2.2: Bitwise Logical Operations Topics: Introduction: logical operations in C/C++ logical operations in MIPS In 256 lecture, we looked at bitwise operations in C/C++ and MIPS. We ll look at some simple

More information

Today. Putting it all together

Today. Putting it all together Today! One complete example To put together the snippets of assembly code we have seen! Functions in MIPS Slides adapted from Josep Torrellas, Craig Zilles, and Howard Huang Putting it all together! Count

More information

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

We will study the MIPS assembly language as an exemplar of the concept. MIPS Assembly Language 1 We will study the MIPS assembly language as an exemplar of the concept. MIPS assembly instructions each consist of a single token specifying the command to be carried out, and

More information

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

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A. 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

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design ECE232: Hardware Organization and Design Lecture 6: Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Procedures have different names in different languages Java:

More information

Computer Systems and Networks

Computer Systems and Networks LECTURE 16: MIPS (11 AND 12) Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) University of the Pacific Deadline Lab 11 is open: DUE 1 st NOV 5 AM Lab 12 is open: DUE 8

More information

MIPS function continued

MIPS function continued 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

More information

MIPS Functions and the Runtime Stack

MIPS Functions and the Runtime Stack MIPS Functions and the Runtime Stack COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline

More information

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

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary Control Instructions Computer Organization Architectures for Embedded Computing Thursday, 26 September 2013 Many slides adapted from: Computer Organization and Design, Patterson & Hennessy 4th Edition,

More information

Control Instructions

Control Instructions Control Instructions Tuesday 22 September 15 Many slides adapted from: and Design, Patterson & Hennessy 5th Edition, 2014, MK and from Prof. Mary Jane Irwin, PSU Summary Previous Class Instruction Set

More information

CSc 256 Midterm 1 Fall 2011

CSc 256 Midterm 1 Fall 2011 CSc 256 Midterm 1 Fall 2011 NAME: Problem 1a: Given the C++ function prototype and variable declarations: int func(int arg0, int *arg1, int *arg2); int *ptr, n, arr[10]; which of the following statements

More information

MIPS Assembly (FuncDons)

MIPS Assembly (FuncDons) ECPE 170 Jeff Shafer University of the Pacific MIPS Assembly (FuncDons) 2 Lab Schedule AcDviDes Monday Discuss: MIPS FuncDons Lab 11 Assignments Due Sunday Apr 14 th Lab 11 due by 11:59pm Wednesday Discuss:

More information

Common Problems on Homework

Common Problems on Homework MIPS Functions Common Problems on Homework 1.3: Convert -3000 ten to binary in 8bit, 16bit, and 32bit Even though it overflows with 8bits, there is plenty of room with 16 and 32 bit. Common Problems on

More information

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

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware Supporting Procedures in Computer Hardware James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy

More information

CS153: Compilers Lecture 8: Compiling Calls

CS153: Compilers Lecture 8: Compiling Calls CS153: Compilers Lecture 8: Compiling Calls Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 2 out Due Thu Oct 4 (7 days) Project 3 out Due Tuesday Oct 9 (12 days) Reminder:

More information

SPIM Procedure Calls

SPIM Procedure Calls SPIM Procedure Calls 22C:60 Jonathan Hall March 29, 2008 1 Motivation We would like to create procedures that are easy to use and easy to read. To this end we will discuss standard conventions as it relates

More information

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls Lecture 5: Procedure Calls Today s topics: Procedure calls and register saving conventions 1 Example Convert to assembly: while (save[i] == k) i += 1; i and k are in $s3 and $s5 and base of array save[]

More information

Lecture 6: Assembly Programs

Lecture 6: Assembly Programs Lecture 6: Assembly Programs Today s topics: Procedures Examples Large constants The compilation process A full example 1 Procedures Local variables, AR, $fp, $sp Scratchpad and saves/restores, $fp Arguments

More information

ECE331: Hardware Organization and Design

ECE331: Hardware Organization and Design ECE331: Hardware Organization and Design Lecture 8: Procedures (cont d), Binary Numbers and Adders Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Review: Procedure Calling Steps

More information

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

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language Instructors: Randy H. Katz David A. PaGerson hgp://inst.eecs.berkeley.edu/~cs61c/sp11 1 2 Machine Interpreta4on

More information

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

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} Subroutines Also called procedures or functions Example C code: int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} // subroutine converts Celsius to kelvin int celtokel(int i) { return (i

More information

Lecture 7: Examples, MARS, Arithmetic

Lecture 7: Examples, MARS, Arithmetic Lecture 7: Examples, MARS, Arithmetic Today s topics: More examples MARS intro Numerical representations 1 Dealing with Characters Instructions are also provided to deal with byte-sized and half-word quantities:

More information

CSc 256 Midterm (green) Fall 2018

CSc 256 Midterm (green) Fall 2018 CSc 256 Midterm (green) Fall 2018 NAME: Problem 1 (5 points): Suppose we are tracing a C/C++ program using a debugger such as gdb. The code showing all function calls looks like this: main() { bat(5);

More information

MIPS Assembly (FuncDons)

MIPS Assembly (FuncDons) ECPE 170 Jeff Shafer University of the Pacific MIPS Assembly (FuncDons) Instructor: Dr. Vivek Pallipuram 2 Lab Schedule This Week AcDviDes Lab work Dme MIPS funcdons MIPS Random Number Generator Lab 10

More information

LAB C Translating Utility Classes

LAB C Translating Utility Classes LAB C Translating Utility Classes Perform the following groups of tasks: LabC1.s 1. Create a directory to hold the files for this lab. 2. Create and run the following two Java classes: public class IntegerMath

More information

CS64 Week 5 Lecture 1. Kyle Dewey

CS64 Week 5 Lecture 1. Kyle Dewey CS64 Week 5 Lecture 1 Kyle Dewey Overview More branches in MIPS Memory in MIPS MIPS Calling Convention More Branches in MIPS else_if.asm nested_if.asm nested_else_if.asm Memory in MIPS Accessing Memory

More information

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

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI. CSCI 402: Computer Architectures Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI Recall Big endian, little endian Memory alignment Unsigned

More information

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015 Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative

More information

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

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10 Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10 Ziad Matni Dept. of Computer Science, UCSB Lecture Outline More on MIPS Calling Convention Functions calling functions

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #8: MIPS Procedures 2005-06-30 CS 61C L08 MIPS Procedures (1) Andy Carle Topic Outline Functions More Logical Operations CS 61C L08

More information

Lecture 7: Procedures

Lecture 7: Procedures Lecture 7: Procedures CSE 30: Computer Organization and Systems Programming Winter 2010 Rajesh Gupta / Ryan Kastner Dept. of Computer Science and Engineering University of California, San Diego Outline

More information

Chapter 2A Instructions: Language of the Computer

Chapter 2A Instructions: Language of the Computer Chapter 2A Instructions: Language of the Computer Copyright 2009 Elsevier, Inc. All rights reserved. Instruction Set The repertoire of instructions of a computer Different computers have different instruction

More information

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8 MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8 Ziad Matni Dept. of Computer Science, UCSB CS 64, Spring 18, Midterm#1 Exam

More information

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

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types 1 Lecture 08 Introduction to the MIPS ISA + Procedure Calls in MIPS Longer instructions = more bits to address registers MIPS Datapath 6 bit opcodes... 2 MIPS Instructions are 32 bits More ways to address

More information

QtSPIM and MARS : MIPS Simulators

QtSPIM and MARS : MIPS Simulators QtSPIM and MARS : MIPS Simulators Learning MIPS & SPIM MIPS assembly is a low-level programming language The best way to learn any programming language is to write code We will get you started by going

More information

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

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions Wen Zong Department of Computer Science and Engineering The Chinese University of Hong Kong wzong@cse.cuhk.edu.hk Overview

More information

SPIM Instruction Set

SPIM Instruction Set SPIM Instruction Set This document gives an overview of the more common instructions used in the SPIM simulator. Overview The SPIM simulator implements the full MIPS instruction set, as well as a large

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-08 In Class Example Handout Part A: J-Type Example: If you look in your book at the syntax for j (an unconditional jump instruction), you see something like: e.g. j addr would seemingly

More information

ECE260: Fundamentals of Computer Engineering

ECE260: Fundamentals of Computer Engineering Supporting Nested Procedures James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy Memory Layout

More information

Implementing Procedure Calls

Implementing Procedure Calls 1 / 39 Implementing Procedure Calls February 18 22, 2013 2 / 39 Outline Intro to procedure calls Caller vs. callee Procedure call basics Calling conventions The stack Interacting with the stack Structure

More information

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE 1 ARCHITECTURE MODEL The basic instruction set of a computer is comprised of sequences of REGISTER TRANSFERS. Example: Add A, B, C Register B # A

More information

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates CS 61C: Great Ideas in Computer Architecture MIPS Instruction Representation II Guest Lecturer: Justin Hsia 2/11/2013 Spring 2013 Lecture #9 1 Review of Last Lecture Simplifying MIPS: Define instructions

More information

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.

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. MIPS Programming This is your crash course in assembler programming; you will teach yourself how to program in assembler for the MIPS processor. You will learn how to use the instruction set summary to

More information

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

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11 Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11 Ziad Matni Dept. of Computer Science, UCSB Administrative Lab 5 due end of day tomorrow Midterm: to

More information

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls Lecture 5: Procedure Calls Today s topics: Memory layout, numbers, control instructions Procedure calls 1 Memory Organization The space allocated on stack by a procedure is termed the activation record

More information

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization CISC 662 Graduate Computer Architecture Lecture 4 - ISA MIPS ISA Michela Taufer http://www.cis.udel.edu/~taufer/courses Powerpoint Lecture Notes from John Hennessy and David Patterson s: Computer Architecture,

More information

MIPS ISA-II: Procedure Calls & Program Assembly

MIPS ISA-II: Procedure Calls & Program Assembly MIPS ISA-II: Procedure Calls & Program Assembly Module Outline Reiew ISA and understand instruction encodings Arithmetic and Logical Instructions Reiew memory organization Memory (data moement) instructions

More information

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H 2.8 and 2.12, and A.5-6 Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University Upcoming agenda PA1 due yesterday PA2 available and discussed during lab section this week

More information

Chapter 2. Instructions:

Chapter 2. Instructions: Chapter 2 1 Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow Very restrictive e.g., MIPS Arithmetic Instructions We ll be working with

More information

EEC 581 Computer Architecture Lecture 1 Review MIPS

EEC 581 Computer Architecture Lecture 1 Review MIPS EEC 581 Computer Architecture Lecture 1 Review MIPS 1 Supercomputing: Suddenly Fancy 2 1 Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control

More information

MIPS Functions and Instruction Formats

MIPS Functions and Instruction Formats MIPS Functions and Instruction Formats 1 The Contract: The MIPS Calling Convention You write functions, your compiler writes functions, other compilers write functions And all your functions call other

More information

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

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f CS2 Fall 28 Exam 2 Name: ) The Logisim TinyProc2 has four instructions, each using 8 bits. The instruction format is DR SR SR2 OpCode with OpCodes of for add, for subtract, and for multiply. Load Immediate

More information

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA CISC 662 Graduate Computer Architecture Lecture 4 - ISA Michela Taufer http://www.cis.udel.edu/~taufer/courses Powerpoint Lecture Notes from John Hennessy and David Patterson s: Computer Architecture,

More information

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

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs Slide Set 2 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section

More information

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

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design Professor Sherief Reda http://scale.engin.brown.edu School of Engineering Brown University Spring 2014 Sources: Computer

More information

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

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 Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than (slt) compares its source registers

More information

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

Implement a C language function that computes the factorial of a number, recursively, according to the following prototype: McGill University Department of Electrical and Computer Engineering ECSE-221 Introduction to Computer Engineering Assignment 5 Assembly and Datapaths Due Date: Monday, November 30 th 2009 at 11:59PM Question

More information

EE 109 Unit 15 Subroutines and Stacks

EE 109 Unit 15 Subroutines and Stacks 1 EE 109 Unit 15 Subroutines and Stacks 2 Program Counter and GPRs (especially $sp, $ra, and $fp) REVIEW OF RELEVANT CONCEPTS 3 Review of Program Counter PC is used to fetch an instruction PC contains

More information

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

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions Representing Instructions Instructions are encoded in binary Called machine code MIPS instructions Encoded as 32-bit instruction words Small number of formats encoding operation code (opcode), register

More information

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley. CS61C Machine Structures Lecture 12 - MIPS Procedures II & Logical Ops 2/13/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L12 MIPS Procedures II / Logical (1)

More information

ECE/CS 314 Fall 2003 Homework 2 Solutions. Question 1

ECE/CS 314 Fall 2003 Homework 2 Solutions. Question 1 ECE/CS 314 Fall 2003 Homework 2 Solutions Question 1 /* Equivalent C code with no loops (goto instead) char* strchr(char* pcstring, char csearchchr) strchr_loop: if (!*pcstring) return 0; if (*pcstring

More information

ECE 30 Introduction to Computer Engineering

ECE 30 Introduction to Computer Engineering ECE 30 Introduction to Computer Engineering Study Problems, Set #4 Spring 2015 Note: To properly study and understand the problems below, it is strongly recommended to implement and run your solution (and/or

More information

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Architecture II. Computer Systems Laboratory Sungkyunkwan University MIPS Instruction ti Set Architecture II Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Making Decisions (1) Conditional operations Branch to a

More information

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

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines EE 352 Unit 6 Stack Frames Recursive Routines Arguments and Return Values MIPS convention is to use certain registers for this task $a0 - $a3 used to pass up to 4 arguments. If more arguments, use the

More information

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei Instruction Set Architecture part 1 (Introduction) Mehran Rezaei Overview Last Lecture s Review Execution Cycle Levels of Computer Languages Stored Program Computer/Instruction Execution Cycle SPIM, a

More information

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

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands Stored Program Concept Instructions: Instructions are bits Programs are stored in memory to be read or written just like data Processor Memory memory for data, programs, compilers, editors, etc. Fetch

More information

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2) ELEC 5200-001/6200-001 Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2) Victor P. Nelson, Professor & Asst. Chair Vishwani D. Agrawal, James J. Danaher Professor Department

More information

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

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2) CSE 202: Computer Organization Lecture-4 Code Translation-2 Memory, Data transfer instructions, Data segment,, Procedures, Stack Shakil M. Khan (adapted from Prof. Roumani) Previously Registers $s0 - $s7,

More information

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

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space Memory Usage A software convention stack

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

CENG3420 Lecture 03 Review

CENG3420 Lecture 03 Review CENG3420 Lecture 03 Review Bei Yu byu@cse.cuhk.edu.hk 2017 Spring 1 / 38 CISC vs. RISC Complex Instruction Set Computer (CISC) Lots of instructions of variable size, very memory optimal, typically less

More information

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats Instructor: Justin Hsia 6/27/2012 Summer 2012 Lecture #7 1 Review of Last Lecture New registers: $a0-$a3, $v0-$v1, $ra, $sp Also: $at,

More information

CSc 256 Midterm 1 Spring 2011

CSc 256 Midterm 1 Spring 2011 CSc 256 Midterm 1 Spring 2011 NAME: Problem1a: GiventheC++functionprototypeandvariabledeclarations: intfunc(intarg0,int*arg1,int*arg2); int*ptr,n,arr[10]; whichofthefollowingstatementswillcausesyntaxerrorsorwarningsabouttype

More information

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

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University

More information

Thomas Polzer Institut für Technische Informatik

Thomas Polzer Institut für Technische Informatik Thomas Polzer tpolzer@ecs.tuwien.ac.at Institut für Technische Informatik Branch to a labeled instruction if a condition is true Otherwise, continue sequentially beq rs, rt, L1 if (rs == rt) branch to

More information

Procedures and Stacks

Procedures and Stacks Procedures and Stacks Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. March 15, 2018 L10-1 Announcements Schedule has shifted due to snow day Quiz 2 is now on Thu 4/12 (one week later)

More information

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

Review of Activation Frames. FP of caller Y X Return value A B C Review of Activation Frames In general, activation frames are organized like this: HI LO Bookkeeping/preserved registers I/O parameters Locals and temps RA of caller FP of caller Y X Return value A B C

More information

Chapter 3. Instructions:

Chapter 3. Instructions: Chapter 3 1 Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow Very restrictive e.g., MIPS Arithmetic Instructions We ll be working with

More information

Assignment 1: Pipelining Implementation at SPIM Simulator

Assignment 1: Pipelining Implementation at SPIM Simulator Assignment 1: Pipelining Implementation at SPIM Simulator Due date: 11/26 23:59 Submission: icampus (Report, Source Code) SPIM is a MIPS processor simulator, designed to run assembly language code for

More information

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

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions Instructors: John Wawrzynek & Vladimir Stojanovic http://inst.eecs.berkeley.edu/~cs61c/fa15 1 Machine Interpretation Levels of Representation/Interpretation

More information

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

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University Calling Conventions See P&H 2.8 and 2.12 Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University Goals for Today Review: Calling Conventions call a routine (i.e. transfer control to

More information

MIPS Assembly Language. Today s Lecture

MIPS Assembly Language. Today s Lecture MIPS Assembly Language Computer Science 104 Lecture 6 Homework #2 Midterm I Feb 22 (in class closed book) Outline Assembly Programming Reading Chapter 2, Appendix B Today s Lecture 2 Review: A Program

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 12 Introduction to MIPS Procedures II, Logical and Shift Ops 2004-09-27 Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia Gotta love

More information

CS 110 Computer Architecture MIPS Instruction Formats

CS 110 Computer Architecture MIPS Instruction Formats CS 110 Computer Architecture MIPS Instruction Formats Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University Slides based

More information

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

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9 Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9 Ziad Matni Dept. of Computer Science, UCSB Lecture Outline More on MIPS Calling Convention Functions calling functions

More information

COMP2611: Computer Organization MIPS function and recursion

COMP2611: Computer Organization MIPS function and recursion COMP2611 Fall2015 COMP2611: Computer Organization MIPS function and recursion Overview 2 You will learn the following in this lab: how to use MIPS functions in a program; the concept of recursion; how

More information