mips recursive

Reply

Join Date: Sep 2009
Posts: 31
Reputation: shahab03 is an unknown quantity at this point 
Solved Threads: 0
shahab03 shahab03 is offline Offline
Light Poster

mips recursive

 
0
  #1
Oct 6th, 2009
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?

  1. .data
  2. msg0: .asciiz "Enter number of disks:"
  3. msg1: .asciiz "Move disk 1 from TOWER "
  4. msg2: .asciiz "Move disk "
  5. msg3: .asciiz " from TOWER "
  6. msg4: .asciiz " to TOWER "
  7. msg5: .asciiz "\n"
  8.  
  9. .text
  10. .globl main
  11. main:
  12.  
  13.  
  14. la $a0,msg0 #Print "Enter number of disks:"
  15. li $v0,4
  16. syscall
  17.  
  18. li $v0,5 #cin, $v0=input value
  19. syscall
  20.  
  21. addi $t0,$zero,6 #$t0=6
  22. move $a2,$v0 #$a2=number of disc,initialized by input value
  23. addi $t7,$zero,1 #$t7=from,initialize to 1
  24. addi $a1,$zero,3 #$a1=to,initialize to 3
  25. move $t1,$t0 #$t1=temp,initialize to $t1=6
  26.  
  27. addi $sp,$sp,-20 #create a stack with 5 boxes
  28. sw $ra,16($sp) #$ra |retun address|
  29. sw $t7,12($sp) #$t7 |from |
  30. sw $a1,8($sp) #$a1 |to |
  31. sw $a2,4($sp) #$a2 |numberof disc|
  32. sw $t1,0($sp) #$t1 |temp |<---$sp
  33.  
  34. jal hanoi # call hanoi
  35.  
  36. #lw $ra,16($sp) # -----------------(*)
  37.  
  38. lw $ra,16($sp) #$ra |retun address|
  39. lw $t7,12($sp) #$t7 |from |
  40. lw $a1,8($sp) #$a1 |to |
  41. lw $a2,4($sp) #$a2 |numberof disc|
  42. lw $t1,0($sp) #$t1 |temp |<---$sp
  43.  
  44. addi $sp,$sp,20
  45. #jr $ra
  46.  
  47.  
  48. hanoi:
  49.  
  50.  
  51. sub $t1,$t0,$t7
  52. sub $t1,$t1,$a1 #temp = 6-from-to
  53.  
  54.  
  55.  
  56. addi $sp,$sp,-20 #create a stack with 5 boxes
  57.  
  58. sw $ra,16($sp) #$ra |retun address|
  59. sw $t7,12($sp) #$t7 |from |
  60. sw $a1,8($sp) #$a1 |to |
  61. sw $a2,4($sp) #$a2 |numberof disc|
  62. sw $t1,0($sp) #$t1 |temp |<---$sp
  63.  
  64. bne $a2,1,recurse # if (n =/= 1) recurse
  65.  
  66. li $v0,4
  67. la $a0,msg1
  68. syscall
  69. li $v0,1
  70. move $a0,$t7
  71. syscall
  72. li $v0,4
  73. la $a0,msg4
  74. syscall
  75. li $v0,1
  76. move $a0,$a1
  77. syscall
  78. li $v0,4
  79. la $a0,msg5
  80. syscall
  81.  
  82. endcase: # exit code
  83.  
  84. addi $sp,$sp,20 #delete a stack with 5 boxes
  85.  
  86. lw $ra,16($sp) #$ra |retun address|
  87. lw $t7,12($sp) #$t7 |from |
  88. lw $a1,8($sp) #$a1 |to |
  89. lw $a2,4($sp) #$a2 |numberof disc|
  90. lw $t1,0($sp) #$t1 |temp |<---$sp
  91.  
  92. jr $ra # return
  93.  
  94. recurse:
  95.  
  96. addi $a2,$a2,-1
  97.  
  98. move $a1,$t1
  99.  
  100. jal hanoi # call hanoi
  101.  
  102. li $v0,4 #--------------------(**)
  103. la $a0,msg2
  104. syscall
  105. li $v0,1
  106. move $a0,$a2
  107. syscall
  108. li $v0,4
  109. la $a0,msg3
  110. syscall
  111. li $v0,1
  112. move $a0,$t7
  113. syscall
  114. li $v0,4
  115. la $a0,msg4
  116. syscall
  117. li $v0,1
  118. move $a0,$a1
  119. syscall
  120. li $v0,4
  121. la $a0,msg5
  122. syscall
  123.  
  124. lw $ra,16($sp) #$ra |retun address|
  125. lw $t7,12($sp) #$t7 |from |
  126. lw $a1,8($sp) #$a1 |to |
  127. lw $a2,4($sp) #$a2 |numberof disc|
  128. lw $t1,0($sp) #$t1 |temp |<---$sp
  129. addi $sp,$sp,20 #delete a stack with 5 boxes
  130.  
  131. addi $a2,$a2,-1
  132.  
  133. move $t7,$t1
  134.  
  135.  
  136. jal hanoi # call hanoi
  137.  
  138. j endcase # goto exit code (return)---------------------(***)
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


Views: 435 | Replies: 0
Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC