IC220 Computer Architecture SAMPLE SOLUTION 6-Week Exam Last Name ____________________ First Name _________________ Alpha ___________________ Note: This exam is closed-book, closed-notes. No calculators are permitted. To receive partial credit, show all work. UNLESS STATED OTHERWISE, PSEUDO-INSTRUCTIONS MAY BE USED Page 1 (11 pts) ______________ Page 2 (14 pts) ______________ Page 3 (15 pts) ______________ Page 4 (6 pts) ______________ Page 5 (14 pts) ______________ TOTAL(60 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 XXX SAMPLE IC220 6wk Exam (1 pt) What is the program that translates from assembly language to machine language? Answer: ____assembler_______________ (3.5 pts) Now pretend that you are performing a task similar to that program mentioned above. As a first step, translate the following pseudo-instructions into one or more real MIPS instructions. Where necessary, follow any conventions that that program would normally use. bge $s0, $s1, L2 slt $at, $s0, $s1 beq $at, $zero, L2 lw $t0, $s0($t2) add $at, $s0, $t2 lw $t0, 0($at) # If $s0 >= $s1, branch to L2 # $at=1 IF $so<$s1 # branch if $at is zero (so NOT case $s0<$s1) # $t0 = Mem[$s0 + $t2] # Can only use one register, so must add first (1 pts) Give an EXAMPLE of a particular “ISA” that is in use today. Answer: _MIPS, SPARC, x86, ARM___________________________ (2.5 pts) List the five classic components associated with all computers. No description needed. 1. Input 2. Output 3. Memory 4. Datapath 5. Control (3 pts) Suppose register $s2 holds the base address of array A (an array of 32-bit integers), and register $s1 holds the value of variable X. Show the MIPS instruction(s) for this C code: A[4] = A[8] + X; lw $t0, 32($s2) add $t0, $t0, $s1 sw $t0, 16($s2) # get A[8] # temp = A[8} + x # store result in A[4] IC220 SAMPLE 6 week Exam 1 (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). addi $t0, 13, 17 VALID INVALID because: sources must be 1 reg and 1 constant lw $t1, 0($s0) VALID INVALID because: jr $v0, $a0 VALID INVALID because: jr requires ONE register as input VALID INVALID because: lui expects 1 reg (dest) and constant (so this is fine) lui $t2, 23 (5 pts) In MIPS, there are three different instruction types: R, I, and J. For each instruction below, circle the appropriate instruction type. Note: some of these instructions you may not have seen before, but you should still be able to answer the question based on what we know about the instruction types. 1. sw $t0, 8($sp) Type (circle one): R I J 3. sub $t0, $t1, $t2 Type (circle one): R I J 2. bne $t0, $t1, Label7 Type (circle one): R I J 4. xor $t0, $a0, $a1 Type (circle one): R I J 5. j Label42 Type (circle one): R I J (1 pts) (circle best answer) Which type of ISA tends to have a small number of simple, fast instructions? BISC CISC LISC RISC YISC IC220 SAMPLE 6 week Exam 2 (2 pts) Machine A is a 4 GHz machine. It requires 10 seconds of actual CPU time to execute a certain program. How many clock cycles does that program require in total? Time NumCyc = NumCyc/ClockRate = Time * ClockRate = 10sec * (4*109 cyc/sec) = 4*1010 cycles (6 pts) The instructions in the instruction set of a particular computer can be categorized into three broad classes, Class A, Class B and Class C, depending on the average number of cycles that each instruction takes: Class A instructions 1 CPI Class B instructions 2 Class C instructions 3 Suppose you are writing a program. You have two different approaches you can take. These two different approaches use different numbers of instructions, as shown in the table below: Approach 1 Approach 2 Instruction Counts for each Instruction Class Class A instructions Class B instructions Class C instructions 2 1 2 4 1 1 Circle/provide the correct answers below but remember to show your work. (1 pt) Which approach executes the most instructions? APPROACH 1 APPROACH 2 APPROACH 1 APPROACH 2 Appr 1: 2+1+2 = 5 insts Appr 2: 4+1+1 = 6 insts (3 pts) Which approach will be faster and by how much? is faster by __10/9_______________ NumCyc1: 2*1 + 1*2 + 2*3 = 10 cyc NumCyc2: 4*1 + 1*2 + 1*3 = 9 cyc (2 pts) What is the CPI for each approach? CP1 for approach 1 = 10/5 = 2 CPI for approach 2 = 9/6 = 1.5 IC220 SAMPLE 6 week Exam 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. # Note – assume “b” has some existing value – you don’t know what it is! d = 0; for (int a=b; a < 25; a++) { d = d + a; } li $s3, 0 # move $s0, $s1 # li $t0, 25 j Test Loop: add $s3, $s3, $s0 # addi $s0, $s0, 1 # Test: blt $s0, $t0, Loop # # also okay: blt $s0, 25, d = 0 a = b d = d + a a++ Loop if a < 25 Loop -- but is (more)pseudo IC220 SAMPLE 6 week Exam 4 (14 pts) Translate the following C++ code into MIPS assembly. Follow all conventions. int bellasChoice (int a, int b) { int score = a + jacob(b); if (score == 0) return 17; else return edward(score); } bellasChoice: addi $sp, $sp, -8 sw $ra, 0($sp) sw $a0, 4($sp) move $a0, $a1 jal jacob lw $a0, 4($sp) add $t0, $a0, $v0 bne $t0, $zero, Else li $v0, 17 j doReturn # space for 3 items # save return address # save a # # # # put arg b in place for call call jacob(b), result in $v0 (CORRECTED) reload a temp=a+jacob(b) (or: put directly in $a0) # return value=17 Else: move $a0, $t0 jal Edward doReturn: lw $ra, 0($sp) addi $sp, $sp, +8 jr $ra # (CORRECTED) set argument = score # call edward(b, score) # reload $ra # fixup $sp # return IC220 SAMPLE 6 week Exam 5