Trying to understand to MIPS code

Not sure how to interpret this line of code.

Page 1 of 1

3 Replies - 4665 Views - Last Post: 08 February 2009 - 06:45 PM Rate Topic: -----

#1 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Trying to understand to MIPS code

Post icon  Posted 08 February 2009 - 01:41 PM

HI,
I was reading my book on MIPS and found this code in C translated in MIPS but was not sure about some line of code.
I have written my question and my understanding in red next to code. If anyone can explain me how this works, I would really appreciate it.


Code in C 

void swap (int v[], int k)
{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}

code in MIPS

$a0, $a1, $t0 #This would be register allocations. $a0, $a1 for v[] and k, since they are parameters & $t0 for temp.[/color]

sll $t1, $a1, 2  [color=#FF0000]1)why are we multiplying with 2. I know that a word in MIPS is 4 bytes, so for addressing we need to multiply by 4 but the code is multiplying by 2. How can we multiply by any number without knoing its actual value?
2)So how would we know if a variable (as in this case) is a multiple of 4 or not just by looking at it?
3)Also how did register $t0 become $t1?
4) Lastly, why are we shifting left? Why can't we just multiply by 4 if we need proper addressing? [/color]
add $t1, $a0, $t1 
lw $t0, 0($t1)
lw $t2,, 4($t1)
sw $t2, 0($t1)
sw $t0, 4($t1)






Thanks.

This post has been edited by jingoria: 08 February 2009 - 05:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to understand to MIPS code

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Trying to understand to MIPS code

Posted 08 February 2009 - 04:57 PM

Can you please post more of the code listing?
Was This Post Helpful? 0
  • +
  • -

#3 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Trying to understand to MIPS code

Posted 08 February 2009 - 05:17 PM

View Postbsaunders, on 8 Feb, 2009 - 03:57 PM, said:

Can you please post more of the code listing?


I have posted the whole code now.
Was This Post Helpful? 0
  • +
  • -

#4 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Trying to understand to MIPS code

Posted 08 February 2009 - 06:45 PM

The first part of the code multiplies the value stored in register $a1 (k in the C code) by 4 and stores the result in register $t1.

sll $t1, $a1, 2


It does this by shifting the bits in $a1 two places to the left (sll stands for Shift Left Logical). This effectively multiples $a1 by 4. For example, if $a1 is 5, this is how it looks in binary:

00000000 00000000 00000000 00000101

If I shift it 2 bits to the left (hence the 2 in the sll instruction), it is multiplied by 4. It is shifted by adding zeros to the right end of the number:

00000000 00000000 00000000 00010100

Now the value is 5 x 4 = 20 (10100 binary) and it is stored in register $t1 as an address.

Now why must $a1 by multiplied by 4? In the C code, k is an index into array v. Suppose this is a definition of array v:

int v[4] = {34, 72, 11, 9};


Let's see how that would look in memory:

v =
00000000 00000000 00000000 00100010 (= 34)
00000000 00000000 00000000 01001000 (= 72)
00000000 00000000 00000000 00001011 (= 11)
00000000 00000000 00000000 00000000 (= 9)


Now, if I wanted to access the third integer in array v (11), in the C code, I would type v[k], wherek is 2. The C compiler converts the index k into an byte offset from the first byte in v. So, how many bytes from the first byte in v is the integer 11? Firstly, how many bytes are in an integer? There are 4 bytes (32 bits) in an integer (one word). So, to find the byte offset, we multiply the index k (which in the MIPS code is in register $a1) by 4. To find the offset of the third integer, I multiple the index 2 by 4 giving 8, meaning the third integer of array v is 8 bytes from the first byte:

v =
1 byte 2 bytes 3 bytes 4 bytes
00000000 00000000 00000000 00100010 (= 34)
5 bytes 5 bytes 7 bytes 8 bytes
00000000 00000000 00000000 01001000 (= 72)
00000000 00000000 00000000 00001011 (= 11)
00000000 00000000 00000000 00000000 (= 9)


That's why in the MIP code, the value in register $a0 was multiplied by 4 are stored in $t1.
Was This Post Helpful? 1

Page 1 of 1