Section# _________________ Name ____________________________________ Computer Organization
Assignment 3
Write or type your answers on these pages.
Hand in a paper copy on the due date. Staple your pages together.
1. What are the conventions for the following registers? (one or two words is fine) S:
A:
T: V:
2. What are the corresponding C++ statements for the MIPS assembly below: # RAT: Register Allocation Table:
$s0isf, $s1isg, $s2ish, $s3isi, $s4isj, $s6isthebaseaddressofint arrayA. Assume all variables and A elements are integers.
a: add $s4, $s1, $s2 b:addi $s3, $s3, 1
sll $s2, $s1, 3 c:lw $s0, 4($s6)
d: add $s0, $s0, $s1
add $s0, $s0, $s2 ___________________________________ add $s0, $s0, $s3 ___________________________________ add $s0, $s0, $s4 ___________________________________
e: What one C++ expression might translate into the above 4 statements.
(All of Question 2d)?
_________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
1 of 5
3. For MIPS coded instructions below (Note: You can double check a and b using MARS):
a) 1010 1110 0000 1011 0000 0000 0000 0100 What instruction format is this?
Write out the instruction in full in MIPS assembly language:
b) 0000 0001 0011 0110 1011 0000 0010 0000 What instruction format is this?
Write out the instruction in full in MIPS assembly language:
c) 0000 1000 0000 0000 0000 0000 1000 0100 What instruction format is this?
What instruction is it, if you assume the address is written in hex 0x???? ). (Note: this cannot be double checked directly in MARS)
2 of 5
4. Translate the following C program into MIPS assembly language, and write it in the MIPS simulator. Take a screenshot of the code and print it. (do not email)
Document your RAT (Register Allocation Table).
Document the code.
USE THE CORRECT REGISTER CONVENTIONS Run it 3 times and print the values of X, Y and Z.
Use the 3 specific use cases below. Do not send in any others: a) X=3, Y=3
b) X=5,Y=2
c) X=1, Y=12
Submit:
A screenshot of the code.
A screenshot of each of the use-cases after running.
Hint: Other than setting up registers and printing, the code should be about 9-14 lines
if (x==y)
z = x+y;
else
if (x==(y+3))
z = x-y;
else z = 0;
cout << ¡°x= ¡° << x << ¡° y= ¡° << y << ¡° z= ¡° << z << endl;
3 of 5
5. Translate the following C program into MIPS assembly language. Build it, run it. Attach the code and print the screen showing the output. Allocate registers as you need, but document the register allocation table.
Use the proper register conventions.
int funcQ5(int myArray[], int len)
{
int i = 0;
int sum = 0;
while (i < 10)
{
sum += myArray[i++];
sum *= 2; }
return(sum);
}
void main() {
int firstVal = 90;
int secondVal = 9;
int quotient = firstVal/secondVal;
int A[10] = {0,1,2,3,4,5,6,7,8,9};
int answer = funcQ5(A,10);
cout << answer << endl; //shouldequal2026
cout << ¡°low=¡± << low << ¡°hi=¡° << hi << endl;
cout << ¡°average = ¡° << quotient << endl; // should be 10
}
Essential Resources:
Use the examples from the PowerPoint slides, MIPS programs, Green card and SYSCALL reference - all on BB.
Use the simulator for debugging! Go step by step to see how the values change.
4 of 5
A general outline of steps for Question 5:
1) Make a RAT: register allocation table (mapping from variable names to register names)
2) Set up the array with values
3) Push the values low, hi and average on the stack.
4) Call the function with 2 parameters, the base address of A, and the length.
a. Make LOOP and EXIT labels, and test when to exit
b. Calculate the offset (in bytes). 4 bytes per int, so offset = i*4 or i<<2 (shift left by 2)
c. Add the base address of A to the offset to get the actual address to be looked up in A
d. load the word currently in memory (lw) into a register
e. add the value in that register to sum
f. increment i
g. jump to top of the LOOP
h. Return the sum (in the proper register)
5) Pop the values of low and high off the stack
6) Print the values as given.
5 of 5