0

I am trying to use Mips Mars to create assembly code that uses bit shifting and a loop to multiply any number up to 32 bits. But it is only iterating twice if that.

I have already gotten the multiplier and multiplicand earlier in the program.

$t0 = multiplicand

$t1 = Multiplier

#Multiply operation
li $t3 0 #Counter
li $t4 0 #result
li $t6 0x80000000
myLoop:
    bgt $t3, 31, showMessage

    addi $t3, $t3, 1 #Keep track of counter $t3
    andi $t7, $t1, 1 #Find lsb of multiplier
    andi $t8, $t6, 1 #Find LSB $t6
    and $t5, $t8, $t7 #And LSB of multiplier and lsb $t6
    sll $t6, $t6, 1 #shift $t6 right 1
    srl $t1, $t1, 1 #Shift multiplier right 1


    blez $t7, myLoop
    add $t4, $t0,0  #Result

    sll $t0, $t0, 1 #Shift multiplicand left one
    j myLoop

I expect the output of 4*3 to be 12 but it is 8.

Jakekone23
  • 21
  • 5
  • 1
    It's unclear what `$t6` and hence `$t8` and `$t5` are but they are unused so don't affect result. Your problem seems to be `add $t4, $t0, 0` which really wants to add to the result so should be `add $t4, $t4, $t0`. PS: next time use your simulator to single step and see what is happening. – Jester Sep 24 '19 at 18:38
  • That did help but it doesn't work for some number combinations. If I input 5*4 it says the answer is 5 – Jakekone23 Sep 24 '19 at 19:36
  • 1
    `blez $t7, myLoop` should not go back to `myLoop` because it will skip the `sll $t0, $t0, 1` which is still needed. – Jester Sep 24 '19 at 19:54

0 Answers0