Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. (10 points) For a given computer, its CPI (clock cycles per instruction) is unique and fixed. Is this true?
Explain your answer briefly.
False. CPI depends on the instructions used in the program and clock cycle of the computer. For a
given computer, clock cycle is fixed. But CPI would be different for different sets of instructions.
2. (15 points) Suppose that the following two instructions are stored in the memory starting from address
0x00400000. What is the value in register $31 after the “jal” instruction is executed?
[ 0x00400000 ] addi $31, $0, 0x1000
[ 0x00400004 ] jal swap
$31 corresponds to $ra.
After executing the first line, $ra has the value 0x1000 in it. (you needn’t write this in your solution)
After a jal instructions, (PC+4) gets stored in the $ra register.Value of PC when executing jal instruction
is [ 0x00400004 ]. The address of the next instruction is what the program return to after the
procedure. Hence $ra gets the value of [ 0x00400008 ]. And $ra gets overwritten. You are not
supposed to sum this value to the previous value.
3. (15 points) Given two decimal numbers a=-105 and b=28, convert them into signed 8-bit numbers in
2’s complement representation, and then perform a-b by hand. Determine whether there is an overflow.
1
4. (30 points) What does the following code do? (You can simply write the corresponding code in any
high-level programming language such as C. For convenience, you can define several variables
corresponding to the registers if needed.)
You need to provide a solution in a high level language. Merely explaining each line
would not suffice.
2
5. (30 points) (1) Look at the following C subroutine. What does the code do? (5 points)
It compares the first character in str2 to every character in str1, and returns the number of matches.
(2) Translate the C subroutine into equivalent MIPS assembly code. (25 points)
Assume that the base addresses of str1 and str2 are in $a0 and $a1 respectively, and n is in $a3. Also,
assume that the return values will be stored in $v0.
Points to remember (common mistakes made): Strings use byte addressing instead of
word addressing. Implement the for loop. Implement the if loop. This is not string
compare, we are comparing str1 to first letter in str2. Do not forget to populate the return
value into $v0. Checking for if the string is empty is a good practice, but not a necessary
requirement in this test. Minor syntax errors weren’t penalized. The stack preserving
operations were also not a necessary requirement.
Add: addi $v0,$v0,1 //if equal, increment the count for the return value
j Loop // need to loop until n characters are checked for.
Exit: jr $ra
Score:
1 2 3 4 5 Total