Starting from:

$30

Homework assignment 2 Solution

Exercises:

2.9 [4 points] Translate the following C code to MIPS. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively. Assume that the elements of the arrays A and B are 4-byte words:




B[8] = A[i] + A[j];




2.12 [4 points] Assume that registers $s0 and $s1 hold the values 0x80000000 and 0xD0000000, respectively.




2.12.1 What is the value of $t0 for the following assembly code?

add $t0, $s0, $s1




2.12.2 Is the result in $t0 the desired result, or has there been overflow?




2.12.3 For the contents of registers $s0 and $s1 as specified above, what is the value of $t0 for the following assembly code?

sub $t0, $s0, $s1




2.12.4 Is the result in $t0 the desired result, or has there been overflow?




2.13 [4 points] Assume that $s0 holds the value 12810




2.13.1 For the instruction add $t0, $s0, $s1, what is the range(s) of values for $s1 that would result in overflow?




2.13.2 For the instruction sub $t0, $s0, $s1, what is the range(s) of values for $s1 that would result in overflow?




2.27 [6 points] Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds the base address of the array D.




for(i=0; i<a; i++)

for(j=0; j<b; j++)

D[4*j] = i + j;




2.28 [3 points] How many MIPS instructions does it take to implement the C code from Exercise 2.27? If the variables a and b are initialized to 10 and 1 and all elements of D are initially 0, what is the total number of MIPS instructions that is executed to complete the loop?




2.29 [5 points] Translate the following loop into C. Assume that the C-level integer i is held in register $t1, $s2 holds the C-level integer called result, and $s0 holds the base address of the integer MemArray.




addi $t1, $0, $0




LOOP: lw $s1, 0($s0)




add $s2, $s2, $s1




addi $s0, $s0, 4




addi $t1, $t1, 1




slti $t2, $t1, 100




bne $t2, $s0, LOOP




3.1 [1 point] What is 5ED4 - 07A4 when these values represent unsigned 16- bit hexadecimal numbers? The result should be written in hexadecimal. Show your work.




3.5 [1 point] What is 4365 - 3412 when these values represent signed 12-bit octal numbers stored in sign-magnitude format? The result should be written in octal. Show your work.




Computer Project:




[4 points] Write a MIPS code to compute the factorial of a positive integer. Assume that the integer is to be given as input by the user. You have to output the factorial of the number as



The factorial of the given integer xxx is xxx




[8 points] Write a MIPS code to arrange the given set of integers in descending order. You may either assume that the set of integers are provided as initialization of array elements (or) you may ask the user to input the set of integers. You must have the output as



The given integers arranged in descending order is




xx

yy

z

More products