In: Computer Science
g[8]=4[A[3j] + B[i+6j]
I and j are variables. When doing MIPS instructions for above statement what needs to be done to 4[A[3j]? Choose your own temporary and store registers. Explain show step by step
g[8] = 4[A[3j] + B[i+6j]
1. In order to expand the above statement we need to understand the Instruction Set Architecture (ISA) & Memory allocation
Example ( instruction format for load instruction )
lw $to, 4($t1)
// here $to is the destination address, $t1 inside the bracket is the base address and 4 is offset . Lets assume the base //address was 4n then the 4($t1) will correspond to 4n+4.
Please look at the table below for memory allocation
4n | 4n+1 | 4n+2 | 4n+3 | 4n+4 | 4n+5 | 4n+6 | 4n+7 | 4n+8 | 4n+9 | 4n+10 | 4n+11 | 4n+12 | 4n+13 | 4n+14 | 4n+15 |
|---------- A[0]-----------|--------------A[1]------------|--------------A[2]--------------|-------------A[3]---------------------|
2. Now that we have understood the ISA basics we can proceed to the question g[8] = 4[A[3j] + B[i+6j]
here i & j are variables so if we consider 4[A[3j] it will be expanded as A[3*j] so A[3] will be the base address with some offset to be added
so if we consider i = 1 , j = 1 , value of A will become A[3] with memory constant 12, This output willl be further multiplied with 4
B will become B[1+6] which is B[7] , Memory constant to be added will become 28
Assembly:
lw $t0, 12($s1) // A[3] is brought in to $t0 with constant offset 4 added
ori $t1, $zero, 4 // store immediate value for 4 in 4[A[3j]]
mult $t0, $t0, $t1 // multiply the value by 4
lw $t2, 28($s2) // B[7] is brought into $t1
add $t0, $to, $t2 // added and stored in $t0
sw $t0, 32($s4) // $t0 is stored into g[8]