the problem is with solution. I do not understand how tehe method to translate f
ID: 3728527 • Letter: T
Question
the problem is with solution. I do not understand how tehe method to translate from C to MIPS. please I need explanation in details for each character in solution.
thanks
Explanation / Answer
PLEASE FIND THE ANSWE BELOW:
j = a[2]
j+=2
a[10] =j;
To access a[2]. Need the base address of a that is stored in $a0 as shown in table.
now each integer in mips will be of size 4.
So a[0] will store at base address
a[1] will store at base address + 4
a[2] will store at base address + 8
So by doing 8($a0) can access a[2]
to get some value from address need load word lw instruction
that's why j = a[2] will be lw $s2,8($a0)
Now j+=2 increasing j by 2
so same can be done by adding 2 in $s2
that why j+=2 will be addi $s2,$s2,2
again to access a[10] == base address of a + 40 (4 bit for each integer)
and to store at address will use save word sw instruction.
that why a[10] = j will be sw $s2,40($a0)
So final conversion will be
lw $s2,8($a0)
$s2,$s2,2
sw $s2,40($a0)
B)
i=a[j]
a[k]=i;
first a[j] can be accessed by base address of a + ( j * 4) (to convert address for integer)
so doing shift left(sll) one time will multiply number by 2
so to multiply number by 4 will do sll 2 time and will store the result to temp register $t0
so j can be converted to integer index like below
sll $t0,$s2,2 # do the left shift twice and store result to $t0
now add the address to base address
add $t0,$t0,$a0
so instruction i = a[j] will be
lw $s1,0($t0)
again
a[k] will be like
sll $t0,$s3,2 #store k address to t0
add $t0,$t0,$a0 #add address to base address
sw $s1, 0($t0)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.