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

Translate the following code into MIPS code. A[i - 6] = A[i + 2] + 10; i = i + 7

ID: 3883376 • Letter: T

Question

Translate the following code into MIPS code.

A[i - 6] = A[i + 2] + 10;

i = i + 7;
A[3] = A[i - 1];

a) Assume A is an array of integers (each integer takes 4 bytes). A's address
is stored at register $10. Also assume that the compiler associates the
variable i to the register $11.


b) Assume A is an array of characters (each character takes one byte). A's address
is stored at register $10. Also assume that the compiler associates the variable i to   the register $11.

Please show me every step of how to solve it but not just answers. thank you!

Explanation / Answer

li $t0,10 // 10 value declaration
li $t1,7 // 7 value declaration
lw $t2, 0($a1) // load A[i+2]
lw $t3, 0($a1) // A[i - 1] code
add $t4,$t1,$t0 //A[i - 6] = A[i + 2] + 10;
add i $t1,7 //i=i+7 code
sw $t3, 4($a1) //A[3] = A[i - 1];  

Example:

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

{

C[i] = A[i + 1] - A[i] * B[i + 2]

}

MIPS code with explanation:

--------------------------------------

li $s0, 0xA000 # Load Address of A
li $s1, 0xB000 # Load Address of B
li $s2, 0xC000 # Load Address of C
li $t0, 0 # Starting index of i
li $t5, 98 # Loop bound
loop:
lw $t1, 0($s1) # Load A[i]
lw $t2, 8($s2) # Load B[i+2]
mul $t3, $t1, $t2 # A[i] * B[i+2]
lw $t1, 4($s1) # Load A[i+1]
add $t2, $t1, $t3 # A[i+1] + A[i]*B[i+2]
sw $t2, 4($s3) # C[i] = A[i+1] + A[i]*B[i+2]
addi $s1, 4 # Go to A[i+1]
addi $s2, 4 # Go to B[i+1]
addi $s3, 4 # Go to C[i+1]
addi $t0, 1 # Increment index variable
bne $t0, $t5, loop # Compare with Loop Bound
halt:
nop

EXAMPLE 2:

int A[100], B[100];

for (i=1; i < 100; i++) {

A[i] = A[i-1] + B[i];

}

MIPS CODE :

li $t0, 1 # Starting index of i
li $t5, 100 # Loop bound
loop:
lw $t1, 0($a1) # Load A[i-1]
lw $t2, 4($a2) # Load B[i]
add $t3, $t1, $t2 # A[i-1] + B[i]
sw $t3, 4($a1) # A[i] = A[i-1] + B[i]
addi $a1, 4 # Go to i+1
addi $a2, 4 # Go to i+1
addi $t0, 1 # Increment index variable
bne $t0, $t5, loop # Compare with Loop Bound
halt:
nop

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