Please Help (MIPS)

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 2
Reputation: Spardante is an unknown quantity at this point 
Solved Threads: 0
Spardante Spardante is offline Offline
Newbie Poster

Please Help (MIPS)

 
0
  #1
Apr 18th, 2007
Hi. I'm having trouble with a program I am writing in MIPS. This is an assignment, so I'll understand if no one helps. The program has two recursive functions: Power and Multiply. The basic algorithms for each are:

  1. int Power(int base, int expon)
  2. {
  3. if(expon == 0)
  4. return 1;
  5. else return Multiply(base, Power(base, expon-1))
  6. }
  7.  
  8. int Multiply(int multiplicand, int multiplier)
  9. {
  10. if(multiplier == 0 || multiplicand == 0)
  11. return 0;
  12. else if(multiplier > multiplicand)
  13. return Multiply(multiplier, multiplicand)
  14. else
  15. return multiplicand + Multiply(multiplicand, multiplier-1)
  16. }

It seems very simple, but I just can't get it to work. Also, our algorithms may assume the base > 0, the exponent is not negative, and the result will fit in a word. My program is quite long, so here is what I have for just the Power and Multiply functions:

  1. # Power
  2. # Parameter: t8 = base
  3. # Parameter: t9 = exponent
  4. # t0 being used
  5. # t1 being used
  6. # Returned value in s7
  7.  
  8. .text
  9. .ent Power
  10. Power:
  11. addi $sp,$sp,-4
  12. sw $ra,0($sp)
  13.  
  14.  
  15. li $v0,4 # "Power("
  16. la $a0,power
  17. syscall
  18.  
  19. li $v0,1 # output t8
  20. move $a0,$t8
  21. syscall
  22.  
  23. li $v0,4 # ","
  24. la $a0,comma
  25. syscall
  26.  
  27. li $v0,1 # output t9
  28. move $a0,$t9
  29. syscall
  30.  
  31. li $v0,4 # ")\n"
  32. la $a0,cparen
  33. syscall
  34.  
  35.  
  36.  
  37.  
  38. beqz $t9,expIsZero # test if t9 = 0
  39. expNotZero:
  40. # PARAM: base. stayed same
  41. addi $t9,$t9,-1 # PARAM: t9 = expon-1
  42. jal Power # returned value in s7
  43.  
  44.  
  45. move $t6,$t8 # PARAM: t6 = base
  46. move $t7,$s7 # PARAM: t7 = Power(base,expon-1)
  47. jal Multiply
  48. j endPower
  49. expIsZero:
  50. li $s7,1 # if t9 = 0, return 1
  51. endPower:
  52.  
  53.  
  54. lw $ra,0($sp)
  55. addi $sp,$sp,4
  56. j $ra
  57. .end
  58.  
  59.  
  60.  
  61. # Multiply
  62. # Parameter: t6 = multiplicand
  63. # Parameter: t7 = multiplier
  64. # t0 being used
  65. # t1 being used
  66. # t2 temporary
  67. # Returned value in s6
  68.  
  69. .text
  70. .ent Multiply
  71. Multiply:
  72. addi $sp,$sp,-4 # allocate space on stack for $ra
  73. sw $ra,0($sp) # save the return address on the stack
  74.  
  75. li $v0,4 # " "
  76. la $a0,space
  77. syscall
  78.  
  79. la $a0,multiply # "Multiply("
  80. syscall
  81.  
  82. li $v0,1 # output t6
  83. move $a0,$t6
  84. syscall
  85.  
  86. li $v0,4 # ","
  87. la $a0,comma
  88. syscall
  89.  
  90. li $v0,1 # output t7
  91. move $a0,$t7
  92. syscall
  93.  
  94. li $v0,4 # ")\n"
  95. la $a0,cparen
  96. syscall
  97.  
  98.  
  99.  
  100. beqz $t7,IsZero # test if multiplier = 0
  101. beqz $t6,IsZero # test if multiplicand = 0
  102. elseIf:
  103. bgt $t7,$t6,greater # test if multiplier > multiplicand
  104. else:
  105. # PARAM: multiplicand stays same
  106. addi $t7,$t7,-1 # PARAM: multiplier = multiplier - 1
  107. jal Multiply # returned value in s6
  108.  
  109. add $s6,$t6,$s6 # return multiplicand +
  110. # Multiply(multiplicand,multiplier-1)
  111.  
  112. j endMultiply
  113. greater:
  114. move $t2,$t7 # swap the values and
  115. move $t7,$t6 # call again
  116. move $t6,$t2
  117. jal Multiply
  118. j endMultiply
  119. IsZero:
  120. li $s6,0 # return 0
  121. endMultiply:
  122.  
  123.  
  124.  
  125.  
  126. lw $ra,0($sp)
  127. addi $sp,$sp,4
  128. j $ra
  129. .end

The output I am getting for Power(3,5) is:

Power(3,5)
Power(3,4)
Power(3,3)
Power(3,2)
Power(3,1)
Power(3,0)
Multiply(3,1)
Multiply(3,0)
Multiply(3,1)
Multiply(3,0)
Multiply(3,1)
Multiply(3,0)
Multiply(3,1)
Multiply(3,0)
Multiply(3,1)
Multiply(3,0)
1

...where 1 the final answer output in Main after Power finishes.
Everything is being called correctly, it just seems to be a logic problem. The program is due friday, so if anyone can lend some help, it would be greatly appreciated. I can post my entire code if needed, it just comes out lookin weird.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 39
Reputation: MacGyver Orca is an unknown quantity at this point 
Solved Threads: 4
MacGyver Orca's Avatar
MacGyver Orca MacGyver Orca is offline Offline
Light Poster

Re: Please Help (MIPS)

 
0
  #2
Apr 18th, 2007
Please post all the code, split into two sections, one for each function.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: Spardante is an unknown quantity at this point 
Solved Threads: 0
Spardante Spardante is offline Offline
Newbie Poster

Re: Please Help (MIPS)

 
0
  #3
Apr 19th, 2007
  1. .data
  2. array: .space 40
  3. space: .asciiz " "
  4. newline: .asciiz "\n"
  5. prompt: .asciiz "Enter 5 sets for Power(X,Y):\n"
  6. set: .asciiz "Set "
  7. X: .asciiz "X = "
  8. Y: .asciiz "Y = "
  9. power: .asciiz "Power("
  10. multiply: .asciiz "Multiply("
  11. comma: .asciiz ","
  12. cparen: .asciiz ")\n"
  13. separator: .asciiz "==========================================\n\n"
  14. terminated:.asciiz "Program Terminated"
  15. .text
  16. .globl main
  17. #------------------------------------#
  18. # Main #
  19. #------------------------------------#
  20. # t0 counter #
  21. # t1 ends counter #
  22. # a2 address of array #
  23. #------------------------------------#
  24. main:
  25. li $v0,4 # "Enter 5 sets for Power(X,Y):\n"
  26. la $a0,prompt
  27. syscall
  28.  
  29. li $t0,1 # counter for sets. will input 5 times
  30. li $t1,5 # terminates counter
  31. la $a2,array # a2 = address of array
  32.  
  33. Sets:
  34. li $v0,4 # "\n"
  35. la $a0,newline
  36. syscall
  37. la $a0,space # " "
  38. syscall
  39. la $a0,set # "Set "
  40. syscall
  41.  
  42. li $v0,1 # outputs set number
  43. move $a0,$t0
  44. syscall
  45.  
  46. li $v0,4 # "\n"
  47. la $a0,newline
  48. syscall
  49.  
  50. li $v0,4
  51. la $a0,X # "X = "
  52. syscall
  53.  
  54. li $v0,5 # Input array($a2) = X
  55. syscall
  56. sw $v0,($a2)
  57. addi $a2,$a2,4 # next place in array
  58.  
  59. li $v0,4 # "Y = "
  60. la $a0,Y
  61. syscall
  62.  
  63. li $v0,5 # Input array($a2) = Y
  64. syscall
  65. sw $v0,($a2)
  66. addi $a2,$a2,4 # next place in array
  67.  
  68.  
  69. addi $t0,$t0,1 # increment counter
  70. ble $t0,$t1,Sets
  71.  
  72. # array should now look like: base, expon, base, expon...
  73.  
  74. li $v0,4 # "\n"
  75. la $a0,newline
  76. syscall
  77. la $a2,array # a2 = address of array
  78. li $t0,1 # reset counter. will repeat 5 times
  79.  
  80. Repeat:
  81. li $v0,4 # "==========================================\n\n"
  82. la $a0,separator
  83. syscall
  84. la $a0,set # "Set "
  85. syscall
  86.  
  87. li $v0,1 # outputs set number
  88. move $a0,$t0
  89. syscall
  90.  
  91. li $v0,4 # "\n" twice
  92. la $a0,newline
  93. syscall
  94. syscall
  95.  
  96. lw $t8,($a2) # PARAM: t8 = base
  97. addi $a2,$a2,4 # next item in array will be exponent
  98. lw $t9,($a2) # PARAM: t9 = exponent
  99.  
  100. jal Power # calls Power(t8,t9)
  101. # will return the value in s7
  102.  
  103. li $v0,1 # print the final value
  104. move $a0,$s7
  105. syscall
  106.  
  107. li $v0,4 # "\n"
  108. la $a0,newline
  109. syscall
  110.  
  111. addi $a2,$a2,4 # next item in array is next base
  112. addi $t0,$t0,1 # increment counter
  113. ble $t0,$t1,Repeat
  114.  
  115. li $v0,10
  116. syscall
  117. .end
  118.  
  119.  
  120. #------------------------------------#
  121. # Power #
  122. #------------------------------------#
  123. # Param: t8 = base #
  124. # Param: t9 = exponent #
  125. # t0 being used #
  126. # t1 being used #
  127. #------------------------------------#
  128. # Returned value in s7 #
  129. #------------------------------------#
  130.  
  131. .text
  132. .ent Power
  133. Power:
  134. addi $sp,$sp,-4 # allocate space on stack for $ra
  135. sw $ra,0($sp) # save the return address on the stack
  136.  
  137. li $v0,4 # "Power("
  138. la $a0,power
  139. syscall
  140. li $v0,1 # output t8
  141. move $a0,$t8
  142. syscall
  143. li $v0,4 # ","
  144. la $a0,comma
  145. syscall
  146. li $v0,1 # output t9
  147. move $a0,$t9
  148. syscall
  149. li $v0,4 # ")\n"
  150. la $a0,cparen
  151. syscall
  152.  
  153.  
  154.  
  155. beqz $t9,expIsZero # test if t9 = 0
  156. expNotZero:
  157. # PARAM: base. stayed same
  158. addi $t9,$t9,-1 # PARAM: t9 = expon-1
  159. jal Power # returned value in s7
  160.  
  161. move $t6,$t8 # PARAM: t6 = base
  162. move $t7,$s7 # PARAM: t7 = Power(base,expon-1)
  163. jal Multiply
  164. j endPower
  165. expIsZero:
  166. li $s7,1 # if t9 = 0, return 1
  167. endPower:
  168.  
  169.  
  170. lw $ra,0($sp) # restore return address
  171. addi $sp,$sp,4 # deallocate the space on the stack
  172. j $ra # return to main
  173. .end
  174.  
  175.  
  176. #------------------------------------#
  177. # Multiply #
  178. #------------------------------------#
  179. # Param: t6 = multiplicand #
  180. # Param: t7 = multiplier #
  181. # t0 being used #
  182. # t1 being used #
  183. # t2 temporary #
  184. #------------------------------------#
  185. # Returned value in s6 #
  186. #------------------------------------#
  187.  
  188. .text
  189. .ent Multiply
  190. Multiply:
  191. addi $sp,$sp,-4 # allocate space on stack for $ra
  192. sw $ra,0($sp) # save the return address on the stack
  193.  
  194. li $v0,4 # " "
  195. la $a0,space
  196. syscall
  197. la $a0,multiply # "Multiply("
  198. syscall
  199. li $v0,1 # output t6
  200. move $a0,$t6
  201. syscall
  202. li $v0,4 # ","
  203. la $a0,comma
  204. syscall
  205. li $v0,1 # output t7
  206. move $a0,$t7
  207. syscall
  208. li $v0,4 # ")\n"
  209. la $a0,cparen
  210. syscall
  211.  
  212.  
  213.  
  214. beqz $t7,IsZero # test if multiplier = 0
  215. beqz $t6,IsZero # test if multiplicand = 0
  216. elseIf:
  217. bgt $t7,$t6,greater# test if multiplier > multiplicand
  218. else:
  219. # PARAM: multiplicand stays same
  220. addi $t7,$t7,-1 # PARAM: multiplier = multiplier - 1
  221. jal Multiply # returned value in s6
  222.  
  223. add $s6,$t6,$s6 # return multiplicand +
  224. # Multiply(multiplicand,multiplier-1)
  225. j endMultiply
  226. greater:
  227. move $t2,$t7 # swap the values and
  228. move $t7,$t6 # call again
  229. move $t6,$t2
  230. jal Multiply
  231. j endMultiply
  232. IsZero:
  233. li $s6,0 # return 0
  234. endMultiply:
  235.  
  236.  
  237.  
  238.  
  239. lw $ra,0($sp) # restore return address
  240. addi $sp,$sp,4 # deallocate the space on the stack
  241. j $ra # return to main
  242. .end

Okay, here's my entire program. Main, Power, and Multiply are all separated.
Last edited by Spardante; Apr 19th, 2007 at 12:23 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 39
Reputation: MacGyver Orca is an unknown quantity at this point 
Solved Threads: 4
MacGyver Orca's Avatar
MacGyver Orca MacGyver Orca is offline Offline
Light Poster

Re: Please Help (MIPS)

 
0
  #4
Apr 19th, 2007
The main problem I am seeing is that before your [code = MIPS] jal [/code] instructions, you're not passing the parameters, and after the instruction, you're not getting the value out of [code = MIPS] $v0 [/code], in fact I don't see you returning to there in your functions, so that might just be your convention.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC