Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Convert the following C program to MIPS program. Assuming that i, j, k, f, ar

ID: 3532279 • Letter: 1

Question

1. Convert the following C program to MIPS program. Assuming that i, j, k, f, are stored in registers $s0, $s1, $s2, $s3 already. The base address of arrays A and B are in registers $s4 and $s5, respectively.

1) f = k + A[5];

2) B[8] = A[i - 3];

3) f = A[B[i]-10] + 3;

2. Consider the following C program

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

                  a = a + b;

1)Draw a flowchart for the above program

2) Translate the C program to MIPS assembly code. Assume that the values of a, b, i are in registers $s0, $s1, $t0, respectively.

3) How many MIPS instructions does it take to implement the above C program? What is the total number of MIPS instructions that is executed to complete the program?

3. Convert the following C procedure into MIPS assembly code.

1)

int compare(int a, int b)

{

                  if (a < b)

                                    return 1;

                  else

                                    return 0;

}

2)

int compare(int a, int b)

{

                  if (subit (a, b) <0)

                                    return 1;

                  else

                                    return 0;

}

int subit (int a, int b)

{

                return a-b;

}

Explanation / Answer

A)
li $t2, 4 # put the index into $t2
add $t2, $t2, $t2 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s4 # combine the two components of the address
lw $t4, 0($t1) # get the value from the array cell
sub $s3 , $s2 , $t4 #Subtraction with result in $s3

B)
sub $t2,$s0,$s1 # $t5 = i-j
add $t2, $t2, $t2 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s4 # combine the two components of the address
lw $t4, 0($t1) # get the value from the array cell

li $t2, 8 # put the index into $t2
add $t2, $t2, $t2 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s5 # combine the two components of the address

move $t1,$t4 # B[8]=A[i-j]

C)

add $t2, $s0, $s0 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s5 # combine the two components of the address .i.e B[i]

add $t2, $t1, $t1 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s4 # combine the two components of the address .i.e A[B[i]]

li $t2 , 3
add $s3 , $t1 , $t2 # f = A[B[i]] + 3; A)
li $t2, 4 # put the index into $t2
add $t2, $t2, $t2 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s4 # combine the two components of the address
lw $t4, 0($t1) # get the value from the array cell
sub $s3 , $s2 , $t4 #Subtraction with result in $s3

B)
sub $t2,$s0,$s1 # $t5 = i-j
add $t2, $t2, $t2 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s4 # combine the two components of the address
lw $t4, 0($t1) # get the value from the array cell

li $t2, 8 # put the index into $t2
add $t2, $t2, $t2 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s5 # combine the two components of the address

move $t1,$t4 # B[8]=A[i-j]

C)

add $t2, $s0, $s0 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s5 # combine the two components of the address .i.e B[i]

add $t2, $t1, $t1 # double the index
add $t2, $t2, $t2 # double the index again (now 4x)
add $t1, $t2, $s4 # combine the two components of the address .i.e A[B[i]]

li $t2 , 3
add $s3 , $t1 , $t2 # f = A[B[i]] + 3;