Sei sulla pagina 1di 3

CSE230/EEE230 Computer Organization & Assembly Language Programming

Midterm Exam 2, October 14th, 2010


Arizona State University
Rules
1. The exam is open book and open notes, you may use your laptop to access the lecture notes. However,
you are not allowed to use your laptop for communication or Internet access (e.g., no email, no
chatting, no Google search).
2. Work on all the problems. They add up to 100 points total.
3. Whenever appropriate, you should try to show how you solved the problem (e.g., writing down
intermediate steps leading to your final answer) instead of supplying only the final answer, so that you
may earn partial credit even if your final answer is incorrect.
4. Your answers must be legible and organized.

Student Name: ________________________________ ASU ID: ________________________

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.

(105)10 = (0110 1001)2 (28)10 = (0001 1100)2


(105)’10 = (1001 0110)2 (28)’10 = (1110 0011)2
(-105)10 = (105)’+1 = (1001 0111)2 (-28)10 = (28)’+1 = (1110 0100)2

a-b = a + (-b) = (-105) + (-28) = 1001 0111


+1110 0100 (we are adding two number of the same sign)
=(1)01111011
Yes, 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.)

add $t0, $zero, $zero // i = 0


addi $t1, $zero, 10 // n = 10
loop: sll $t2, $t0, 2 //set i, the indexing variable to jump 4 bytes at a time.
add $t3, $t2, $a1 //adjust the address of the array to v[i]
lw $t4, 0($t3) //get the value in v[i]
add $t4, $t4, $t0 // v[i] + i
sll $t2, $t0, 4 // adjust the index to pick the 16th byte i.e 4 words away (4*i)
add $t3, $t2, $a0 //adjust the address of the array u[4*1]
sw $t4, 0($t3) // store v[i] + i into u[4*i]
addi $t0, $t0, 1 // counter increment
bne $t0, $t1, loop //for loop

for ( i = 0; i != 10; i++ )


{
u(4*i) = v(i) + i ;
}

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)

int A_Good_Funct(char str1[ ], char str2[ ], int n)


{
int i, j=0;

for (i = 0; i < n; i++){ {


if (str1[i] == str2[0]) j=j+1;
}
return j;
}

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.

str1 à $a0 str2 à $a1 n à $a3

mov $t0, $zero //the counter


mov $t1, $a0 //str1
mov $t2, $a1 //str2
mov $v0, $zero //initialize return count to 0
lb $t4, 0($t2) //store the first char of str2 to t4. This needn’t be in the loop.

Loop: beq $t0, $a3, Exit // exit if i equals n


lb $t3, 0($t1)
addi $t0,$t0,1 // increment the counter, by 1.
Addi $t1,$t1,1 // adjust address of srt1 to the next char in the string
beq $t3, $t4, Add
j Loop

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

/10 /15 /15 /30 /30 /100

Potrebbero piacerti anche