Translate the following code into MIPS code. A[i - 6] = A[i + 2] + 10; i = i + 7
ID: 3884521 • 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 itseparate a) and b) clearly, dont mix them togetherthank you!
Explanation / Answer
li $t0,10 // 10 value decleration
li $1,7 // 7 value decleration
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 $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 explanaation:
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, $t5,loop #Compare with Loop Bound
halt:
nop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.