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

Write in mips instruction. Use only add, sub, load, store . Your assembly code s

ID: 3748190 • Letter: W

Question

Write in mips instruction. Use only add, sub, load, store. Your assembly code should implement the C code directly (i.e., do not 'optimize' the C code to change the order of operations or reduce computations).  

2. a[j+2] = 4*j - a[2*j-3]);  
Try to do it as efficiently as possible (under a 1CPI assumption).

For all problems, make sure that you state the register allocation at the top of the code, using the style of our example below (for x=a+b-c):
# Register allocation:
#   x-->$s2 a-->$s0 b-->$s1 c-->$s3
#
       add $t0,$s0,$s1    # $t0 <-- a+b
       sub $s2,$t0,$s3    # x <-- a+b-c

Hints:

Prob 2: If you are having trouble with this, you should probably practice on something simpler such as a[j]=x or x=a[2*j-3].

Explanation / Answer

Please find the code below::

.data
a: 1,2,3,4,5,6,7,8,9,10 #define array a
.text
la $10,a
li $11,5 #loading j = 5
li $20,4 #size of integer

mul $16,$11,2 #get 2*j
sub $16,$16,3 #get 2*j -3
mul $16,$16,$20 #multiply by integer size
add $16,$10,$16 #equal to a[2*j-3]
lw $14,0($16) #get value from a[2*j-3]

mul $15,$11,4 #get 4*j
sub $15,$15,$14 #equal to 4*j - a[2*j-3];

addi $13,$11,2 # j+2
mul $13,$13,$20 #multiply by integer size
add $13,$10,$13 #equal to a[2+j]

sw $15,0($13) #equal to a[j+2] = 4*j - a[2*j-3]);  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote