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

Similar documents
Computer Architecture I Midterm I

Midterm. CS64 Spring Midterm Exam

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

Lecture 5: Procedure Calls

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

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

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

ECE 30 Introduction to Computer Engineering

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

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

CS 61c: Great Ideas in Computer Architecture

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

MIPS%Assembly% E155%

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

Chapter 2A Instructions: Language of the Computer

CS61c MIDTERM EXAM: 3/17/99

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

Lecture 4: MIPS Instruction Set

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

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

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

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First)

University of California, Berkeley College of Engineering

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

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

UCB CS61C : Machine Structures

Flow of Control -- Conditional branch instructions

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

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

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

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

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

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

COMPUTER ORGANIZATION AND DESIGN

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

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

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Mips Code Examples Peter Rounce

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

Reduced Instruction Set Computer (RISC)

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

Examples of branch instructions

Unsigned Binary Integers

Unsigned Binary Integers

University of California, Berkeley College of Engineering

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

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Instructions: Language of the Computer

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

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

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

CS3350B Computer Architecture MIPS Instruction Representation

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

ECE232: Hardware Organization and Design

CS 351 Exam 2 Mon. 11/2/2015

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Three 10 April 2013

CDA3101 Project 1: MIPS Assembler Due 2/5. I. Purpose

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

Reduced Instruction Set Computer (RISC)

Winter 2003 MID-SESSION TEST Monday, March 10 6:30 to 8:00pm

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

Q1: /30 Q2: /25 Q3: /45. Total: /100

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

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

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

MIPS Functions and Instruction Formats

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

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

CS/COE1541: Introduction to Computer Architecture

CS61C : Machine Structures

MIPS Instruction Reference

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Lectures 3-4: MIPS instructions

Computer Architecture

Chapter 2. Instructions:

MIPS PROJECT INSTRUCTION SET and FORMAT

Assembly Programming

MIPS Assembly Programming

Computer Organization MIPS ISA

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

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

The MIPS Instruction Set Architecture

ECE 15B Computer Organization Spring 2010

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

Computer Architecture. The Language of the Machine

Computer Architecture

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

CENG3420 Lecture 03 Review

Winter 2012 MID-SESSION TEST Tuesday, March 6 6:30pm to 8:15pm. Please do not write your U of C ID number on this cover page.

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

Computer Architecture. MIPS Instruction Set Architecture

CS222: MIPS Instruction Set

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 22 September Your Name (please print clearly) Signed.

CS 110 Computer Architecture MIPS Instruction Formats

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

Transcription:

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 please. You have 1 hour. Each question is marked with its number of points. This exam booklet should have 8 printed pages. Check to make sure that you have all the pages. Put your name and login neatly on each page. Show your answers in the space provided for them. Write neatly and be well organized. If you need extra space to work out your answers, you may use the back of previous questions. However, only the answers appearing in the proper answer space will be graded. Good luck! Problem maximum score 1 35pts 2 22pts 3 23pts 4 15pts Total 95pts 1

MIPS instructions Important please note: The MIPS instructions shown in this table are the ones that you must use on the entire exam. Do not use any instructions that are not in this table. If you use any instructions not listed below, you will lose points. The columns under format show the bit fields of the instructions. The number in the parentheses following each name is the number of bits in that field. In the table, PC refers to the program counter. You may carefully tear this page from your exam booklet for easy reference. name Format Syntax meaning Op(6) rs(5) rt(5) rd(5) Shamt(5) func(6) add 0 0 32 add rd,rs,rt rd = rs + rt sub 0 0 34 sub rd,rs,rt rd = rs - rt and 0 0 36 and rd,rs,rt rd = rs AND rt or 0 0 37 or rd,rs,rt rd = rs OR rt sll 0 0 0 sll rd,rt,shamt rd = logical shift rt left shamt bits srl 0 0 2 srl rd,rt,shamt rd = logical shift rt right shamt bits slt 0 0 42 slt rd,rs,rt if rs<rt set rd=1 else rd=0 jr 0 0 0 0 8 jr rs PC=rs Op(6) rs(5) rt(5) immediate(16) addi 8 addi rt,rs,immed rt = rs + immed andi 12 andi rt,rs,immed rt = rs AND immed ori 13 ori rt,rs,immed rt = rs OR immed lw 35 lw rt,immed(rd) rt = MEMORY[rd+immed] sw 43 sw rt,immed(rd) MEMORY[rd+immed] = rt lui 15 Lui rt,immed rt = immed shifted left 16 bits beq 4 beq rs,rt,label branch if equal bne 5 Bne rs,rt,label branch if not equal Op(6) target address(26) j 2 j label jump jal 3 Jal label jump and link 2

1. Rewrite the following C source code using the MIPS Assembly instructions. Please follow the register conventions mentioned in class. ONLY USE THE INSTRUCTIONS ON PAGE 2. int func1(int *a) int *temp; if (*a) temp=a; a++; return(*temp+func1(a)); else return 0; The following solution keeps closely to the C code: func1: # $s0 is a, $s1 is temp addi $sp, $sp, -16 # allocate stack sw $s0, 0($sp) # save some registers sw $s1, 4($sp) sw $ra, 8($sp) # save the return address add $s0, $0, $a0 # make s0 hold the value of a lw $t0, 0($s0) # derefrence a, t0 = *a beq $t0, $0, else # if (*a == 0) then do the else addi $s1, $0, $s0 # temp = a addi $s0, $s0, 4 # a++, a is an int pointer lw $t1, 0($s1) # dereference temp sw $t1, 12($sp) # save (*temp) so it survives the jal add $a0, $s0, $0 # put a as arg to func1 jal func1 # call func1(a) lw $t1, 12($sp) # bring back the value of (*temp) add $v0, $t1, $v0 # return = (*temp)+func(a) j fin else: add $v0, $0, $0 # return 0 fin: lw $s0, 0($sp) # save some registers lw $s1, 4($sp) lw $ra, 8($sp) # save the return address addi $sp, $sp, 16 # allocate stack jr $ra 3 points for each of the following (partial credit was given): 1) allocating space on the stack and storing 6) restoring temp if needed $ra and other registers you needed 2) dereferencing a correctly 7) dereferencing temp, if necessary 3) implementing the branch correctly 8) correct resulting value in $v0 4) implementing the pointer arithmetic 9) storing 0 in $v0 for the else case correctly 5) setting $a0 correctly 10) restore stack, $ra, other registers 3

2. In the following, some of the statements are incorrect or illegal; cross out any such bad statements. Show in the spaces provided what the remaining print statements will print when the program is executed. Briefly state why the illegal statements are wrong. int main() char a = A, ur[] = "ORDINALS", b = C ; char *alpha = &a, *beta = alpha, *gamma = ur; char **aleph = &alpha, **beth = &beta printf("%c\n", *gamma); The letter O. printf("%s\n", b); printf("%c\n", *beta); printf("%c\n", alpha); alpha = ur + 1; printf("%c\n", *(ur + 3)); printf("%s\n", &ur[1]); Ur = alpha; printf("%c\n", *beta); beth = &(alpha); If( (*aleph)[1] == (*beth)[1] ) printf("ch is true.\n"); Else printf("ch is false.\n"); Incorrect, b isn t a char*. The letter A. Incorrect, alpha isn t a char. Correct, but nothing printed. The letter I. The string RDINALS. Illegal, can t assign to an array. The letter A. Correct, but nothing printed. This case is printed. return 0; Each part was worth 2 points. We tried not to let wrong answers cascade, but the way we did this is a little complicated to explain. 4

3. Convert this MIPS machine code into MAL (MIPS Assembly Language) instructions. Your final answers should use the register names, not the numbers (i.e. $t0, not $8) Also, values which represent addresses (if any) should be converted into the full 32 bit address. Some space is left below each binary encoding to let you write down your work. However, you will only receive credit for what you write in the answer box. Address: 0x10001A00 0x10001A04 0X10001A08 0X10001A0C 0X10001A10 0X10001A14 0X10001A18 Instructions: 001000 11101 11101 11111 11111 111100 101011 11101 11111 00000 00000 000000 000011 00000 10000 01000 00000 000111 000000 00010 00010 00010 00000 100000 100011 11101 11111 00000 00000 000000 001000 11101 11101 00000 00000 000100 000000 11111 00000 00000 00000 001000 Box: Address: 0x10001A00 addi $sp, $sp, -4 0x10001A04 sw $ra, 0($sp) 0X10001A08 jal 0x1041001C 0X10001A0C add $v0, $v0, $v0 0X10001A10 lw $ra, 0($sp) 0X10001A14 addi $sp, $sp, 4 0X10001A18 jr $ra Instructions: Each of the instructions were worth 3 points, except for the jal, which was worth 5. You lost 1 point for each mistake you made on an instruction, until you lost all the points associated with that instruction. 5

4. Short Questions (5 Parts) a. Assume an 8-bit two s complement machine on which all operations are performed on 8-bit registers. the results of the following operations in hexadecimal. Assume that subtraction is done with SUBU and addition is done with ADDU. (i) 43 (hex) - 4A (hex) ---------- F9 (hex) (ii) 82 (hex) + AB (hex) ---------- 2D (hex) Each part was worth 2 points. 12D for part (ii) got you 1 point, unless you said something about a trap not occurring on ADDU, or overflow not being detected, or something like that. For both parts, we also accepted correct answers in binary or decimal. b. List the two values that can change on execution of the jal instruction. The values we were looking for were $ra and PC. Some people put down $v0, $a0, or something like that; these might be changed in preparation for a jal or before returning from a jal, but not directly by the jal itself. These were 1 point each, no exceptions. c. Describe how the calculation of the target address for the beq instruction is different from that of the j instruction. We accepted answers mentioning either that 1) The beq instruction is PC-relative, while the j instruction is absolute (well, it is a little bit PC-relative because it takes the top 4 bits of the PC, but that s beside the point). 2) The beq takes the 16-bit immediate and adds it (after multiplying by 4) to the PC, whereas the j takes the 26 bits in the instruction, multiplies by 4, and takes the top 4 bits of the PC for its own top 4 bits. We were generous with partial credit on this question; if you had some idea, we gave you a point, but you had to be fairly detailed to get full credit. Yes, it was a little subjective. 6

d. What output would typically be seen from running the following (correct) program on a 32-bit machine, such as the MIPS machine we are studying? The sizeof operator determines (at compile-time) the size (in bytes) of the type yielded by its argument. #include <stdio.h> int main (void) char a[] = "foobar", b[15] = "baz", *c = "garply"; int d[5] = 1, 2, 4, 8, 16 ; void bar (char*, int[]); printf("%d\n", sizeof(a)); printf("%d\n", sizeof(b)); bar(c, d); return 0; void bar (char *c, int d[]) printf("%d\n", sizeof(c)); printf("%d\n", sizeof(d)); List, in order from first to last, the four values that appear. 1) 7. Since a is an array initialized without specific bounds, the initializer determines the array size. 6 for the letters in "foobar", plus 1 for the null-terminating character. 2) 15. b is a char array, with 15 elements, so it takes 15 bytes of storage. 3) 4. c is a pointer, and the typical size of a pointer on a 32-bit machine is 32 bits, or 4 bytes. 4) 4. d is actually a pointer to char, not an array, since arrays cannot be passed by value in C. This was worth 4 points, 1 point for each correct printed value. 7

e. Write an equivalent, one-line statement in C. Do not use any bit operators or the comma operator (no &,, ^, ~, <<, >>, or commas). Assume the C variable unsigned int x is stored in register $t0. andi $t0, $t0, 63 We expected you to write something like x = x % 64; or similar. This is because we are basically masking out all bits past the 6 th lowest (which represents 2 6 ). But since these bits all add on values that are multiples of 64, they would have left a remainder of 0 when divided by 64. A lot of people wrote something like x = x + 63. Perhaps you misread andi as addi (no comment), or you had an OR operation in mind, although that wouldn t quite have worked either. This was worth 2 points. If you mentioned mod or modulo (but you didn t write the %), or if you had a % but the wrong modulus, we gave you a point if you were close; e.g., if you said x = x % 63. Relaxation Question: What is your favorite movie? The correct answer was Forrest Gump, but since this question wasn t worth any points, it didn t much matter what you put. 8