Up until now, we\'ve been accessing arrays with constant indices by multiplying
ID: 3883834 • Letter: U
Question
Up until now, we've been accessing arrays with constant indices by multiplying the index by 4 and offsetting the base address of the array. We can accomplish the same thing with for a variable index i by observing that 4i = i + i + i + i Use this insight to write an assembly code for the C statement below K[i] = 710 where, base address of K is passed to you in $s0 and the value of i is passed to you m $s2 Assuming the base address of a certain array A is passed to you in $s3, what would be the result of the following lines of code? addi $t0, $zero, 867 addi $s2, $s3, 16 sw $t0, 0($s2)Explanation / Answer
A[0] - 3 at address 32
A[1] - 6 at address 36
A[2] - 8 at address 40
A[3] - 2 at address 44
and address of $s0 is A[0] , i.e 32
lw $s1, 0($s0) - This means that get the address of $s0 and add an offset of 0 and then load it in $s1.
So $s1= $s0(address=32) + 0 = A[0] = 3. Hence $s0 now holds 3.
lw $s2, 8($s0) - This means get the address of $s1 and add 8 to it, then get the values at that location and store it in $s2. Hence $s2 will now hold , $s2 = $s0(address = 32) + 8 = A[2](address = 40) = 8
add $s3, $s1, $s2 - This means that add $s2 and $s1 and store the result in $s3.
Hence $s3 will now hold 11
So ,
$s0 = 3
$s1 = 3
$s2 = 8
$s3 = 11
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.