=========================preview======================
(Comp180)[2005](s)final~cs_nfp^_10171.pdf
Back to COMP180 Login to download
======================================================
HONG KONG UNIVERSITY OF SCIENCE &
TECHNOLOGY
Computer Organization (COMP 180)

Spring Semester, 2005


Final Examination Solution
May 25, 2005
Name: Student ID:
Email: Lab Section Number:
Instructions:
1.
This examination paper consists of 10 pages (the last 4 pages are appendices 1 to 4), and 7 questions

2.
Please write your name, student ID, email and lab section number on this page.

3.
For each subsequent page, please write your student ID at the top of the page in the space provided.

4.
Please answer all the questions in the spaces provided on the examination paper. You may use the empty spaces on the pages for your rough work and afterwards draw a diagonal line through it to show that it is not part of your answer.

5.
Please read each question very carefully and answer clearly and to the point. Make sure that your answers are neatly written.

6.
Keep all pages stapled together.

7.
The examination period will last for 2 hours and 15 minutes.

8.
Stop writing immediately when the time is up.


Question Percentage % Scores
1 15
2 15
3 20
4 15
5 15
6 10
7 10
TOTAL 100

Question 1 (15 points) MIPS and MIPS addressing modes
Assume that the following MIPS code is a procedure that takes one input parameter in register $a0 and returns one output result in register $v0. You may also assume that $t0 corresponds to variable i0, $t1 corresponds to i1, and $a0 corresponds to variable
p.
begin: addi $t0, $zero, 0 ;
addi $t1, $zero, 2 ;

loop: slt $t2, $a0, $t1 ; bne $t2, $zero, finish ; add $t0, $t0, $t1 ; addi $t1, $t1, 2 ; ; # We want to insert instructions here j loop ;
finish: add $v0, $t0, $zero ;
a) In one sentence, what does this procedure calculate? (2 points)
The procedure calculates and returns the sum of all even numbers from 0 to p if p is even or to p-1 if p is odd.
b) Using only the variables described above, transcribe the procedure to C or C++ using a while loop (for and goto are not allowed). (2 points)
i0 = 0;
i1 = 2;
While (i1<=p) //deduct 1pt if i1<p
{

i0 = i0 + i1;
i1 = i1 + 2;}return i0;
c) If we want to insert more instructions instead at the , between addi $t1, $t2, 2 and j loop, how many such instructions are allowed without changing the code. (6 points)
i) (1pt) The bne instruction uses the PC relative addressing mode, that is,
the value of the label is added to PC + 4 to calculate the branch address;
ii) (1pt) In jump and branch instructions, the label value is represented in
number of 32bit words;
iii) (1pt) The label field is 16 bit wide. iv) (1pt) The address can be positive or negative (above or below the current instruction).
There can be at most 215 -1 + 1 instructions between the bne instruction and the
one labeled finish (including this one) (1pt)
Since there are already 3 instructions, the maximum number of new instructions
that we can insert is