1 / 5

CS232 Exam 1 Solutions February 20, 2004

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.

elgin
Download Presentation

CS232 Exam 1 Solutions February 20, 2004

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS232 Exam 1Solutions 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! 1

  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 • j L1 • 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. 2

  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 3

  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 2-126 to 2127 while 32-bit integers can only do 0 and from 1 to 232-1) 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. 4

  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 5

More Related