Advertisement
Guest User

Untitled

a guest
Dec 4th, 2013
1,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .globl nCk
  2. ########################################################################
  3. #****************************Name: TJ Zimmerman************************
  4. #****************************Homework: nCr*****************************
  5. #
  6. #
  7. #Main function is used to run the program. It accepts input from the
  8. #command line and processes the answe accordingly.
  9. #
  10. #******In Java******
  11. #nCr = factorial(n)/(factorial(n-r) * factorial (r))
  12. ########################################################################
  13. #t3 = n
  14. #t4 = k
  15. #a0/v0 = syscall params
  16.  
  17. .data
  18. enterN: .asciiz "Please enter the n value: \n"
  19. enterK: .asciiz "Please enter the k value: \n"
  20. valueError: .asciiz "Much error. Such bad param. Very crash. Wow. Terminating meow."
  21. answerChoose: .asciiz " choose "
  22. answerIs: " is "
  23. .text
  24.  
  25.  
  26. #Collect params and check for validity.
  27. la $a0, enterN #Ask for the first param, n.
  28. li $v0, 4 #String syscall
  29. syscall #Prints out string.
  30.  
  31. li $v0, 5
  32. syscall #Places inputted value in v0.
  33. la $s0, ($v0) #Copy n into saved register for final output string.
  34. move $v0, $t3 #Puts n into $s0.
  35. blt $t3, $0, errorMessage #n<0 ERROR
  36.  
  37. la $a0, enterK #Asks for the second param, k.
  38. li $v0, 4 #String syscall
  39. syscall #Prints out string
  40.  
  41. li $v0, 5
  42. syscall #Places inputted value into v0.
  43. la $s1, ($v0) #Copy k into saved register for final output string.
  44. move $v0, $t4 #Puts k into $t1
  45. blt $t4, $0, errorMessage #k<0 ERROR
  46. bgt $t4, $t3, errorMessage #k>n ERROR
  47.  
  48. #Start program now.
  49. jal nCk #Jump to nCk function.
  50. jal printAnswer
  51. jr $ra #End program. SHOULD JR RA BE USED HERE?
  52.  
  53.  
  54. ########################################################################
  55. #nCk function is used to calculate the number of possible combinations
  56. #of the numbers n and k using a simple recursive function like below:
  57. #
  58. #******In Java******
  59. #int factorial(int n) {
  60. #    if((n==0) || (n==1)) {
  61. #        return 1;
  62. #    }
  63. #    int f;
  64. #    f = n * factorial(n-1); // uses recursion
  65. #    return f;
  66. #}
  67. ########################################################################
  68. #t3 = n
  69. #t4 = k
  70. #v0/a0/t2 = working params.
  71.  
  72. nCk:
  73. sub $sp, $sp, 16 #allocate the needed space in stack.
  74. sw $ra, 0($sp) #save return address in first position
  75. sw $t3, 4($sp) #save n in the stack
  76. sw $t4, 8($sp) #save k in the stack
  77.  
  78. sub $t3, $t3, 1 #Subtract one from n
  79. sub $t4, $t4, 1 #Subtract one from k
  80.  
  81. jal checkBounds #Check for end of recursion.
  82. sw $v0, 12($sp) #copy returned 1 or 0 into stack.
  83.  
  84. lw $t3, 4($sp) #Load original n back into t3.
  85. lw $t4, 8($sp) #Load original k back into t4.
  86.  
  87. sub $t3, $t3, 1 #Subtract one from n again. (n-1 step of recursive algorithm)
  88. jal checkBounds #Check for end of recursion with n 1 number lower.
  89.  
  90. lw $t2, 12($sp) #Load the value held in the previously returned v0.
  91. add $v0, $v0, $t2 #Add old returned value to new returned value.
  92.  
  93. lw $ra, 0($sp) #Load the original return address.
  94. addi $sp, $sp, 16 #Add 16 more bytes to the stack.
  95. jr $ra
  96.  
  97.  
  98. checkBounds: #Check if program should still recurse
  99. beq $t3, $t4, return1 #If n==k
  100. beq $t4, $0, return1  #if k==0
  101. li $v0, 0 #If (j!=k || k!=0){ return 0};
  102. jal nCk
  103. jr $ra
  104.  
  105.  
  106. return1: #Returns 1
  107. li $v0, 1
  108. jr $ra
  109.  
  110.  
  111. errorMessage: #Inputted params were illegal.
  112. la $t0, valueError #Loads error message into t0
  113. li $v0, 4 #Print string syscall.
  114. syscall #Output error.
  115. jr $ra
  116.  
  117.  
  118. printAnswer: #Prints out the final answer.
  119. la $a0, ($s0) #Copy n into a0 for syscall syntax.
  120. li $v0, 1 #Print Int syscall.
  121. syscall #Print out n
  122.  
  123. la $a0, answerChoose #print out "choose"
  124. li $v0, 4
  125. syscall
  126.  
  127. la $a0, ($s1) #Copy k into a0 for syscall syntax.
  128. li $v0, 1 #Print int syscall.
  129. move $t4, $a0
  130. syscall  #Print out k
  131.  
  132. la $a0, answerIs #Print out "is"
  133. li $v0, 4
  134. syscall
  135.  
  136. la $a0, ($t3)
  137. li $v0, 1
  138. syscall
  139. jr $ra
  140.  
  141.  
  142.  
  143.  
  144. ###################OLD FACTORIAL ALGORITHM################
  145.  
  146. #nCk:
  147. #
  148. #factorial: #Otherwise, start the recursive loop.
  149. #beq $a0, 0, equalsZero #Checks if either number is 0 or 1. If so, then break recursion
  150. #beq $a1, 0, equalsZero #simply return 1.
  151. #
  152. #subi $t0, $t0, 1 #calculate n-1
  153. #mul $a0, $a0, $t0 #n*(n-1) step
  154. #j factorial #Recursively return. SHOULD THIS BE JAL?
  155. #
  156. #equalsZero:
  157. #li $v0, 1
  158. #j endnCk
  159. #jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement