# Solutions for Chapter 2 Exercises

Save this PDF as:

Size: px
Start display at page:

## Transcription

2 2 Solutions for Chapter 2 Exercises 2.10 slt \$t3, \$s5, \$zero # test k < 0 bne \$t3, \$zero, Exit # if so, exit slt \$t3, \$s5, \$t2 # test k < 4 beq \$t3, \$zero, Exit # if not, exit sll \$t1, \$s5, 2 # \$t1 = 4*k add \$t1, \$t1, \$t4 # \$t1 = &JumpTable[k] lw \$t0, 0(\$t1) # \$t0 = JumpTable[k] jr \$t0 # jump register L0: add \$s0, \$s3, \$s4 # k == 0 j Exit # break L1: add \$s0, \$s1, \$s2 # k == 1 j Exit # break L2: sub \$s0, \$s1, \$s2 # k == 2 j Exit # break L3: sub \$s0, \$s3, \$s4 # k == 3 j Exit # break Exit: 2.11 a. if (k==0) f = i + j; else if (k==1) f = g + h; else if (k==2) f = g - h; else if (k==3) f = i - j;

3 Solutions for Chapter 2 Exercises 3 b. bne \$s5, \$0, C1 # branch k!= 0 add \$s0, \$s3, \$s4 # f = i + j j Exit # break C1: addi \$t0, \$s5, -1 # \$t0 = k - 1 bne \$t0, \$0, C2 # branch k!= 1 add \$s0, \$s1, \$s2 # f = g + h j Exit # break C2: addi \$t0, \$s5, -2 # \$t0 = k - 2 bne \$t0, \$0, C3 # branch k!= 2 sub \$s0, \$s1, \$s2 # f = g - h j Exit # break C3: addi \$t0, \$s5, -3 # \$t0 = k - 3 bne \$t0, \$0, Exit # branch k!= 3 sub \$s0, \$s3, \$s4 # f = i - j Exit: c. The MIPS code from the previous problem would yield the following results: (5 arithmetic)1.0 + (1 data transfer)1.4 + (2 conditional branch)1.7 + (2 jump)1.2 = 12.2 cycles while the MIPS code from this problem would yield the following: (4 arithmetic)1.0 + (0 data transfer)1.4 + (4 conditional branch)1.7 + (0 jump)1.2 = 10.8 cycles 2.12 The technique of using jump tables produces MIPS code that is independent of N, and always takes the following number of cycles: (5 arithmetic)1.0 + (1 data transfer)1.4 + (2 conditional branch)1.7 + (2 jump)1.2 = 12.2 cycles However, using chained conditional jumps takes the following number of cycles in a worst-case scenario: (N arithmetic)1.0 + (0 data transfer)1.4 + (N conditional branch)1.7 + (0 jump)1.2 = 2.7N cycles Hence, jump tables are faster for the following condition: N > 12.2/2.7 = 5 case statements

4 4 Solutions for Chapter 2 Exercises 2.13

6 6 Solutions for Chapter 2 Exercises The following is a diagram of the status of the stack: 2.16 # Description: Computes the Fibonacci function using a recursive process. # Function: F(n) = 0, if n = 0; # 1, if n = 1; # F(n 1) + F(n 2), otherwise. # Input: n, which must be a nonnegative integer. # Output: F(n). # Preconditions: none # Instructions: Load and run the program in SPIM, and answer the prompt.

7 Solutions for Chapter 2 Exercises 7 # Algorithm for main program: # print prompt # call fib(read) and print result. # Register usage: # \$a0 = n (passed directly to fib) # \$s1 = f(n).data.align 2 # Data for prompts and output description prmpt1:.asciiz "\n\nthis program computes the Fibonacci function." prmpt2:.asciiz "\nenter value for n: " descr:.asciiz "fib(n) = ".text.align 2.globl start start: # Print the prompts li \$v0, 4 # print_str system service... la \$a0, prmpt1 #... passing address of first prompt li \$v0, 4 # print_str system service... la \$a0, prmpt2 #... passing address of 2nd prompt # Read n and call fib with result li \$v0, 5 # read_int system service move \$a0, \$v0 # \$a0 = n = result of read jal fib # call fib(n) move \$s1, \$v0 # \$s1 = fib(n) # Print result li \$v0, 4 # print_str system service... la \$a0, descr #... passing address of output descriptor li \$v0, 1 # print_int system service... move \$a0, \$s1 #... passing argument fib(n) # Call system exit li \$v0, 10 # Algorithm for Fib(n): # if (n == 0) return 0 # else if (n == 1) return 1 # else return fib(n 1) + fib(n 2). #

9 Solutions for Chapter 2 Exercises # Description: Computes the Fibonacci function using an # iterative process. # Function: F(n) = 0, if n = 0; # 1, if n = 1; # F(n 1) + F(n 2), otherwise. # Input: n, which must be a nonnegative integer. # Output: F(n). # Preconditions: none # Instructions: Load and run the program in SPIM, and answer # the prompt. # # Algorithm for main program: # print prompt # call fib(1, 0, read) and print result. # # Register usage: # \$a2 = n (passed directly to fib) # \$s1 = f(n).data.align 2 # Data for prompts and output description prmpt1:.asciiz "\n\nthis program computes the the Fibonacci function." prmpt2:.asciiz "\nenter value for n: " descr:.asciiz "fib(n) = ".text.align 2.globl start start: # Print the prompts li \$v0, 4 # print_str system service... la \$a0, prmpt1 #... passing address of first prompt li \$v0, 4 # print_str system service... la \$a0, prmpt2 #... passing address of 2nd prompt # Read n and call fib with result li \$v0, 5 move \$a2, \$v0 li \$a1, 0 li \$a0, 1 jal fib move \$s1, \$v0 # read_int system service # \$a2 = n = result of read # \$a1 = fib(0) # \$a0 = fib(1) # call fib(n) # \$s1 = fib(n)

10 10 Solutions for Chapter 2 Exercises # Print result li \$v0, 4 # print_str system service... la \$a0, descr #... passing address of output # descriptor li \$v0, 1 # print_int system service... move \$a0, \$s1 #... passing argument fib(n) # Call system - exit li \$v0, 10 # Algorithm for Fib(a, b, count): # if (count == 0) return b # else return fib(a + b, a, count 1). # # Register usage: # \$a0 = a = fib(n 1) # \$a1 = b = fib(n 2) # \$a2 = count (initially n, finally 0). # \$t1 = temporary a + b fib: bne \$a2, \$zero, fibne0 # if count == 0... move \$v0, \$a1 #... return b jr \$31 fibne0: # Assert: n!= 0 addi \$a2, \$a2, 1 # count = count 1 add \$t1, \$a0, \$a1 # \$t1 = a + b move \$a1, \$a0 # b = a move \$a0, \$t1 # a = a + old b j fib # tail call fib(a+b, a, count 1) 2.18 No solution provided Iris in ASCII: Iris in Unicode: Julie in ASCII: Julie in Unicode: 004A C Figure 2.21 shows decimal values corresponding to ACSII characters. A b y t e i s 8 b i t s

13 Solutions for Chapter 2 Exercises 13 here: bne \$s0, \$s2, skip j there skip: there: add \$s0, \$s0, \$s0 This will work as long as our program does not cross the 256MB address boundary described in the elaboration on page Compilation times and run times will vary widely across machines, but in general you should find that compilation time is greater when compiling with optimizations and that run time is greater for programs that are compiled without optimizations Let I be the number of instructions taken on the unmodified MIPS. This decomposes into 0.42I arithmetic instructions (24% arithmetic and 18% logical), 0.36I data transfer instructions, 0.18I conditional branches, and 0.03I jumps. Using the CPIs given for each instruction class, we get a total of ( ) I cycles; if we call the unmodified machine's cycle time C seconds, then the time taken on the unmodified machine is ( ) I C seconds. Changing some fraction, f (namely 0.25) of the data transfer instructions into the autoincrement or autodecrement version will leave the number of cycles spent on data transfer instructions unchanged. However, each of the 0.36 I f data transfer instructions that are changed corresponds to an arithmetic instruction that can be eliminated. So, there are now only (0.42 (0.36 f)) I arithmetic instructions, and the modified machine, with its cycle time of 1.1 C seconds, will take (( f) ) I 1.1 C seconds to execute. When f is 0.25, the unmodified machine is 2.2% faster than the modified one Code before: lw \$t2, 4(\$s6) # temp reg \$t2 = length of array save Loop: slt \$t0, \$s3, \$zero # temp reg \$t0 = 1 if i < 0 bne \$t0, \$zero, IndexOutOfBounds # if i< 0, goto Error slt \$t0, \$s3, \$t2 # temp reg \$t0 = 0 if i >= length beq \$t0, \$zero, IndexOutOfBounds # if i >= length, goto Error sll \$t1, \$s3, 2 # temp reg \$t1 = 4 * i add \$t1, \$t1, \$s6 # \$t1 = address of save[i] lw \$t0, 8(\$t1) # temp reg \$t0 = save[i] bne \$t0, \$s5, Exit # go to Exit if save[i]!= k addi \$s3, \$s3, 1 # i = i + 1 j Loop Exit:

14 14 Solutions for Chapter 2 Exercises The number of instructions executed over 10 iterations of the loop is = 109. This corresponds to 10 complete iterations of the loop, plus a final pass that goes to Exit from the final bne instruction, plus the initial lw instruction. Optimizing to use at most one branch or jump in the loop in addition to using only at most one branch or jump for out-of-bounds checking yields: Code after: lw \$t2, 4(\$s6) # temp reg \$t2 = length of array save slt \$t0, \$s3, \$zero # temp reg \$t0 = 1 if i < 0 slt \$t3, \$s3, \$t2 # temp reg \$t3 = 0 if i >= length slti \$t3, \$t3, 1 # flip the value of \$t3 or \$t3, \$t3, \$t0 # \$t3 = 1 if i is out of bounds bne \$t3, \$zero, IndexOutOfBounds # if out of bounds, goto Error sll \$t1, \$s3, 2 # tem reg \$t1 = 4 * 1 add \$t1, \$t1, \$s6 # \$t1 = address of save[i] lw \$t0, 8(\$t1) # temp reg \$t0 = save[i] bne \$t0, \$s5, Exit # go to Exit if save[i]!= k Loop: addi \$s3, \$s3, 1 # i = i + 1 slt \$t0, \$s3, \$zero # temp reg \$t0 = 1 if i < 0 slt \$t3, \$s3, \$t2 # temp reg \$t3 = 0 if i >= length slti \$t3, \$t3, 1 # flip the value of \$t3 or \$t3, \$t3, \$t0 # \$t3 = 1 if i is out of bounds bne \$t3, \$zero, IndexOutOfBounds # if out of bounds, goto Error addi \$t1, \$t1, 4 # temp reg \$t1 = address of save[i] lw \$t0, 8(\$t1) # temp reg \$t0 = save[i] beq \$t0, \$s5, Loop # go to Loop if save[i] = k Exit: The number of instructions executed by this new form of the loop is * 9 = 100.

16 16 Solutions for Chapter 2 Exercises 2.50 From Figure 2.44, 39% of all instructions for SPEC2000fp are data access instructions. Thus, for every 100 instructions there are 39 data accesses, yielding a total of 139 memory accesses (1 to read each instruction and 39 to access data). a. The percentage of all memory accesses that are for data = 39/139 = 28%. b. Assuming two-thirds of data transfers are loads, the percentage of all memory accesses that are reads = (100 + (39 2/3))/139 = 91% Effective CPI = Sum of (CPI of instruction type Frequency of execution) The average instruction frequencies for SPEC2000int and SPEC2000fp are 0.47 arithmetic (0.36 arithmetic and 0.11 logical), data transfer, 0.12 conditional branch, jump. Thus, the effective CPI is = Accumulator Instruction Code bytes Data bytes load b # Acc = b; 3 4 add c # Acc += c; 3 4 store a # a = Acc; 3 4 add c # Acc += c; 3 4 store b # Acc = b; 3 4 neg # Acc =- Acc; 1 0 add a # Acc -= b; 3 4 store d # d = Acc; 3 4 Total: Code size is 22 bytes, and memory bandwidth is = 50 bytes. Stack Instruction Code bytes Data bytes push b 3 4 push c 3 4 add 1 0 dup 1 0

17 Solutions for Chapter 2 Exercises 17 Stack Instruction Code bytes Data bytes pop a 3 4 push c 3 4 add 1 0 dup 1 0 pop b 3 4 neg 1 0 push a 3 4 add 1 0 pop d 3 4 Total: Code size is 27 bytes, and memory bandwidth is = 55 bytes. Memory-Memory Instruction Code bytes Data bytes add a, b, c # a=b+c 7 12 add b, a, c # b=a+c 7 12 sub d, a, b # d=a b 7 12 Total: Code size is 21 bytes, and memory bandwidth is = 57 bytes. Load-Store Instruction Code bytes Data bytes load \$1, b # \$1 = b; 4 4 load \$2, c # \$2 = c; 4 4 add \$3, \$1, \$2 # \$3 = \$1 + \$2 3 0 store \$3, a # a = \$3; 4 4 add \$1, \$2, \$3 # \$1 = \$2 + \$3; 3 0 store \$1, b # b = \$1; 4 4 sub \$4, \$3, \$1 # \$4 = \$3 \$1; 3 0 store \$4, d # d = \$4; 4 4 Total: Code size is 29 bytes, and memory bandwidth is = 49 bytes.

21 Solutions for Chapter 2 Exercises 21 skip: bc inner, \$ctr!=0 # j--, loop if j!=0 slt \$t2, \$s0, \$v0 # \$t2 = 0 if x >= freq bne \$t2, \$zero, next # skip freq = x if add \$v0, \$s0, \$zero # freq = x next: addi \$t0, \$t0, 4 # i++ bne \$t0, \$t8, outer # loop if i!= xor \$s0, \$s0, \$s1 xor \$s1, \$s0, \$s1 xor \$s0, \$s0, \$s1

### Part II: Solutions Guide

Part II: Solutions Guide 52 Instructors Manual for Computer Organization and Design 1 Solutions 1.1 q 1.2 u 1.3 f 1.4 a 1.5 c 1.6 d 1.7 i 1.8 k 1.9 j 1.10 o 1.11 w 1.12 p 1.13 n 1.14 r 1.15 y 1.16 s 1.17

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

Machine Language Instructions Introduction Instructions Words of a language understood by machine Instruction set Vocabulary of the machine Current goal: to relate a high level language to instruction

### 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

### 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

### Chapter loop: lw \$v1, 0(\$a0) addi \$v0, \$v0, 1 sw \$v1, 0(\$a1) addi \$a0, \$a0, 1 addi \$a1, \$a1, 1 bne \$v1, \$zero, loop

Chapter 3 3.7 loop: lw \$v1, 0(\$a0) addi \$v0, \$v0, 1 sw \$v1, 0(\$a1) addi \$a0, \$a0, 1 addi \$a1, \$a1, 1 bne \$v1, \$zero, loop Instructions Format OP rs rt Imm lw \$v1, 0(\$a0) I 35 4 3 0 addi \$v0, \$v0, 1 I 8

### Patterson PII. Solutions

Patterson-1610874 978-0-12-407726-3 PII 2 Solutions Chapter 2 Solutions S-3 2.1 addi f, h, -5 (note, no subi) add f, f, g 2.2 f = g + h + i 2.3 sub \$t0, \$s3, \$s4 add \$t0, \$s6, \$t0 lw \$t1, 16(\$t0) sw \$t1,

### Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Computer Science 324 Computer Architecture Mount Holyoke College Fall 2009 Topic Notes: MIPS Instruction Set Architecture vonneumann Architecture Modern computers use the vonneumann architecture. Idea:

### MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

MIPS ISA and MIPS Assembly CS301 Prof. Szajda Administrative HW #2 due Wednesday (9/11) at 5pm Lab #2 due Friday (9/13) 1:30pm Read Appendix B5, B6, B.9 and Chapter 2.5-2.9 (if you have not already done

### Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Assembly Language Programming CPSC 252 Computer Organization Ellen Walker, Hiram College Instruction Set Design Complex and powerful enough to enable any computation Simplicity of equipment MIPS Microprocessor

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

Computer Organization MIPS Architecture Department of Computer Science Missouri University of Science & Technology hurson@mst.edu Computer Organization Note, this unit will be covered in three lectures.

### MIPS%Assembly% E155%

MIPS%Assembly% E155% Outline MIPS Architecture ISA Instruction types Machine codes Procedure call Stack 2 The MIPS Register Set Name Register Number Usage \$0 0 the constant value 0 \$at 1 assembler temporary

### Lecture 6 Decision + Shift + I/O

Lecture 6 Decision + Shift + I/O Instructions so far MIPS C Program add, sub, addi, multi, div lw \$t0,12(\$s0) sw \$t0, 12(\$s0) beq \$s0, \$s1, L1 bne \$s0, \$s1, L1 j L1 (unconditional branch) slt reg1,reg2,reg3

### 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

### 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

### 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

### Chapter 2. Instruction Set Architecture (ISA)

Chapter 2 Instruction Set Architecture (ISA) MIPS arithmetic Design Principle: simplicity favors regularity. Why? Of course this complicates some things... C code: A = B + C + D; E = F - A; MIPS code:

### ECE232: Hardware Organization and Design. Computer Organization - Previously covered

ECE232: Hardware Organization and Design Part 6: MIPS Instructions II http://www.ecs.umass.edu/ece/ece232/ Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Computer Organization

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

Instructions: This is a closed book, closed note exam. Calculators are not permitted. If you have a question, raise your hand and I will come to you. Please work the exam in pencil and do not separate

### MIPS Reference Guide

MIPS Reference Guide Free at PushingButtons.net 2 Table of Contents I. Data Registers 3 II. Instruction Register Formats 4 III. MIPS Instruction Set 5 IV. MIPS Instruction Set (Extended) 6 V. SPIM Programming

### Shift and Rotate Instructions

Shift and Rotate Instructions Shift and rotate instructions facilitate manipulations of data (that is, modifying part of a 32-bit data word). Such operations might include: Re-arrangement of bytes in a

### All instructions have 3 operands Operand order is fixed (destination first)

Instruction Set Architecture for MIPS Processors Overview Dr. Arjan Durresi Louisiana State University Baton Rouge, LA 70803 durresi@csc.lsu.edu These slides are available at: http://www.csc.lsu.edu/~durresi/_07/

### 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

### 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

### Announcements. EE108B Lecture MIPS Assembly Language III. MIPS Machine Instruction Review: Instruction Format Summary

Announcements EE108B Lecture MIPS Assembly Language III Christos Kozyrakis Stanford University http://eeclass.stanford.edu/ee108b PA1 available, due on Thursday 2/8 Work on you own (no groups) Homework

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

Instructions: ti Language of the Computer Rui Wang, Assistant professor Dept. of Information and Communication Tongji University it Email: ruiwang@tongji.edu.cn Computer Hierarchy Levels Language understood

### 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

### 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

### Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

Today s topics CS/COE1541: Introduction to Computer Architecture MIPS operations and operands MIPS registers Memory view Instruction encoding A Review of MIPS ISA Sangyeun Cho Arithmetic operations Logic

### CS3350B Computer Architecture

CS3350B Computer Architecture Winter 2015 Lecture 4.1: MIPS ISA: Introduction Marc Moreno Maza www.csd.uwo.ca/courses/cs3350b [Adapted d from lectures on Computer Organization and Design, Patterson & Hennessy,

### Mips Code Examples Peter Rounce

Mips Code Examples Peter Rounce P.Rounce@cs.ucl.ac.uk Some C Examples Assignment : int j = 10 ; // space must be allocated to variable j Possibility 1: j is stored in a register, i.e. register \$2 then

### Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Computer Science 324 Computer Architecture Mount Holyoke College Fall 2007 Topic Notes: MIPS Instruction Set Architecture vonneumann Architecture Modern computers use the vonneumann architecture. Idea:

### MIPS Assembly Programming

COMP 212 Computer Organization & Architecture COMP 212 Fall 2008 Lecture 8 Cache & Disk System Review MIPS Assembly Programming Comp 212 Computer Org & Arch 1 Z. Li, 2008 Comp 212 Computer Org & Arch 2

### Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

CS 61C Faux Midterm Review (1) CS61C Machine Structures Faux Midterm Review 2002-09-29 Jaein Jeong Cheng Tien Ee www-inst.eecs.berkeley.edu/~cs61c/ Numbers: positional notation Number Base B B symbols

### CS3350B Computer Architecture MIPS Procedures and Compilation

CS3350B Computer Architecture MIPS Procedures and Compilation Marc Moreno Maza http://www.csd.uwo.ca/~moreno/cs3350_moreno/index.html Department of Computer Science University of Western Ontario, Canada

### HW2 solutions You did this for Lab sbn temp, temp,.+1 # temp = 0; sbn temp, b,.+1 # temp = -b; sbn a, temp,.+1 # a = a (-b) = a + b;

HW2 solutions 3.10 Pseuodinstructions What is accomplished Minimum sequence of Mips Move \$t5, \$t3 \$t5=\$t3 Add \$t5, \$t3, \$0 Clear \$t5 \$t5=0 Xor \$t5, \$t5, \$t5 Li \$t5, small \$t5=small Addi \$t5, \$0, small

### ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

Instructions: This is a closed book, closed note exam. Calculators are not permitted. If you have a question, raise your hand and I will come to you. Please work the exam in pencil and do not separate

### Grading: 3 pts each part. If answer is correct but uses more instructions, 1 pt off. Wrong answer 3pts off.

Department of Electrical and Computer Engineering University of Wisconsin Madison ECE 552 Introductions to Computer Architecture Homework #2 (Suggested Solution) 1. (10 points) MIPS and C program translations

### Field 6-Bit Op Code rs Field rt Field 16-bit Immediate field

Introduction to MIPS Instruction Set Architecture The MIPS used by SPIM is a 32-bit reduced instruction set architecture with 32 integer and 32 floating point registers. Other characteristics are as follows:

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

Problem 1 (3 parts, 30 points) Code Fragments Part A (5 points) Write a MIPS code fragment that branches to label Target when register \$1 is less than or equal to register \$2. You may use only two instructions.

### MIPS (SPIM) Assembler Syntax

MIPS (SPIM) Assembler Syntax Comments begin with # Everything from # to the end of the line is ignored Identifiers are a sequence of alphanumeric characters, underbars (_), and dots () that do not begin

### Lecture 14: Recap. Today s topics: Recap for mid-term No electronic devices in midterm

Lecture 14: Recap Today s topics: Recap for mid-term No electronic devices in midterm 1 Modern Trends Historical contributions to performance: Better processes (faster devices) ~20% Better circuits/pipelines

### Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

CS61C L8 Introduction to MIPS : Decisions II & Procedures I (1) Instructor Paul Pearce inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures http://www.xkcd.org/627/ Lecture 8 Decisions & and Introduction

### Supplement for MIPS (Section 4.14 of the textbook)

Supplement for MIPS (Section 44 of the textbook) Section 44 does a good job emphasizing that MARIE is a toy architecture that lacks key feature of real-world computer architectures Most noticable, MARIE

### Forecast. Instructions (354 Review) Basics. Basics. Instruction set architecture (ISA) is its vocabulary. Instructions are the words of a computer

Instructions (354 Review) Forecast Instructions are the words of a computer Instruction set architecture (ISA) is its vocabulary With a few other things, this defines the interface of computers But implementations

### Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

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

### 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

### ECE 2035 A Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 14 December 2016

Instructions: This is a closed book, closed note exam. Calculators are not permitted. If you have a question, raise your hand and I will come to you. Please work the exam in pencil and do not separate

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

Part Part Part What if we need to call a function inside of a function? Will this work? int twofun(int a, int b) { int res; res = addfun(a, b) a / ; return res; } twofun: addi \$sp, \$sp, -4 sw \$s0, 0(\$sp)

### ECE Exam I February 19 th, :00 pm 4:25pm

ECE 3056 Exam I February 19 th, 2015 3:00 pm 4:25pm 1. The exam is closed, notes, closed text, and no calculators. 2. The Georgia Tech Honor Code governs this examination. 3. There are 4 questions and

Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection L5 Addressing Modes 1 Assembly Exercise Let s write some assembly language programs Program #1: Write a function

### CS 4200/5200 Computer Architecture I

CS 4200/5200 Computer Architecture I MIPS Instruction Set Architecture Dr. Xiaobo Zhou Department of Computer Science CS420/520 Lec3.1 UC. Colorado Springs Adapted from UCB97 & UCB03 Review: Organizational

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

The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

### Procedure Call Instructions

Procedure Calling Steps required 1. Place parameters in registers x10 to x17 2. Transfer control to procedure 3. Acquire storage for procedure 4. Perform procedure s operations 5. Place result in register

### 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

### Midterm. CS64 Spring Midterm Exam

Midterm LAST NAME FIRST NAME PERM Number Instructions Please turn off all pagers, cell phones and beepers. Remove all hats & headphones. Place your backpacks, laptops and jackets at the front. Sit in every

### The Program Counter. QtSPIM Register Display (Fixed-Point Registers) Erik Jonsson School of Engineering and Computer Science

The Program Counter PC = 400064 EPC = 0 Cause = 0 BadVAddr = 0 Status = 3000ff10 1 The program counter is a register that always contains the memory address of the next instruction (i.e., the instruction

### CO Computer Architecture and Programming Languages CAPL. Lecture 13 & 14

CO20-320241 Computer Architecture and Programming Languages CAPL Lecture 13 & 14 Dr. Kinga Lipskoch Fall 2017 Frame Pointer (1) The stack is also used to store variables that are local to function, but

### 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)

### Concocting an Instruction Set

Concocting an Instruction Set Nerd Chef at work. move flour,bowl add milk,bowl add egg,bowl move bowl,mixer rotate mixer... Read: Chapter 2.1-2.7 L03 Instruction Set 1 A General-Purpose Computer The von

### MIPS Instruction Format

MIPS Instruction Format MIPS uses a 32-bit fixed-length instruction format. only three different instruction word formats: There are Register format Op-code Rs Rt Rd Function code 000000 sssss ttttt ddddd

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #9: MIPS Procedures 2006-07-11 CS 61C L09 MIPS Procedures (1) Andy Carle C functions main() { int i,j,k,m;... i = mult(j,k);... m =

### Lecture 3: The Instruction Set Architecture (cont.)

Lecture 3: The Instruction Set Architecture (cont.) COS / ELE 375 Computer Architecture and Organization Princeton University Fall 2015 Prof. David August 1 Review: Instructions Computers process information

### CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

CSIS1120A 10. Instruction Set & Addressing Mode CSIS1120A 10. Instruction Set & Addressing Mode 1 Elements of a Machine Instruction Operation Code specifies the operation to be performed, e.g. ADD, SUB

### 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

### Recitation 5: Recitation 5: MIPS Interrupts and Floating Point

Float Operations We will write a full program to read in two floats, perform an add, subtract, multiply, and divide, printing out each result Let s attempt in C (using blocking IO) Float Operations We

### A General-Purpose Computer The von Neumann Model. Concocting an Instruction Set. Meaning of an Instruction. Anatomy of an Instruction

page 1 Concocting an Instruction Set Nerd Chef at work. move flour,bowl add milk,bowl add egg,bowl move bowl,mixer rotate mixer... A General-Purpose Computer The von Neumann Model Many architectural approaches

### 8*4 + 4 = 36 each int is 4 bytes

CS 61CL (Clancy) Solutions and grading standards for exam 1 Spring 2009 169 students took the exam. The average score was 43.6; the median was 46. Scores ranged from 1 to 59. There were 89 scores between

### 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

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

Problem 1 (20 points) Compilation Perform at least five standard compiler optimizations on the following C code fragment by writing the optimized version (in C) to the right. Assume f is a pure function

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

Function Calls Tom Kelliher, CS 220 Oct. 24, 2011 1 Administrivia Announcements Assignment SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how. From Last Time Outline 1.

### 2 Solutions. Solution 2.1. Solution a. sub f, g, h. b. addi f, h, 5 (note, no subi) add f, f, g. a. 1. b.

2 Solutions Solution 2.1 2.1.1 a. sub f, g, h b. addi f, h, 5 (note, no subi) add f, f, g 2.1.2 a. 1 b. 2 2.1.3 a. 1 b. 0 2.1.4 a. f = f + 4 b. f = g + h + i 2.1.5 a. 5 b. 9 Solution 2.2 2.2.1 a. sub f,

### Lab 4 : MIPS Function Calls

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

### Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008 MIPS Intro II Lect 10 Feb 15, 2008 Adapted from slides developed for: Mary J. Irwin PSU CSE331 Dave Patterson s UCB CS152 M230 L10.1

### Programming with QtSpim: A User s Manual

Programming with QtSpim: A User s Manual John McCranie November 15, 2013 1 1 Introduction to SPIM Spim is a self-contained simulator that runs MIPS32 programs. It utilizes the instruction set originally

### CSCE 212: FINAL EXAM Spring 2009

CSCE 212: FINAL EXAM Spring 2009 Name (please print): Total points: /120 Instructions This is a CLOSED BOOK and CLOSED NOTES exam. However, you may use calculators, scratch paper, and the green MIPS reference

### Lecture Topics. Branch Condition Options. Branch Conditions ECE 486/586. Computer Architecture. Lecture # 8. Instruction Set Principles.

ECE 486/586 Computer Architecture Lecture # 8 Spring 2015 Portland State University Instruction Set Principles MIPS Control flow instructions Dealing with constants IA-32 Fallacies and Pitfalls Reference:

### Chapter 2. Instructions: Language of the Computer

Chapter 2 Instructions: Language g of the Computer Outlines Introduction to MIPS machine Operations of the Computer HW Operands of the Computer HW Representing instructions in the Computer Logical Operations

### 4. What is the average CPI of a 1.4 GHz machine that executes 12.5 million instructions in 12 seconds?

Chapter 4: Assessing and Understanding Performance 1. Define response (execution) time. 2. Define throughput. 3. Describe why using the clock rate of a processor is a bad way to measure performance. Provide

### Compilers and computer architecture: A realistic compiler to MIPS

1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

### UCB CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 Introduction to MIPS Procedures I Sr Lecturer SOE Dan Garcia 2014-02-14 If cars broadcast their speeds to other vehicles (and the

### Architecture I. Computer Systems Laboratory Sungkyunkwan University

MIPS Instruction ti Set Architecture I Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Architecture (1) the attributes of a system as seen by the

### Week 10: Assembly Programming

Week 10: Assembly Programming Arithmetic instructions Instruction Opcode/Function Syntax Operation add 100000 \$d, \$s, \$t \$d = \$s + \$t addu 100001 \$d, \$s, \$t \$d = \$s + \$t addi 001000 \$t, \$s, i \$t = \$s +

### MIPS PROJECT INSTRUCTION SET and FORMAT

ECE 312: Semester Project MIPS PROJECT INSTRUCTION SET FORMAT This is a description of the required MIPS instruction set, their meanings, syntax, semantics, bit encodings. The syntax given for each instruction

### Problem maximum score 1 35pts 2 22pts 3 23pts 4 15pts Total 95pts

University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Sciences CS61c Summer 2001 Woojin Yu Midterm Exam This is a closed-book exam. No calculators

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

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats Instructors: Vladimir Stojanovic and Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/sp16 1 Machine Interpretation Levels of Representation/Interpretation

### ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 29 April 2015

Problem 1 (20 points) Optimization Perform at least five standard compiler optimizations on the following C code fragment by writing the optimized version (in C) to the right. Assume f and g are pure functions

### A Processor. Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University. See: P&H Chapter , 4.1-3

A Processor Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University See: P&H Chapter 2.16-20, 4.1-3 Let s build a MIPS CPU but using Harvard architecture Basic Computer System Registers ALU

### CPS311 - COMPUTER ORGANIZATION. A bit of history

CPS311 - COMPUTER ORGANIZATION A Brief Introduction to the MIPS Architecture A bit of history The MIPS architecture grows out of an early 1980's research project at Stanford University. In 1984, MIPS computer

### COMP 303 MIPS Processor Design Project 3: Simple Execution Loop

COMP 303 MIPS Processor Design Project 3: Simple Execution Loop Due date: November 20, 23:59 Overview: In the first three projects for COMP 303, you will design and implement a subset of the MIPS32 architecture

### MIPS Hello World. MIPS Assembly 1. # PROGRAM: Hello, World! # Data declaration section. out_string:.asciiz "\nhello, World!\n"

MIPS Hello World MIPS Assembly 1 # PROGRAM: Hello, World!.data # Data declaration section out_string:.asciiz "\nhello, World!\n".text # Assembly language instructions main: # Start of code section li \$v0,

### Instruction Set Architecture (Contd)

Instruction Set Architecture (Contd) Lecture 3 (Chapter 2) EEC170 FQ 2005 Review: MIPS Architecture Good example of RISC processor: Reduced Instruction-Set Computer RISC really a misnomer: architecture

### COMP2421 COMPUTER ORGANZATION. Lab 3

Lab 3 Objectives: This lab shows you some basic techniques and syntax to write a MIPS program. Syntax includes system calls, load address instruction, load integer instruction, and arithmetic instructions

### EE108B Lecture 3. MIPS Assembly Language II

EE108B Lecture 3 MIPS Assembly Language II Christos Kozyrakis Stanford University http://eeclass.stanford.edu/ee108b 1 Announcements Urgent: sign up at EEclass and say if you are taking 3 or 4 units Homework

### 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

### MIPS Instruction Reference

Page 1 of 9 MIPS Instruction Reference This is a description of the MIPS instruction set, their meanings, syntax, semantics, and bit encodings. The syntax given for each instruction refers to the assembly

### CS 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and RISC-V Instruction Set Architecture

CS 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and RISC-V Instruction Set Architecture Instructors: Krste Asanović & Randy H. Katz http://inst.eecs.berkeley.edu/~cs61c 9/7/17

### Computer Systems Architecture

Computer Systems Architecture http://cs.nott.ac.uk/ txa/g51csa/ Thorsten Altenkirch and Liyang Hu School of Computer Science and IT University of Nottingham Lecture 05: Comparisons, Loops and Bitwise Operations

### MIPS Processor Overview

MIPS Processor Cptr280 Dr Curtis Nelson MIPS Processor Overview Hardware Design philosophy Architecture Software Assembly language program structure QTSpim simulator Example programs 1 MIPS Processor Power