Assuming that the values of variables f, g, h, i, and j are stored in registers $s0, $s1, $s2, $s3, and $s4, respectively. Furthermore the base address of arrays of integers A and B are in registers $s6 and $s7 respectively.
Write MIPS assembly language code for following C language statements: f = g - A[B[4] + 2]
lw $s0, 16($s7) #$s0 = B[4]
sll $s0, $s0, 2 #$s0 = B[4] * 4
add $s0, $s6, $s0 #$s0 = A + B[4] * 4
lw $s0, 0($s0) #$s0 = A[B[4]]
sub $s0, $s1, $s0 #f = g – A[B[4]]
I found the above example online but I don't understand how it really works. Second line, isn't it shift logical left? So why is it used like this in here, doing operation $s= B[4] * 4 instead of adding 2? Third line, why is it adding instead of doing something like first line? Fourth line, 0($s0)... meaning we are getting the index 0? Why?
I was have f = h + B[g] and f = g + A[h + B[1]]. I'm sorry if this is too much question but I just don't get it.
f = g – A[B[4]]doesn't seem to matchf = g - A[B[4] + 2]... - Mooing Duck