# I need help understanding this MIPS code (Beginner)

Page 1 of 1

## 5 Replies - 7049 Views - Last Post: 15 September 2012 - 06:05 PM

### #1 Gantz

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 21-May 11

# I need help understanding this MIPS code (Beginner)

Posted 15 September 2012 - 02:21 PM

Assumptions

Quote

variables f,g,h,i and j are assigned to registers \$s0, \$s1, \$s2, \$s3, and \$s4 respectively

The base addresses for the arrays A and B are in registers \$s6 and \$s7

```addi \$t0, \$s6, 4    #load the forth element of array A into \$t0
add  \$t1, \$s6, \$0   #load the first element of array A into \$t1
sw   \$t1, 0(\$t0)    #store \$t0(the forth element of array A) into \$t1
lw   \$t0, 0(\$t0)    #load \$t0 into \$t0
add  \$s0, \$t1, \$t0  #add \$t1 and \$t0 into \$s0 <=> add the forth element of array A and the first element of array A, save in \$s0

```

**comments are added by me and are my attempt at understanding the code**

I really don't understand lines three and four. How did these temporary registers "become arrays"?
specifically the part '0(\$t0)' is what's confusing me. Wouldn't this imply that \$t0 is an array and we're trying to access the first element of that array?

P.S. I have to convert this code to C after, so I'll post my conversion after I've gained a better understanding of this code.

Is This A Good Question/Topic? 0

## Replies To: I need help understanding this MIPS code (Beginner)

### #2 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

## Re: I need help understanding this MIPS code (Beginner)

Posted 15 September 2012 - 02:46 PM

The problem is that you haven't specified whether this is an array of bytes or words (i.e. integers - 4 bytes). Since you're using the sw and lw (and not lb & sb) instructions, I'll assume it's an array of words.

```addi \$t0, \$s6, 4
add  \$t1, \$s6, \$0

```

1. t0 = the address of A[1]
2. t1 = the address of A[0]

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

```

1. A[1] = address of A[0]
2. t0 = A[1]

```add  \$s0, \$t1, \$t0

```

1. s0 = address of A[0] + address of A[1]

This is a really weird piece of code to have someone decipher.

Quote

specifically the part '0(\$t0)' is what's confusing me. Wouldn't this imply that \$t0 is an array and we're trying to access the first element of that array?

0(\$t0) is the 0th word at the address in \$t0. If the base of the array is at address \$t0, then 0(\$t0) references the first element, 4(\$t0) references the second, and so on. So sw \$s0, 0(\$t0) really means A[0] = contents of s0.
Was This Post Helpful? 2

### #3 sepp2k

• D.I.C Lover

Reputation: 2226
• Posts: 3,429
• Joined: 21-June 11

## Re: I need help understanding this MIPS code (Beginner)

Posted 15 September 2012 - 02:58 PM

Cross-posted here: http://www.daniweb.c...s-code-beginner

Duplicated work makes sepp2k sad :-(
Was This Post Helpful? 0

### #4 Gantz

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 21-May 11

## Re: I need help understanding this MIPS code (Beginner)

Posted 15 September 2012 - 03:23 PM

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

```

Wouldn't this make t1 point to A[1] and make t0 point to A[1] aswell?

So, I think that the equivalent code in C would be:

```
f = A[1] + A[1];

```

Thanks for the help blackcompe btw.

sepp2k, on 15 September 2012 - 02:58 PM, said:

Cross-posted here: http://www.daniweb.c...s-code-beginner

Duplicated work makes sepp2k sad :-(

Is that a Faux pas? I wasn't trying to offend anyone, and was only trying to access as many resources as possible -- I was really struggling with this question! (as sad as that sounds).
Was This Post Helpful? 0

### #5 sepp2k

• D.I.C Lover

Reputation: 2226
• Posts: 3,429
• Joined: 21-June 11

## Re: I need help understanding this MIPS code (Beginner)

Posted 15 September 2012 - 03:31 PM

Gantz, on 16 September 2012 - 12:23 AM, said:

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

```

Wouldn't this make t1 point to A[1] and make t0 point to A[1] aswell?

The value of \$t1 before those lines is the address of A[0] (or the address of A if you want). That value is not changed in the above lines because \$t1 is only read on those lines - never written to.

So, no t1 will not point to A[1]. And neither will \$t0 because the lw command takes the value stored in A[0] (which is the same value that is in \$t1, i.e. the address of A[0], because that's what has been stored there just before) and stores it in \$t0.

Quote

sepp2k, on 15 September 2012 - 02:58 PM, said:

Cross-posted here: http://www.daniweb.c...s-code-beginner

Duplicated work makes sepp2k sad :-(

Is that a Faux pas?

It leads to people answering a question without realizing that someone else already answered it on another board (as I did). Which is just an utter waste of time.

And at least on daniweb it's explicitly forbidden by the site rules. Don't know about here.
Was This Post Helpful? 1

### #6 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

## Re: I need help understanding this MIPS code (Beginner)

Posted 15 September 2012 - 06:05 PM

You can easily see what's happening by running this code through the MARS simulator. I ran it once just to make sure I was right, so you can rest assure what I said above is correct.
Was This Post Helpful? 1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }