

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Computer Science 240 Fall 2008 Due: Monday, September 22 Reading. Patterson and Hennessy §2.5 – §2. 2.1. Construct a control flow graph (like the one shown in Figure 2.11 of the text) for the following section of C or Java code: for (i=0; i<x; i=i+1) y = y + i; Implement this C code in MIPS. Assume that i, x, and y correspond to registers $s1, $s2, $s3 respectively. 2.2. MIPS to C. Assume $s3 = i, $s4 = j, $s5 = @A. Below is some MIPS code: Loop: addi $s4, $s4, 1 # j = j + 1 sll $t1, $s3, 2 # $t1 = 4 * i add $t1, $t1, $s5 # $t1 = @ A[i] lw $t0, 0($t1) # $t0 = A[i] addi $s3, $s3, 1 # i = i + 1 slti $t1, $t0, 10 # $t1 = $t0 < 10? beq $t1, $zero, Loop # goto Loop if >= slti $t1, $t0, 0 # $t1 = $t0 < 0? bne $t1, $zero, Loop # goto Loop if < Below is part of the corresponding C code: do j = j + 1 while (______); What C code properly fills in the blank in the loop on the right? do j = j + 1 while (______); What C code properly fills in the blank in the loop on the right? A: [i++] >= 10? B: A[i++] >= 10 | A[i] < 0? C: A[i++] >= 10 & A[i] < 0? D: A[i++] >= 10 || A[i] < 0?
Assignment 2 Page 2 Computer Science 240 E: A[i++] >= 10 && A[i] < 0? F: None of the above 2.3. Add comments to the following MIPS code and describe in one sentence what it computes. Assume that $a0 and $a1 are used for the input and both initially contain the integers a and b, respectively. Assume that $v0 is used for the output. add $t0, $zero, $zero loop: beq $a1, $zero, finish add $t0, $t0, $a sub $a1, $a1, 1 j loop finish: addi $t0, $t0, 100 add $v0, $t0, $zero 2.4. The following code fragment processes two arrays and produces an important value in register $v0. Assume that each array consists of 2500 words indexed from 0 through 2499, that the base addresses of the arrays are stored in $a0 and $a1, respectively, and their sizes (2500) are stored in $a2 and $a3, respectively. Add comments to the code and describe in one sentence what this code does. Specifically, what will be returned in $v0? sll $a2, $a2, 2 sll $a3, $a3, 2 add $v0, $zero, $zero add $t0, $zero, $zero outer: add $t4, $a0, $t lw $t4, 0($t4) add $t1, $zero, $zero inner: add $t3, $a1, $t lw $t3, 0($t3) bne $t3, $t4, skip addi $v0, $v0, 1 skip addi $t1, $t1, 4 bne $t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer 2.5. Assume that the code from the previous exercise is run on a machine with a 2 GHz clock that requires the following number of cycles for each instruction: Instruction Cycles add, addi, sll 1 lw, bne 2 In the worst case, how many seconds will it take to execute this code?