2 Replies - 1055 Views - Last Post: 07 February 2013 - 07:29 PM

#1 userforgotten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 13

Quick MIPS question regarding multiplication

Posted 06 February 2013 - 08:26 PM

Hi; I know I have to multiply a register's value by 4, then add it to the address of an array to get the address of the [register's value]th element. We were taught to do the multiplication this way:

add $t0, $t0, $t0
add $t0, $t0, $t0

($t0 stores the # of the element)

However, my professor insists that we write code using as few instructions as possible. Thus, could I change the above two lines to:

sll $t0, $t0, 2

Is This A Good Question/Topic? 0
  • +

Replies To: Quick MIPS question regarding multiplication

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,488
  • Joined: 05-May 05

Re: Quick MIPS question regarding multiplication

Posted 06 February 2013 - 10:22 PM

Well, you could of just ran the code to confirm the answer to your question, but yes that would work. Shifting a binary string to the left is the same as multiplying it by 2. Shifting to the right, divides by 2. Alternatively, you could have a mult instruction, but that's actually more work.

This post has been edited by blackcompe: 06 February 2013 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 userforgotten  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 13

Re: Quick MIPS question regarding multiplication

Posted 07 February 2013 - 07:29 PM

Ok, thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1