IC220 Computer Architecture SOLUTION 6-Week Exam Last Name ____________________ First Name _________________ Section: (circle one) 1001 (Roth) 3001 (Rye) 3002 (McDowell) 5001 (Rye) Alpha ___________________ 5002 (McDowell) Note: This exam is closed-book, closed-notes. Exception: A reference sheet is attached to the last page. You may freely use this, but only this sheet (not any other notes, or a copy of the sheet that you brought yourself). No calculators are permitted. To receive partial credit, show all work. UNLESS STATED OTHERWISE, PSEUDO-INSTRUCTIONS MAY BE USED Page 1 (15 pts) ______________ Page 2 (12 pts) ______________ Page 3 (10 9 pts) ______________ Page 4 (6 pts) ______________ Page 5 (11 pts) ______________ TOTAL(54 pts) ______________ NOTE: This is an exam that will be given to multiple sections and possibly to students after the primary exam day. You may not discuss it with anyone until after Tues February 23, 2016. SOLUTION IC220 6wk Exam – Mon Feb 22, 2016 SOLUTION (1 pt) What is the tool that translates from a high-level language to assembly language? Answer: _______COMPILER_____________________ (1 pts) Give an EXAMPLE of a particular “ISA” that is in use today. Answer: ____x86, MIPS, SPARC, ARM________________________ (2.5 pts) List the five classic components associated with all computers. No description needed. 1. Input 2. Output 3. Memory 4. Control 5. Datapath (2.5 pts) Suppose register $s4 holds the base address of array C (an array of 32-bit integers), and register $s2 holds the value of variable K (an integer). Show the MIPS instruction(s) for this C code: C[4] = K + 10; addi $t0, $s2, 10 sw $t0, 16($s4) # Note – don’t modify $s2 – this code should NOT change the value of K! (8 pts) For each of the following, circle whether it is a valid or invalid raw MIPS instruction (count pseudoinstructions as invalid). If invalid, briefly state why (must provide correct reason for credit if invalid). lw $t0, $t1($s0) VALID INVALID because: lw/sw require register+offset (not TWO registers for destination) add $t0, $t1, 4($s2) VALID INVALID because: add requires 3 registers as operands (can’t access memory with add) addi $t0, $t0, -3 VALID INVALID because: blt $a0, $a1, Label17 (assume Label17 exists) VALID INVALID because: blt is pseudo-instruction SOLUTION IC220 6 week Exam – Mon. Feb 22, 2016 SOLUTION 1 (6 pts) Consider this number (given in hex): 28A2000F (3 pts) Show the BINARY form of this number: 0010 1000 1010 0010 0000 0000 0000 1111 Opcode is first 6 bits: 001010 = 00 1010 = 0A (hex). Which is “sltit” according to “green sheet.” (3 pts) If that number represented a MIPS instruction, what would it be? add addi and beq bne j jal jr lw slt slti sw sub subu (NO further details needed, just circle one) (1 pts) (circle best answer) In terms of the instructions available, which type of ISA tends to offer a larger number of complex, powerful instructions? (circle one) BISC CISC LISC RISC YISC (5 pts) Below is a snippet of code. Assume that when it starts executing: - $s0 holds the base address of some array of integers called “array1” - $s1 holds the base address of some array of integers called “array2” - $s3 holds an integer named “K” What does this code do? (in terms of array1, array2, and K) (we’re looking for a brief overall summary of the effect of executing code, NOT a description of how it does it or what each instruction does): L71: move $a0, $s0 move $a1, $s1 add $t2, $zero, $zero beq $t2, $s3, L73 lw $t0, 0($a0) sw $t0, 0($a1) addi $a0, $a0, 4 addi $a1, $a1, 4 addi $t2, $t2, 1 j L71 L73: Your answer: Copies first K elements (words) of array1 to array2. SOLUTION IC220 6 week Exam – Mon. Feb 22, 2016 SOLUTION 2 (2.5 pts) On a certain machine, Program 1 runs in 10 seconds and requires 200 million cycles to execute. What is the clock rate of this machine? Make sure your answer has a unit. Time = NumCycles / ClockRate ClockRate = NumCycles / Time = 200 * 106 cycles / 10 seconds = 20 * 106 cycles = 20 MHz (2.5 pts) Label the UNITS for each term in the equation below. Each term should have a unit for the numerator and denominator (a total of FIVE units for you to fill in, using the blanks below the equation). Time = IC * inst program CPI * cyc inst ClockCycleTime sec cyc (4 pts) Below are two examples of possible metrics for comparing performance. For each, a. indicate whether using that metric, in the given context, is OKAY or BAD IDEA, *and* b. briefly explain why. The number of cycles needed to execute a program (comparing two versions of the same program on the same machine) a. Circle: OKAY or BAD IDEA b. Why? Time = NumCycles * CycleTime The cycle time (clock rate) is the same (same machine), so okay to just compare “NumCycles” The number of instructions per second (comparing the same program on two different machines) a. Circle: OKAY or BAD IDEA b. Why? The number of instructions needed (per program) may vary across the machines. SOLUTION IC220 6 week Exam – Mon. Feb 22, 2016 SOLUTION 3 INSTRUCTIONS: If needed on this page and the next, assume the following variable/register mappings: a – $s0 b – $s1 c – $s2 d – $s3 Where appropriate, be sure to obey register conventions. It’s fine to use PSEUDO-INSTRUCTIONS (it always is unless we say explicitly not to) (6 pts) Translate the following C++ code into MIPS assembly. d = 0; while (d < c) { d = dragon(b); b = b + 1; } Note – a “while” loop, so must “test first” Loop: li $s3, 0 j Test move $a0, $s1 jal dragon move $s3, $v0 addi $s1, $s1, 1 blt $s3, $s2, Loop # or move $s3, $zero or similar # # # # # Or instead of blt: slt $t0, $s3, $s2 bne $t0, $zero, Loop prepare argument (b) for call call dragon (b) store return value into d b++ keep looping if d < c # is d < c # if YES, continue looping Alternate solution: Loop: li $s3, 0 bge $s3, $s2, Exit move $a0, $s1 jal dragon move $s3, $v0 addi $s1, $s1, 1 j Loop # # # # # # or move $s3, $zero or similar quit if d >= c (inverse of d < c) prepare argument (b) for call call dragon (b) store return value into d b++ Exit: Or instead of bge above: slt $t0, $s3, $s2 beq $t0, $zero, Exit # is d < c? # if NOT, exit SOLUTION IC220 6 week Exam – Mon. Feb 22, 2016 SOLUTION 4 (11 pts) Translate the following C++ code into MIPS assembly. Follow all conventions. int finn (int a, int b, int c) { int t = 20; return rey(c, t) + kylo(b, a) + c; } finn: addi $sp, $sp, -20 sw $ra, 0($sp) sw $a0, 4($sp) sw $a1, 8($sp) sw $a2,12($sp) # call rey(c,t) li $t0, 20 move $a0, $a2 move $a1, $t0 jal rey sw $v0, 16($sp) # call kylo(a,b) lw $a0, 8($sp) lw $a1, 4($sp) jal kylo # space for 5 items # save a # save b # save c # t0 = 20 # first argument = c # second argument = t # save rey’s result # first argument = b # first argument = a # call kylo, result now in $v0 # add results together lw $t0, 16($sp) # get lw $t1, 12($sp) # get add $v0, $v0, $t0 # add add $v0, $v0, $t1 # add rey’s result c rey’s result to kylo’s c to overall result # cleanup and return lw $ra, 0($sp) # reload return addres addi $sp, $sp, 20 # fix up stack jr $ra # return # NOTE – if you want to use $s0, $s1, etc. – must save original value # and restore at the end! SOLUTION IC220 6 week Exam – Mon. Feb 22, 2016 SOLUTION 5