1 / 5

CS232 Exam 1 September 29, 2004

CS232 Exam 1 September 29, 2004. Name: Napoleon Dynamite  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.

tammycraft
Download Presentation

CS232 Exam 1 September 29, 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 1September 29, 2004 Name: Napoleon Dynamite 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) • enchilada: li $v0, 0 • li $t0, 0 • li $t1, 0 • mole: bge $t1, $a1, tapas • mul $t2, $t1, 4 • add $t2, $t2, $a0 • lw $t3, 0($t2) • ble $t3, $t0, queso • move $v0, $t1 • move $t0, $t3 • queso: add $t1, $t1, 1 • j mole • tapas: jr $ra • Part (a) • Translate the function enchilada 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 pointer arithmetic. We will not deduct points for syntax errors unless they are significant enough to alter the meaning of your code. (30 points) • int enchilada(int array[], int size) { • int hival=0; • int pos; • for (int i=0; i<size; i++) { • int temp=array[i]; • if (temp>hival) { • pos=i; • hival=temp; • } • } • return pos; • } • Part (b) • Describe briefly, in English, what this function does. (10 points) • Iterates through $a1 elements of integer array beginning at memory location $a0. Returns the index of the greatest positive integer in the array, or zero if there are no positive elements. 2

  3. Question 2: Write a MIPS function (45 points) • Here is a function random_anagram that takes two arguments (str and len) and has no return value (the string is modified in place); this function generates an anagram of a string by swapping random pairs of characters in the string. Random numbers are generated using the random() function, which you don’t have to write and has the prototype as shown on the right. • Translate random_anagram into a MIPS assembly language function. Use the rem pseudoinstruction for the mod (%) operator. You will not be graded on the efficiency of your code, but you must follow all MIPS conventions. Comment your code!!! • random_anagram: • sub $sp, $sp, 20 • sw $ra, 0($sp) # always save return address • sw $a0, 4($sp) # function arguments are caller-saved • sw $a1, 8($sp) • sw $s0, 12($sp) # back up callee-saved registers for use • sw $s1, 16($sp) • li $s0, 0 # $s0 represents loop index • # (we know we execute 100 times) • loop: • jal random • move $s1, $v0 # return value is safe in $s1 • jal random • lw $a0, 4($sp) # restore function arguments from stack • lw $a1, 8($sp) • rem $t1, $s1, $a1 # index1 = random() % len • rem $t2, $v0, $a1 # index2 = random() % len • add $t1, $t1, $a0 # calculate array offsets from $a0 • add $t2, $t2, $a0 • lb $t3, 0($t1) # swap characters • lb $t4, 0($t2) • sb $t3, 0($t2) • sb $t4, 0($t1) • add $s0, $s0, 1 # increment and loop • blti $s0, 100, loop • lw $ra, 0($sp) # restore $ra, S-registers, and $sp • lw $s0, 12($sp) • lw $s1, 16($sp) • add $sp, $sp, 20 • jr $ra 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 a technique to simplify a tool or procedure by allowing the user to be unconcerned about the lower-level details of how the procedure is really performed. • ISAs are a good example of abstraction because they represent a consistent interface by which users can create reliable computer programs without understanding the details of how these instructions are executed on a modern microprocessor. • Part b) In section, we saw code like the following: • li $a0, 1 • mtc1 $a0, $f1 • cvt.s.w $f1, $f1 • What does this code do, and, specifically, what role does the third instruction play? (5 points) • The integer value 1 is moved to floating point register $f1. The third instruction converts the integer representation of 1 stored in $f1 to its single-precision floating point representation. • Part c) What advantages does using interrupts have over polling? (5 points) • Using interrupts allows for more efficient programs than polling, since polling will typically check the event status many times before returning positive. Also when writing the program, using interrupts allows you to decouple event handling from the normal execution, rather than having to track the status of multiple events in the main program thread. 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 • Pointers in C: • Declarartion: either char *char_ptr -or- char char_array[] for char c • Dereference: c = c_array[i] -or- c =*c_pointer • Take address of: c_pointer = &c 5

More Related