I am trying to do towers of hanoi recursively below is the code. my problem is that i am not getting out of the cycle at the right time so code keeps on running... can someone tell me what I may be doing wrong?
.data
msg0: .asciiz "Enter number of disks:"
msg1: .asciiz "Move disk 1 from TOWER "
msg2: .asciiz "Move disk "
msg3: .asciiz " from TOWER "
msg4: .asciiz " to TOWER "
msg5: .asciiz "\n"
.text
.globl main
main:
la $a0,msg0 #Print "Enter number of disks:"
li $v0,4
syscall
li $v0,5 #cin, $v0=input value
syscall
addi $t0,$zero,6 #$t0=6
move $a2,$v0 #$a2=number of disc,initialized by input value
addi $t7,$zero,1 #$t7=from,initialize to 1
addi $a1,$zero,3 #$a1=to,initialize to 3
move $t1,$t0 #$t1=temp,initialize to $t1=6
addi $sp,$sp,-20 #create a stack with 5 boxes
sw $ra,16($sp) #$ra |retun address|
sw $t7,12($sp) #$t7 |from |
sw $a1,8($sp) #$a1 |to |
sw $a2,4($sp) #$a2 |numberof disc|
sw $t1,0($sp) #$t1 |temp |<---$sp
jal hanoi # call hanoi
#lw $ra,16($sp) # -----------------(*)
lw $ra,16($sp) #$ra |retun address|
lw $t7,12($sp) #$t7 |from |
lw $a1,8($sp) #$a1 |to |
lw $a2,4($sp) #$a2 |numberof disc|
lw $t1,0($sp) #$t1 |temp |<---$sp
addi $sp,$sp,20
#jr $ra
hanoi:
sub $t1,$t0,$t7
sub $t1,$t1,$a1 #temp = 6-from-to
addi $sp,$sp,-20 #create a stack with 5 boxes
sw $ra,16($sp) #$ra |retun address|
sw $t7,12($sp) #$t7 |from |
sw $a1,8($sp) #$a1 |to |
sw $a2,4($sp) #$a2 |numberof disc|
sw $t1,0($sp) #$t1 |temp |<---$sp
bne $a2,1,recurse # if (n =/= 1) recurse
li $v0,4
la $a0,msg1
syscall
li $v0,1
move $a0,$t7
syscall
li $v0,4
la $a0,msg4
syscall
li $v0,1
move $a0,$a1
syscall
li $v0,4
la $a0,msg5
syscall
endcase: # exit code
addi $sp,$sp,20 #delete a stack with 5 boxes
lw $ra,16($sp) #$ra |retun address|
lw $t7,12($sp) #$t7 |from |
lw $a1,8($sp) #$a1 |to |
lw $a2,4($sp) #$a2 |numberof disc|
lw $t1,0($sp) #$t1 |temp |<---$sp
jr $ra # return
recurse:
addi $a2,$a2,-1
move $a1,$t1
jal hanoi # call hanoi
li $v0,4 #--------------------(**)
la $a0,msg2
syscall
li $v0,1
move $a0,$a2
syscall
li $v0,4
la $a0,msg3
syscall
li $v0,1
move $a0,$t7
syscall
li $v0,4
la $a0,msg4
syscall
li $v0,1
move $a0,$a1
syscall
li $v0,4
la $a0,msg5
syscall
lw $ra,16($sp) #$ra |retun address|
lw $t7,12($sp) #$t7 |from |
lw $a1,8($sp) #$a1 |to |
lw $a2,4($sp) #$a2 |numberof disc|
lw $t1,0($sp) #$t1 |temp |<---$sp
addi $sp,$sp,20 #delete a stack with 5 boxes
addi $a2,$a2,-1
move $t7,$t1
jal hanoi # call hanoi
j endcase # goto exit code (return)---------------------(***)