0

so I'm currently trying to go through a for loop in MIPS and trying to multiply with it. I did the same thing with addition and it worked. Below, $t0 is the lower integer the user has entered, $t6 is the upper integer + 1, and $t4 is the register that stores the result. Here it is:

        beq $t0, $t6, resultFunction
        add $t4, $t4, $t0
        addi $t0, $t0, 1
        j addForLoop

What this does is store the lowest integer value a user has entered and then increments it to an upper integer value they have entered + 1 so that it increments correctly. It then adds appropriately and then increments the counter by 1, repeating if they are not the same. When the values are the same, it exits out of the loop. For example, if the values are 2 and 4, the answer would be 2 + 3 = 5, then 5 + 4 = 9. The loop would output with the answer 9 as it should. I tried to do this same thing but multiplying instead, and this is where my problem is. When trying to replace add with mul (like this),:

        beq $t0, $t6, resultFunction
        mul $t4, $t4, $t0
        addi $t0, $t0, 1
        j multForLoop

it is not working correctly and I'm getting 0 as the output instead. If the two integers are 2 and 4 again, it should output 24 (2 * 3 = 6, 6 * 4 = 24). I'm not quite sure why this isn't working and any help would be appreciated.

cjmurphy6
  • 1
  • 1

1 Answers1

0

You haven't shown us all your code, but presumably $t4 is 0 when the loop starts, and anything multiplied by 0 equals 0.

Set $t4 to 1 before the loop.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • Thank you. I actually had just figured it out, it was 0 and I had to implement two different for loops. The first added the lowest integer value plus 0 and stored it into $t4, and once that loop was finished, I had the for loop for multiply work correctly since $t4 was now no longer 0. – cjmurphy6 Oct 11 '20 at 07:44