1 CS232 Exam 1 Solutions February 20, 2004 Name:  This exam has 5 pages, including this cover.  There are three questions, worth a total of 100 points.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Branches Two branch instructions:
The University of Adelaide, School of Computer Science
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Assembly Language II CPSC 321 Andreas Klappenecker.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
CS 536 Spring Code generation I Lecture 20.
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Fall 2003SYCS-401 Operating Systems Instruction Set Architecture An overview of MIPS R3000 assembly language.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Spr 2015, Feb 9... ELEC / Lecture 4 1 ELEC / Computer Architecture and Design Spring 2015 Compiling and Executing Programs.
1 CS232: Computer Architecture II Fall 2011 Intel i7 Quad-core.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
R3000/001 Assembly Programming using MIPS R3000 CPU R3000 CPU Chip Manufactured by IDT What is MIPS R3000 Processor? A 32-bit RISC CPU developed by MIPS.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 CS232: Computer Architecture II Fall 2010 AMD dual-core Opteron.
CS232: Computer Architecture II
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Lecture 6: Assembly Programs
Computer Architecture Instruction Set Architecture
CS2100 Computer Organisation
Computer Architecture Instruction Set Architecture
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Assembly Programming using MIPS R3000 CPU
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Review.
Lecture 6: Assembly Programs
Systems Architecture I
Assembly Programming using MIPS R3000 CPU
Computer Architecture
9/27: Lecture Topics Memory Data transfer instructions
Presentation transcript:

1 CS232 Exam 1 Solutions February 20, 2004 Name:  This exam has 5 pages, including this cover.  There are three questions, worth a total of 100 points.  The last page is a summary of the MIPS instruction set, which you may remove for your convenience.  No other written references or calculators are allowed.  Write clearly and show your work. State any assumptions you make.  You have 50 minutes. Budget your time, and good luck! QuestionMaximumYour Score Total100

2 Question 1: Understanding MIPS programs (40 points) func: li$v0, 0 li$t0, 0 L1:add$t1, $a0, $t0 lb$t2, 0($t1) beq$t2, $zero, L3 bne$t2, $a1, L2 add$v0, $v0, 1 L2:add$t0, $t0, 1 jL1 L3: jr$ra Part (a) Translate the function func above into a high-level language like C or Java. You should include a header that lists the types of any arguments and return values. Also, your code should be as concise as possible, without any gotos or pointers. We will not deduct points for syntax errors unless they are significant enough to alter the meaning of your code. (30 points) int func (char s[], char c) { int num = 0; for(int i=0;s[i] !=0;i++) { if(s[i] = = c) num++; } return num; } Part (b) Describe briefly, in English, what this function does. (10 points) Counts the number of occurrences of the second argument (a character) in the character array (first argument) and returns the count when it finds a zero in the array.

3 Question 2: Write a recursive function (45 points) Here is a function is_prime_rec that takes two arguments (i and P, both 32-bit numbers) and returns an integer; 1 is returned if P is found to be prime, otherwise 0 is returned. This function is used by the function is_prime. Translate is_prime_rec into a MIPS assembly language function. You do not need to translate is_prime !! Argument registers $a0 and $a1 will correspond to i and P, and the return value should be placed in $v0 as usual. Use the rem pseudoinstruction for the mod (%) operator. Your implementation must be recursive. You will not be graded on the efficiency of your code, but you must follow all MIPS conventions. Comment your code!!! Is_prime_rec: subi $sp, $sp, 4 #allocate stack sw $ra, 0($sp) bgt $a0, 1 L1 #if (I>1) branch to L1 li $v0, 1 j exit L1:rem $to, $ao, $a0 #rem = (P%i) bne $t0, $zero, L2 #if(rem !=0), branch to L2 li $v0, 0 j exit L2:subi $a0, $a0, 1 #i = i-1 jal is_prime_rec Exit:lw $ra, 0($sp) #restore ra add $sp, $sp, 4 #cleanup stack jr $ra #return $v0 int is_prime_rec(int i, int P) { if (i <= 1) return 0; if ((P % i) == 0) return 1; return is_prime_rec(i-1, P); } int is_prime(int P) { return is_prime_rec(P-1, P); }

4 Question 3: Concepts (15 points) Write a short answer to the following questions. For full credit, answers should not be longer than two sentences. Part a) What is abstraction? How does it relate to instruction set architectures (ISAs)? (5 points) Abstraction is the concept of hiding complexity, e.g. by separating interface from implementation. ISAs abstract the specification of what computation should be performed from how it is performed, allowing binary code compatibility across a family of hardware implementations that implement the same ISA. Part b) Why can IEEE floating point numbers represent a larger range of numbers than integers with the same number of bits? (e.g., single precision can represent from to while 32-bit integers can only do 0 and from 1 to ) It is not enough to simply give the IEEE format; you must explain what drawback of floating point numbers enables the larger range. (5 points) Since both representations use only 32 bits, there are as many single precision FP numbers as there are integers. As the FP number values get farther from zero, it's less likely that they can be specified exactly using the IEEE notation, which may result in rounding errors. Part c) What is the 90/10 rule? How does it relate to assembly language programming? (5 points) 90/10 rule specifies that processor spends 90% of the time executing 10% of the code. To best optimize these 10%, implement that code in the assembly language.

5 MIPS instructions These are some of the most common MIPS instructions and pseudo-instructions, and should be all you need. However, you are free to use any valid MIPS instructions or pseudo-instruction in your programs. The second source operand of the arithmetic and branch instructions may be a constant. Register Conventions The caller is responsible for saving any of the following registers that it needs, before invoking a function. $t0-$t9$a0-$a3$v0-$v1 The callee is responsible for saving and restoring any of the following registers that it uses. $s0-$s7$ra CategoryExample InstructionMeaning Arithmetic add$t0, $t1, $t2$t0 = $t1 + $t2 sub$t0, $t1, $t2$t0 = $t1 – $t2 addi$t0, $t1, 100$t0 = $t mul$t0, $t1, $t2$t0 = $t1 x $t2 div$t0, $t1, $t2$t0 = $t1 / $t2 rem$t0, $t1, $t2$t0 = $t1 mod $t2 Register Setting move$t0, $t1$t0 = $t1 li$t0, 100$t0 = 100 Data Transfer lw$t0, 100($t1)$t0 = Mem[100 + $t1] sw$t0, 100($t1)Mem[100 + $t1] = $t0 Branch beq$t0, $t1, Labelif ($t0 = $t1) go to Label bne$t0, $t1, Labelif ($t0 ≠ $t1) go to Label bge$t0, $t1, Labelif ($t0 ≥ $t1) go to Label bgt$t0, $t1, Labelif ($t0 > $t1) go to Label ble$t0, $t1, Labelif ($t0 ≤ $t1) go to Label blt$t0, $t1, Labelif ($t0 < $t1) go to Label Set slt$t0, $t1, $t2if ($t1 < $t2)then $t0 = 1 else $t0 = 0 slti$t0, $t1, 100if ($t1 < 100)then $t0 = 1 else $t0 = 0 Jump jLabelgo to Label jr$rago to address in $ra jalLabel$ra = PC + 4; go to Label