Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. .data
  2. PROMPTA: .asciiz "Enter how many integers you will be entering: "
  3. PROMPTB: .asciiz "Enter an integer: "
  4. NEWLINE: .asciiz "\n"
  5. LARGEST: .asciiz "The largest integer is: "
  6. SMALLEST: .asciiz "The smallest integer is: "
  7. SUM: .asciiz "The sum of the integers is: "
  8. AVERAGE: .asciiz "The average of the integers is: "
  9. .text
  10.  
  11. main:
  12. la $a0 PROMPTA #Get the number of integers to be entered
  13. li $v0 4
  14. syscall
  15. li $v0 5
  16. syscall
  17. move $a0 $v0 #Put the number into a0 for the first function call
  18. add $s0 $s0 $a0 #Put it into a register that saves across function calls as well for the second call
  19.  
  20. LS:
  21. jal largestSmallest #Get the largest and smallest ($v0 and $v1, respectively) integers from what the user enters
  22. move $t1 $v0
  23. move $t2 $v1
  24. la $a0 LARGEST #Print out the largest integer
  25. li $v0 4
  26. syscall
  27. move $a0 $t1
  28. li $v0 1
  29. syscall
  30. la $a0 NEWLINE
  31. li $v0 4
  32. syscall
  33. la $a0 SMALLEST #Print out the smallest integer
  34. li $v0 4
  35. syscall
  36. move $a0 $t2
  37. li $v0 1
  38. syscall
  39. la $a0 NEWLINE
  40. li $v0 4
  41. syscall
  42. move $a0 $s0 #Put the number of integers to enter back into $a0 for the second function call
  43.  
  44. SA:
  45. jal sumAverage #Get the sum and average ($v0 and $v1, respectively) of what the user enters, without the decimal
  46. move $t1 $v0
  47. move $t2 $v1
  48. la $a0 SUM #Print out the sum of the integers
  49. li $v0 4
  50. syscall
  51. move $a0 $t1
  52. li $v0 1
  53. syscall
  54. la $a0 NEWLINE
  55. li $v0 4
  56. syscall
  57. la $a0 AVERAGE #Print out the average of the integers
  58. li $v0 4
  59. syscall
  60. move $a0 $t2
  61. li $v0 1
  62. syscall
  63.  
  64. EXIT:
  65. li $v0 10 #Exit the program
  66. syscall
  67.  
  68. largestSmallest:
  69. addi $t8 $a0 0 #$t8 is our counter, as a0 is used for printing we need to move it
  70. addi $t1 $t1 -32768 #$t1 is for our largest integer, starting at the minimum it can be
  71. addi $t2 $t2 32767 #$t2 is for our smallest integer, starting at the maximum it can be
  72.  
  73. LOOPLS:
  74. la $a0 PROMPTB #Print out the get integer prompt
  75. li $v0 4
  76. syscall
  77. li $v0 5
  78. syscall
  79. move $t0 $v0 #Store the input into $t0 to check against max/min
  80. addi $t8 $t8 -1 #Decrease our counter
  81. blt $t0 $t2 LESS #If $t0 (input) < $t2 (smallest), set $t2 <- $t0
  82. bge $t0 $t1 GREATER #Elif $t0 (input) >= $t1 (largest), set $t1 <- $t0
  83.  
  84. GREATER:
  85. move $t1 $t0 #Set $t1 <- $t0 (largest)
  86. blez $t8 RET #If all integers have been input, break and return the largest and smallest
  87. b LOOPLS
  88.  
  89. LESS:
  90. move $t2 $t0 #Set $t2 <- $t0 (smallest)
  91. blez $t8 RET #If all integers have been input, break and return the largest and smallest
  92. b LOOPLS
  93.  
  94. RET:
  95. move $v0 $t1 #$v0 <- $t1; $v0 is the largest integer
  96. move $v1 $t2 #$v1 <- $t2; $v1 is the smallest integer
  97. jr $ra #Return
  98.  
  99. sumAverage:
  100. addi $t9 $a0 0 #$t9 is our counter, as $a0 is used for printing we need to move it
  101.  
  102. LOOPSA:
  103. blez $t9 RETURN #Check before anything to see if we need to return the sum and average
  104. la $a0 PROMPTB #Print out the get integer prompt
  105. li $v0 4
  106. syscall
  107. li $v0 5
  108. syscall
  109. add $t3 $t3 $v0 #$t3 is our summation register
  110. addi $t9 $t9 -1 #Decrement our counter
  111. b LOOPSA
  112.  
  113. RETURN:
  114. move $v0 $t3 #$v0 <- $t3; $v0 is the sum
  115. div $t3 $s0 #$t3/$s0; $s0 still contains the number of integers per line
  116. mflo $v1 #The quotient of the above division is in LOW, so $v1 <- LOW; $v1 is the average
  117. jr $ra #Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement