-2

I am just starting to learn assembly and having trouble getting this code to work so if anyone could help me out that would me much appreciated.

main:
ori $5,$0,x 
addu $10,$0,$5
sll $5,3,shft 
addu $10, $0, $5
sll $5,2,shft 
addu $10,$0 ,$5 

"Write a program that computes 13*x. Do this by using register $5 for x and loading it at the beginning of the program with an ori instruction. Assume x is a positive integer represented in unsigned binary. Compute 13*x by shifting and adding. Don't use an explicit multiply instruction. Don't do input or output. Put the result in register $10 at the end of the program. Write the program so that the value for x can easily be changed."

markgz
  • 6,054
  • 1
  • 19
  • 41
user1984103
  • 57
  • 1
  • 3
  • 6
  • Write a program that computes 13*x. Do this by using register $5 for x and loading it at the beginning of the program with an ori instruction. Assume x is a positive integer represented in unsigned binary. Compute 13*x by shifting and adding. Don't use an explicit multiply instruction. Don't do input or output. Put the result in register $10 at the end of the program. Write the program so that the value for x can easily be changed. – user1984103 Feb 17 '13 at 04:12
  • 2
    What it comes down to is: 13x = 8x + 4x + x. – Jerry Coffin Feb 17 '13 at 04:14

1 Answers1

2

Here are the basic steps. Since 13x is the same as (8 + 4 + 1)x, you can do this by shifting and adding thus (pseudo-code):

move x to accumulator.

shift x left twice, it is now 4x.
add x to accumulator.

shift x left once more, it is now 8x.
add x to accumulator.

Now the accumulator holds 13x. All you have to do is figure out the correct MIPS instructions to do that.


I'll tell you a couple of things you're doing wrong, though vaguely since this looks like homework :-)

Modifications to $5 should be done in the order I've specified above. If you shift it three to the left, then another two to the left, that's multiplying it by 25, or 32.

In addition, you may want to check the order of some of those arguments. They don't seem to gel with what is expected.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953