# www.errorediridondanzaciclicodotcom

## My solution for an exercise in MIPS

Here, I would explain MY solution for this exercise I saw on a book. It’s a MIPS exercise and consists in translating the MIPS code into C code. Moreover you can check, in a second exercise, your solution in C with given numbers (hexadecimal).
Here, the assembly code:

sw \$t1, 0(\$t0)
lw \$t0, 0(\$t0)

I will use the numbers of the second exercise in order to check the final code with the right numeric solution.

\$s6 is a register that contains the base address of an array (A) and for the exercise is equal to 0x00000100. It's like to have in memory an array A as following:

Array A in memory (\$s6)
0x00000100 ..........
0x00000104 ..........
0x00000108 ..........

The code: addi \$t0, \$s6, 4 adds the value of 4 to the register \$s6 and stores it in \$t0, therefore 0x00000100 + 4 = 0x00000104 (in this case the hexadecimal addition is equal to the decimal addition). Now it’s important to underline that what we have obtained is the value of an address (an address plus a number is an address also).
In C we could write the result as \$t0=&A that means the address of the second item of the array A (remember in MIPS the addresses are byte-addresses) is put in the register \$t0.

The second line is the same except for the destination register \$t1 and for the address that is now the first (0). What we have now is the base address of A, &A, stored in \$t1, so \$t1 = &A = 0x00000100.

Third line: sw \$t1, 0(\$t0), it stores the content of \$t1 in the first position of the array that has base address specified in \$t0. This register has been previously (line 1) initialized with the address of 0x00000104. So, we are now taking the content of \$t1 (0x00000100) and storing it in memory at address specified in 0(\$t0). This address (0x00000104) will now receive the value 0x00000100. That means, in C, A=0x00000100.

Array A in memory (\$t0)
0x00000104 0x00000100
0x00000108 ..........

Fourth line: lw \$t0, 0(\$t0), load the content (value) of first element of the array as specified in 0(\$t0) in register \$t0, so the register \$t0 is now equal to 0x00000100. The array A, at the base address that is 0x00000104, has the value of 0x00000100 (we just stored it previously). It’s like:

Array A in memory (\$t0)