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

HELP PLEASE I WILL GIVE U THUMPS UP 2.3 (10 pts) [5] Translate the following C l

ID: 3744893 • Letter: H

Question

HELP PLEASE I WILL GIVE U THUMPS UP

2.3 (10 pts) [5] Translate the following C language instruction to MIPS assembly language:

C[k] = A[i]+B[i-j];

This will require multiple MIPS commands: you will need to add, subtract, load, store, and multiply. For this problem we will use the conventional register names that we would see in real MIPS assembly code. There is a table here: http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch05s03.html#mips_regs_convention

Let us try and stay within $s0-$s7 for the starting addresses for the three arrays and the indices (i, j, k).

If we need temporary regisers for intermediate values, use any of the eight registers $t0 through $t7.

A command reference is found in Appendix A, section 10 (which is included in your zyBook).

The formulas behind each command can be found in the MIPS Reference Data card (found at mips_reference_data.pdf in the Extras & Helpful Info folder on Google Drive.

Assume that the variables have been loaded as follows into registers:

Variable

Register name

unused

$s0

i

$s1

j

$s2

k

$s3

unused

$s4

A

$s5

B

$s6

C

$s7

Let’s talk about these registers a little more. For i, j, and k, the values in the $s registers are signed integers used as indices into the arrays.  However, we’re on a 32-bit architecture in this book with MIPS, which is 4 bytes. Each array element thus takes 4 bytes. Therefore, we must multiply the index values by 4 before adding them to the base addresses for the arrays.

There are two ways to multiply by 4: muland sll. If you shift left with sll by 2 bits, it’s the same as multiplying by 4.

The contents of $s5, $s6 and $s7 is a positive integer that is an address of a specific bytein RAM where the array begins. Each element takes up 4 bytes, thus the need to hop by 4.  

Offset commands are very helpful when indexing arrays or stacks, because they let you add or subtract right on the command line. 4($s0) means add 4 bytes to the address in $s0.  -8($s0) means .

If we load or store a word using lw or sw, we need to only give the starting byte address; the system will take care of pulling or pushing the 4-byte block.

Let’s go step-by-step.

Again the equation:

C[k] = A[i]+B[i-j];

For A[i]:

a. Put the value of iin a temporary register

b. Multiply it by 4 using slland shifting left by 2 bits, store it in your temp register.

c. Add the result to the base address for A, in $s5, and store it in your temp register.

d. Load the contents of that location from memory using lw, and place the result in a temp register.

For B[i-j]:

a. Subtract j from i by subtracting $s2 by $s1. Store the result in a temp register.

b. Multiply it by 4.

c. Add it to the starting address for array B, found in $s6.

d. Load that word from memory and store in a temp register.

For C[k]:

a. Calculate k*4, add it to C’s base address and store the resulting memory byte address in a temp variable.

b. Add the two parts from above from arrays A and B, and store in a temp register.

c. Store the result in the address from (a).

Variable

Register name

unused

$s0

i

$s1

j

$s2

k

$s3

unused

$s4

A

$s5

B

$s6

C

$s7

Explanation / Answer

C[k] = A[i]+B[i-j];

For the above statement, the MIPS code is given below

#steps to get the value of A[i] into $t2
move $t1, $s1
sll $t1, $t1, 2
add $t1, $s5, $t1
lw $t2, ($t1)


#steps to get the value of B[i-j] into $t3
sub $t1, $s1, $s2
sll $t1, $t1, 2
add $t1, $s6, $t1
lw $t3, ($t1)


#calculate address of C[k] and store in $t1
move $t1, $s3
sll $t1, $t1,2
add $t1, $s7, $t1

#add A[i]+B[i-j] and store in C[k]
add $t2, $t2, $t3
sw $t2, ($t1)

====================================================================================
If you need detailed conversion for each step given by instructor, the break up is given below. The above is nothing all these instructions put together.
You need to only submit the lines given above. The break up below is for your understanding
=====================================================================================

# a. Put the value of i in a temporary register
move $t1, $s1

#b. Multiply it by 4 using slland shifting left by 2 bits, store it in your temp register.
sll $t1, $t1, 2

#c. Add the result to the base address for A, in $s5, and store it in your temp register.
add $t1, $s5, $t1

#d. Load the contents of that location from memory using lw, and place the result in a temp register.
lw $t2, ($t1)


#For B[i-j]:
#a. Subtract j from i by subtracting $s2 by $s1. Store the result in a temp register.
sub $t1, $s1, $s2

#b. Multiply it by 4.
sll $t1, $t1, 2

#c. Add it to the starting address for array B, found in $s6.
add $t1, $s6, $t1


#d. Load that word from memory and store in a temp register.
lw $t3, ($t1)


#For C[k]:
#a. Calculate k*4, add it to C’s base address and store the resulting memory byte address in a temp variable.
move $t1, $s3
sll $t1,$t1, 2
add $t1, $s7, $t1

#b. Add the two parts from above from arrays A and B, and store in a temp register.
add $t2, $t2, $t3

#c. Store the result in the address from (a).
sw $t2, ($t1)