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

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

ECE 2035 Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 1 May 2013

ECE 30 Introduction to Computer Engineering

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

Lecture 5: Procedure Calls

EE 361 University of Hawaii Fall

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

MIPS function continued

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

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

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

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

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

CSE Lecture In Class Example Handout

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

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

Lecture 5: Procedure Calls

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

ECE260: Fundamentals of Computer Engineering

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

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

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

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

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

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

bits 5..0 the sub-function of opcode 0, 32 for the add instruction

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

ECE232: Hardware Organization and Design

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

Control Instructions

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

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

Lecture 6: Assembly Programs

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

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

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

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

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

Lec 10: Assembler. Announcements

CSE Lecture In Class Example Handout

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

MIPS%Assembly% E155%

Chapter 2A Instructions: Language of the Computer

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

Mips Code Examples Peter Rounce

Instructions: Assembly Language

CS 61c: Great Ideas in Computer Architecture

Thomas Polzer Institut für Technische Informatik

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

Chapter 2. Instructions:

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

COMP2611: Computer Organization MIPS function and recursion

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

Solutions for Chapter 2 Exercises

MIPS Functions and the Runtime Stack

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

MIPS Assembly Language Programming

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

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.

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

MIPS Functions and Instruction Formats

Course Administration

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

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

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

Chapter 2: Instructions:

CS 316: Procedure Calls/Pipelining

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

Machine Organization & Assembly Language

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

Computer Architecture

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

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

CENG3420 Lecture 03 Review

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

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

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

COE608: Computer Organization and Architecture

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 : Machine Structures

ECE260: Fundamentals of Computer Engineering

Lab 4 : MIPS Function Calls

Week 10: Assembly Programming

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

QtSPIM and MARS : MIPS Simulators

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

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

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

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

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

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

Compiling Code, Procedures and Stacks

Quick Review. lw $t0, 4($a0) Registers x Memory. $a0 is simply another name for register 4 $t0 is another name for register (green sheet)

Architecture II. Computer Systems Laboratory Sungkyunkwan University

MIPS Assembly Language Programming

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

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

Reduced Instruction Set Computer (RISC)

Transcription:

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. For maximum credit, include comments. slt $3, $2, $1 # $1<=$2 = not($2<$1) beq $3, $0, Target # if not($2<$1), branch to Target Part B (10 points) Write a for loop in C that takes the values at the even indexes in a 100- element vector A and stores them at consecutive indexes in vector B. For example, B[0] gets A[0], B[1] gets A[2], B[2] gets A[4], etc. For maximum credit, declare and initialize variables as needed. int A[100] = {4, -1, 3,, 17; int B[50]; int I; for(i=0; i<50; i++) B[i] = A[2*i]; Part C (15 points) Write a MIPS code fragment that is equivalent to the for loop you wrote in Part B. For maximum credit, include comments..data Aaddr:.word 4, -1, 3,, 17 # int A[100]={4,-1,3,,17; Baddr:.alloc 50 # int B[50];.text addi $1, $0, 0 # initialize loop counter Loop: slti $3, $1, 200 # is counter < 200 (i<50) beq $3, $0, Exit # if not, Exit Loop sll $2, $1, 1 # 2*i lw $4, Aaddr($2) sw $4, Baddr($1) addi $1, $1, 4 j Loop # read A[2*i] # write it to B[i] # increment counter # loop back Exit:... # instructions after the loop 1

Problem 2 (1 part, 20 points) Draw a control flow diagram for the following C code fragment. int i, y, x; int A[] = {3, 4,... for (i = 0; i < 10; i++) { x = f(i, y); if (x<0) continue; do{ x++; if ((x!=99)&&(a[i]>100)) z = A[i]; else z = x; while(a[z]); printf("%d\n", z); Flow Control 2

Problem 3 (4 parts, 22 points) Short Answer Questions Part A (2 points) In the following command, what does -o tell gcc to do? gcc HW1-2.c g Wall o HW1-2 The executable file that gcc produces as output should be named HW1-2. Part B (3 points) Write a Linux command to rename a file named old.c to new.c. mv old.c new.c Part C (10 points) The following Linux command displays the contents of the file named myfile.txt on the standard output: more myfile.txt Below is a part of an implementation of the more command. Fill in the five empty blanks to correctly access the command line argument and open the file specified by the argument (not just myfile.txt). int main(int argc, char *argv[]) { FILE *FP; if (argc!= 2 ) { printf("usage: more file\n"); exit(1); FP = fopen ( argv[1], r ); if (FP == NULL) { printf("%s could not be opened.\n", argv[1] ); return 0; else { Part D (7 points) Write a single C statement that corresponds to the following MIPS code. Assume $1 holds A, $2 holds B, $3 holds C, and $4 holds D. Do not use an if-then-else. addi $4, $0, 0 bne $1, $0, Set beq $2, $0, End bne $3, $0, End Set: addi $4, $0, 1 End: D = A (B &&!C) 3

Problem 4 (3 parts, 28 points) Subroutines and Stack Part A (15 points) The program Sort calls a subroutine Mystery (defined on next page). Complete the program Sort by adding MIPS code to correctly preserve appropriate registers before the jal by pushing them on the stack and to restore them after the subroutine call. Assume Mystery can modify any registers, not just the ones modified in the code on the next page. Sort: addi $1, $0, 4 # init Array index to 1 OuterLoop: lw $3, Array($1) # load in current element X add $2, $1, $0 # reinitialize Array index addi $29, $29, -12 sw $1, 0($29) sw $3, 4($29) sw $31, 8($29) # make room on stack for 3 words # push $1 # push $3 # push $31 jal Mystery # in: $2, $3; out: $2 lw $1, 0($29) lw $3, 4($29) lw $31, 8($29) addi $29, $29, 12 # pop $1 # pop $3 # pop $31 # readjust stack pointer sw $3, Array($2) # store current value here addi $1, $1, 4 # move current to next element slti $5, $1, 400 # all elements inserted? bne $5, $0, OuterLoop # if not, then continue addi $1, $0, 196 # point to 49th element lw $3, Array($1) # load 49th element addi $1, $1, 4 # point to 50th element lw $4, Array($1) # load 50th element add $3, $3, $4 # sum 49th and 50th sra $2, $3, 1 # compute median (average) jr $31 # return to caller 4

Now consider the following MIPS subroutine that takes two inputs: an array index I in $2 and an integer array element Value in $3. Its result is placed in $2. The label Array is the base address of array A. Mystery:... # L0 InnerLoop: addi $4, $2, -4 # L1 lw $4, Array($4) # L2 slt $5, $3, $4 # L3 beq $5, $0, Exit # L4 sw $4, Array($2) # L5 addi $2, $2, -4 # L6 bne $2, $0, InnerLoop # L7 Exit: jr $31 # L8 Part B (3 points) If $2 holds index I into array A, what does $4 hold at line L3? $4 = A[I-1] Part C (10 points) Write C code that is equivalent to the InnerLoop body of the Mystery subroutine. Assume A is a globally defined array. For maximum credit, choose the most appropriate loop construct and declare and initialize variables as needed. int A[] = {9, 33, -15,...; int Mystery(int I, int Value){ do { if (Value < A[I-1]){ A[I] = A[I-1]; I--; else break; while(i); return(i); 5