0

I made a program for multiplication. But the problem is the condition for ending the loop is not working properly. What is the possible reason for this weird behavior.

The problem is under Loop label..

.text

main:

li $t0,1
li $t1,2
li $t2,3
li $t3,4

li $v0,5
syscall

move $s0,$v0
beq $s0,$t2,MULT


MULT:
li $v0,5
syscall


move $s5,$v0

li $v0,5
syscall

move $s6,$v0
move $t5,$s6

Loop:
add $a0,$s5,$s5

addi $t5,$t5,1
li $v0,1
syscall
bne $t5,$s6, Loop

j EXIT


EXIT:

li $v0,10
syscall

Thanks

Alfred James
  • 1,029
  • 6
  • 17
  • 27
  • Please format your code, this is difficult to read... thanks in advance. – Thomas Sep 16 '12 at 06:31
  • Also, it'd be helpful to strip down the code to the least portion from which it becomes clear what the problem is. Any surrounding code is only distracting when not involved, especially when it contains more bugs/flaws, some of which you could have removed by following the hint in my answer to your "[If else in MIPS](http://stackoverflow.com/questions/12439356/if-else-in-mips)" question. – IdiotFromOutOfNowhere Sep 16 '12 at 08:22
  • Now can you tell me what is the problem in the loop lable – Alfred James Sep 16 '12 at 14:29

1 Answers1

1

This:

addi $t5,$t5,1
...
bne $t5,$s6, Loop

should be:

addi $t5,$t5,-1
...
bnez $t5, Loop

Now you're just counting $t5 from the value you have read to infinity, and will never stop.

Or, you can also replace move $t5,$s6 by li $5, 0

Furthermore, don't forget to initialize $a0 to 0.

Patrik
  • 2,695
  • 1
  • 21
  • 36