Question on my code in tower of hanoi

Reply

Join Date: Mar 2006
Posts: 1
Reputation: kennethwcn is an unknown quantity at this point 
Solved Threads: 0
kennethwcn kennethwcn is offline Offline
Newbie Poster

Question on my code in tower of hanoi

 
0
  #1
Mar 24th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Question on my code in tower of hanoi

 
0
  #2
Mar 29th, 2006
I have a question for you. Did you try to debug your code with SPIM simulator? If answer is no then try it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC