MIPS multiplication via addition
I'm currently using QtSpim for a MIPS program. The objective is to use
addition to multiply. This is my code which is relatively straightforward:
.data
inputX: .word 5
inputY: .word 4
tempX: .word 0
constantOne: .word 1
finalX: .word 0
.text
main:
lw $t1, inputX
lw $t2, inputY
lw $t0, tempX
lw $t3, constantOne
lw $t4, finalX
beq $t2, $zero, Exit #when Y equals zero, X is already zero so exits.
Loop: beq $t2, $t3, yIsOne #when y is one skip rest of loop
add $t0, $t1, $t1 #actual "multiplication".
sub $t2, $t2, $t3 #what makes loop continue
bne $t2, $zero, Loop #while Y isnt yet 0.
yIsOne: add $t0, $t0, $t1 #adds X to 0 or the multiplied Xs
Exit:
sw $t4, finalX
When I run the program, I get this:
R8 [t0] = f
R9 [t1] = 5
R10 [t2] = 1
R11 [t3] = 1
R12 [t4] = 0
which was very baffling so I went through single-step and the first time
t0 should've had 5 added to its total, it became a. It stayed that way
until after my yIsOne jump, which is where it became f. Everything else
appears to work fine. It jumps back up for the loop correctly. t2 gets
subtracted by one every time. Any help is appreciated. Thanks.
No comments:
Post a Comment