Assumptions

variables f,g,h,i and j are assigned to registers \$s0, \$s1, \$s2, \$s3, and \$s4 respectively

The base addresses for the arrays A and B are in registers \$s6 and \$s7

``````addi \$t0, \$s6, 4    #load the forth element of array A into \$t0
add  \$t1, \$s6, \$0   #load the first element of array A into \$t1
sw   \$t1, 0(\$t0)    #store \$t0(the forth element of array A) into \$t1
lw   \$t0, 0(\$t0)    #load \$t0 into \$t0
``````

comments are added by me and are my attempt at understanding the code

I really don't understand lines three and four. How did these temporary registers "become arrays"?
specifically the part '0(\$t0)' is what's confusing me. Wouldn't this imply that \$t0 is an array and we're trying to access the first element of that array?

P.S. I have to convert this code to C after, so I'll post my conversion after I've gained a better understanding of this code.

## All 3 Replies

The first two lines don't load anything. You'd need a load instruction for that. They simply perform pointer arithmetic.

The first line makes `\$t0` point to the fifth byte of the A array. If A is an array of 32-bit values, that means that `\$t0` points to the second element of the array - not the fourth or fifth.

The third line takes the value that's in the register \$t1 (i.e. the address of A) and stores it in the memory location that `\$t0` points to, i.e. it stores it as the first element of A.

The fourth lines takes the second element of `A` (which has been set on the previous line to be the address of `A`) and stores it in the register `\$t0` (which previously stored the address of that element).

You seem to understand the fifth line fine.

So, I think the equivalent cod in C would be

``````f = A[1] + A[1];
``````

Since the same question has been asked on dreamincode, I'll stop responding here. There's no point in having the same discussion twice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.