Jamoum University College Computer Department
Computer Architecture Major Exam #1_Solution Time: 2 hours Student Name:_________________________________ Student Number:_____________________
Questions Points 1
2 3
Total = out of 20
Problem 1 (CLO2) (6 points)
Assume the code below is executed on a computer with a clock rate of 2GHz.
(a)How long will it take to complete execution of the code if CPI=1.0?
(3 points)
Ans.
Since the CPI=1.0, so each instruction takes 1 cycle to execute. There are 8 instructions, and the clock rate is 2GHz
Execution time=I ×CPI
CR = 8×1
2×109=4×10−9s=4ns
(b)Now assume that add takes one clock cycle to execute, lw requires 3 clock (3 points)
cycles to execute and sw requires 2 clock cycles to execute.
1) What is the average CPI?
2) How long will it take the code sequence to complete?
Ans.
CPIavg .= Total cycles
Instruction count=4×1+2×2+2×3
8 =14
8 =1.75
Execution time=I ×CPIavg .
CR =8×1.75
2×109 =7×10−9s=7ns
26/10/1439 Dr. Abdulbasit Abid 1
add $t0 , $t1 ,
$t2
lw $s0 , 12( $t0)
add $t0 , $t3 ,
$t4
lw $s1 , 20( $t0)
add $s3 , $s0 ,
$s1
add $t0 , $t5 ,
$t6
sw $s3 , 24( $t0) sw $s3 , 48( $t0)
Jamoum University College Computer Department
Computer Architecture Major Exam #1_Solution Time: 2 hours Student Name:_________________________________ Student Number:_____________________
Problem 2 (CLO1) (6 points)
Write the compiled MIPS code of the following code:
while (A[j] == m) j += 1;
• Mapping j to $s2 and m to $s3
• Base of array A is $s5
Ans.
Compiled MIPS code:
Loop: sll $t1, $s2, 2 (1 points) add $t1, $t1, $s5 (1 points) lw $t0, 0($t1) (1 points) bne $t0, $s3, Exit (1 points) addi $s2, $s2, 1 (1 points) j Loop (1 points) Exit:
26/10/1439 Dr. Abdulbasit Abid 2
Jamoum University College Computer Department
Computer Architecture Major Exam #1_Solution Time: 2 hours Student Name:_________________________________ Student Number:_____________________
Problem 3 (CLO1) (8 points)
Provide the set of MIPS instructions that may be used to implement any of the following:
1) The pseudoinstruction not $t1, $t2 using nor instruction (1 points)
Ans. nor $t1, $t2, $zero
2) Creating the 32-bit constant (2 points)
0010 0000 0000 0001 0100 1001 0010 0100 and storing that value to register $t1
Ans. lui $t1, 8193 ori $t1, $t1, 18724
3) For the following AVERAGE procedure, rewrite the code and spill (2 points)
register $s3 to memory using stack because it is needed by the caller
Ans. AVERAGE:
addi $sp, $sp, -4 sw $s3, 0($sp) add $s3, $a0, $a1
add $s3, $s3, $a2 add $s3, $s3, $a3 srl $s3, $s3, 2 add $v0, $s3, $zero lw $s3, 0($sp)
addi $sp, $sp, 4 jr $ra
4) If the branch target in the following code is too far to encode with 16-bit (2 points)
offset, write the required code that replaces this short-address conditional branch:
26/10/1439 Dr. Abdulbasit Abid 3
AVERAGE: add $s3, $a0, $a1 add $s3, $s3, $a2 add $s3, $s3, $a3 srl $s3, $s3, 2 add $v0, $s3, $zero jr $ra
Jamoum University College Computer Department
Computer Architecture Major Exam #1_Solution Time: 2 hours Student Name:_________________________________ Student Number:_____________________
beq $s2, $s3, Label1
Ans. bne $s0, $s1, L2
j L1
L2: …
5) For the conditional branch instruction given below:
(1 points)
bne $s0, $s1, Exit (assume PC and Offset values are known) The target address is calculated by the equation:
Ans.
Target address = (PC + 4) + (offset × 4)