0
votes

i have following mips code and i dont fully understand arrays in mips.The program calculates the arithmetic mean of 4 array values and overwrites the first value of the array.

I know you use the register to point to a address, here in the code line 0:ADDI $2, $0, 0 $2 would point to the beginning of the array. With line 3:LW $3, 1000($2) you will get the value and not the address of the pointer, right?. If it adds all array values together shouldnt line 4:ADD $7, $7, $3 be false? isnt it that this line adds the value in $3 to an adresses in $7 which is the beginning address of the array and not the value? how do you know when you adding addresses and when values? Hope you can clear my confusion.

0:ADDI $2, $0, 0

1:ADDI $7, $0, 0

2:ADDI $4, $0, 16

LOOP:

3:LW $3, 1000($2)

4:ADD $7, $7, $3

5:ADDI $2, $2, 4

6:BNE $2, $4, LOOP

7:SRL $7, $7, 2

8:SW $7, 1000($0)

1

1 Answers

0
votes

An address is a value (i.e. a collection of bits) just like any other. That value may be treated as an address depending on the context, but there's nothing inherent about the value 0 or 0x12345678 that makes them addresses or non-addresses.

So the responsibility of deciding when something could and should be used as an address is put on you as the programmer (or on the compiler/interpreter in some languages).

If we take your question "shouldnt line 4:ADD $7, $7, $3 be [incorrect]?": The register $7 appears to be used as a sum in this code. It is initialized to 0, and then each value in the array is added to it.