0

i have written the following codes to implement the tower of hanoi

but it doesn't work properly.....

i have spent almost a week to find out the problem,

but i still don't know what's going on...

can any one can help me ???

Really Thanks for your kindly help!!

Below is my code :

```
.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 __start
__start:
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)---------------------(***)
---------------------------------End of Mips Code ------------------------
--------------------------Corresponding C++ Codes--------------------
void hanoi(int from, int to, int num)
{
int temp = 6 - from - to; //find the temporary
//storage column
if (num == 1){
cout << "move disc 1 from " << from
<< " to " << to << endl;
}
else {
hanoi(from, temp, num - 1);
cout << "move disc " << num << " from " << from
<< " to " << to << endl;
hanoi(temp, to, num - 1);
}
}
int main() {
int num_disc; //number of discs
cout << "Enter number of disc";
cin >> num_disc;
hanoi(1, 3, num_disc);
return 0;
}
-------------------------------------------------------------------------------
```

Expected Ouptut :

```
Enter number of disks: 3
Move disk 1 from TOWER 1 to TOWER 3
Move disk 2 from TOWER 1 to TOWER 2
Move disk 1 from TOWER 3 to TOWER 2
Move disk 3 from TOWER 1 to TOWER 3
Move disk 1 from TOWER 2 to TOWER 1
Move disk 2 from TOWER 2 to TOWER 3
Move disk 1 from TOWER 1 to TOWER 3
```

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting