# Clarification on MIPS sw and lw

If I have the following code in C

A[1] = 2;


Where the starting address A[0] is $s0. addi$t0, $s0, 1 #t0->A[0] lw$t1, 0($t0) #t1->A[1] addi$t1, $t1, 2  This is where I am a bit confused. Is it necessary to add: sw$t1, 0($t1)  Or is the code fine as is? I think it is fine because, I am adding 2 to the contents of$t1, which is effectively, A[1] = 2.

• Isn't this more of a question for Stackoverflow? Mar 5, 2018 at 15:50
• In the C code, you are storing one literal into an array. In the assembly code you're adding a literal into a variable whose value is loaded from the array. Without the store instruction, this becomes x = A[1] + 2;, and with the store instruction this becomes A[1] += 2;. Neither of which is the C code you've posted. Mar 5, 2018 at 15:52
• The comments in the assembly code seem quite confusing, are you sure they say what you intended them to say? Mar 5, 2018 at 15:59
• I also think there is a typo in the store instruction, you're storing to 0($t1) instead of 0($t0) Mar 5, 2018 at 16:02
• You must have been quite confused by something. Why did you load the value of A[1] into a register? Mar 5, 2018 at 16:03

If &A is stored in $s0, then A[1] = 2 compiles to ori$t0, $0, 2 # Move 2 into$t0
sw $t0, 4($s0)      # Store $t0 into$s0 + 4 (because ints are 4 bytes)

As mentioned in the comments, your code doesn't make a lot of sense because you seem to be doing something resembling x = A[1] +2. You have to use a sw because the C code stores a value to the array.