2

I'm a newcomer to MIPS and am having a hard time figuring our what this snip of code means... Where $s6 is the start of an array A:

addi $t0,$s6,4

I'm not sure if this means $t0 = A[4] -or- If it means $t0 = A[0] + 4, take the value in A[0] and add four to it saving it back into A[0]

Any help would be really appreciated.

Thank you in advance!!

melMPLS
  • 345
  • 5
  • 14
  • Probably this instruction is part of [How to understand this basic Assembly Code that seems to be adding two pointers?](https://stackoverflow.com/a/66807155) – Peter Cordes Mar 25 '21 at 20:46

3 Answers3

1

This instruction means you are taking the value stored in $s6 and adding 4 to it and storing that new value in $t0. The value in $s6 remains the same. $s6 is a saved temp register so any time you need to change the value in $s_ you need to save it to the stack first. You are not changeing the value in $s6 so you do not need to save it to the stack. You are saving it to a temp register that you do not have to worry about what contents you have over written. Also $s6 is actualy Regester 22 or $R22 and $t0 is $r8....

You are basicly increasing the array by one 32 bit word to the next instruction/data word.

This is in response to your comment:

By you saying A[0] = 7 by convention you are saying that the "memory address" referenced by the contents of register $s6, that equals 7. $s6 does not equal 7 but what it points to in main memory does. s6 just holds a number that we interpret or know is an address. We add 4 to $s6 because we want to add enough bytes to get to the next element of the array. One word is 32 bits is 4 bytes. By convention we want to add 4 and it will be in bytes to get to the next word. A word is generally 32 bits or 4 bytes long. After adding the 4 bytes to s6 we do not know the result because we were not told what s6 originally was but we do know that t0 now points in essence to s6[1]. I would not say your are adding another indice to the array. The array would of already been set up or you are going to possible read past the end but mips as i know it does not give you an error. It will just tell you what is in the spot in main memory referenced by the address you give it. Could be blank , valid, stail, or another part of your programs data. Would need to know more of the context of the program to know more.

aaron burns
  • 267
  • 4
  • 15
  • Thank you. You had me up to the last sentence. So I'm clear, let's say A[0] = 7 addi $t0, $s6, 4 Means $t0 = 11 The value of A[0] would remain unchanged at 7 I'm not adding another indicie to the Array right? – melMPLS May 06 '12 at 17:16
1

The line:

addi $t0,$s6,4

where $s6 is the base of the array, is taking the contents of register $s6, adding 4 to them and putting the result into $t0. Thus, if the address in $s6 is 0x00400000, after the addi instruction, $t0 would contain 0x00400004.

The addi instruction adds an immediate value, i.e. an integer value, to the source register and stores the result in the destination register, in this case $t0.

So if $s6 is A[0], then $t0 becomes A[1], assuming you have an integer array. If you have a character array, i.e. a string, then $t0 becomes A[3]. $t0 does not hold the value at these array indices, it holds the address of these array indices, because that is what $s6 originally held.

0

Most MIPS instructions are:

instruction $destination, $operands

This concrete instruction, having the memory adress of the start of the array A[] on $s6, stores the position of the 4th byte of A[] on $t0.

As I understand from what you wrote, in $s6 you have the memory adress of A[0], not the value A[0]

That is to say, if $s6 = @A[0] is the adress value 0x00, then $t0 = $s6+4 has the value 0x04

Depending of the type of the array A[] (short, int, long) this could mean you are pointing diferent positions of the array in $t0.

In the normal case of an array of 4 byte long integers, $t0 would be now pointing A[1]

Fonserbc
  • 23
  • 1
  • 4
  • sw (store word) and lw(load word) do not follow the convention you said. SW R1 R2 will take the value in r1 and store it in the main memory(ram) address refrenced in r2. lw r1 r2 takes the contents of main memory refrenced by r2 and stores it in r1. – aaron burns May 06 '12 at 19:25
  • @aaronburns You are right, just edited it to not cause confusion. Thanks – Fonserbc May 06 '12 at 20:46
  • I believe I'm understanding more now...I mistaken thinking that I was adding to the contents of the array at a specific indicie. In the case of adding 4 to an Array's base index I'm advancing to the next word or indicie in the array. Thanks all for your patience and time to walk me through this! much appreciated. – melMPLS May 07 '12 at 05:00