MIPS Differences among manipulating array address using add, addu, and addi - mips

I am a newcomer to MIPS and I am confused with the following 3 operations when it comes to accessing elements in an array. For example, if I want to change the value of an integer array at index 1 to 6 and right now I only get $s0 for the base address of the array, A[], then what should I do? Code example is like this:
addi $t0, $zero, 4
add $s0, $s0, $t0
addi ($s0), $zero, 6
or
addi $t0, $zero, 4
addu $s0, $s0, $t0
addi ($s0), $zero, 6
or
addi $s0, $s0, 4
addi ($s0), $zero, 6
Could you explain the reason and point out special cases as well.

Related

How do I work with integers that result in bigger than 10 with MIPS?

I am trying to make a Fibonacci sequence with MIPS and this is the code I have so far.
main:
li $t1, 0
li $t2, 1
add $t3, $t1, $t2
loop:
add $t3, $t1, $t2
la $t1, ($t3)
add $t3, $t1, $t2
la $t2, ($t3)
j loop
It works fine until it gets to 8, it should then be adding 5 to make 13 but instead the register shows 0x0000000d, then it completely messes up the sequence, somehow a 15 comes up then 22 then 37.
I'm guessing it has something to do with the carry but how would I actually deal with this so that it carries on with the sequence properly?
Thank you

Binary value of a line of code in mips

Hello everyone I am having trouble figuring this out, the question wants me to give the value of the line of code in a binary value of the offset field not too sure what to do please help.
add $t0, $zero, $zero
addi $a0, $zero, 21
loop:
beq $a0, $zero, end
add $t0, $t0, $a0
addi $a0, $a0, -3
j loop
end
For beq $a0, $zero, end give the binary value of the offset field. Briefly explain.

When does stall take place in this code?

Loop: lw $t2, 0($t1)
add $t3, $t2, $s1
sw $t3, 4($t1)
add $t1, $t1, $t9
bne $t1, $t0, Loop
and $s2, $s4, $s5
I have interpreted that stall takes place in second instruction provided that this loop runs 8 times and we are in third iteration. Can stall take place in any more situation?

MIPS store word/load word

have a really basic question here.
Can a register have both a value and an address. As in assuming i want to swap between values: 5 stored in t0 and 7 stored in t1
does this code work:
sw $t0, 0($t0)
sw $t1, 0($t1)
lw $t1, 0 ($t0)
lw $t0, 0 ($t1)
Sorry this might sound stupid
Not really for all values, as sw and lw need proper alignment (valid addresses should be multiple of 4).
That is, your code would only work for values multiple of 4, and anyways it would be a bad idea to do so, because you would be basically write garbage on whichever address you are pointing at.
To swap registers without overwriting a third register you can use the following trick:
xor $t0, $t0, $t1
xor $t1, $t0, $t1
xor $t0, $t0, $t1

MIPS Programming in Past Paper

I'm very confused about a question in a university past paper. It is as follows:
What is the value in register $s1 after executing the following piece of MIPS
assembly code?
li $t0, 0x1
li $s0, 0x0
li $s1, 0xa5a5a5a5
loop: and $t1, $t0, $s1
beq $t1, $zero, skip
addi $s0, $s0, 1
skip: sll $t0, $t0, 1 # Shift left logical
bne $t0, $zero, loop
(a) 0x10
(b) 0xa5a5a5a5
(c) 0x0
(d) 0x5a5a5a5a
(e) 0x1
The given answer is A - now, as far as I'm aware, the value of $s1 is not changed after its initial declaration - so how is this the case? I'd have thought it would be B?
This shows the QTSpim:
This shows the PCSpim: