Silver_Smoulder

[ASM][Func] Project 1

Apr 13th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. ##write a program that finds the sum of numbers entered by the user. Use a sentinel driven loop, where the sentinel is 0, to enter the numbers.
  2. ##Output how many numbers were entered, remember the 0 is not "a number"
  3. ##Output the average of the numbers, to 3 decimal places. No floating point instructions allowed
  4. ##be sure the code is well commented, I will not grade any uncommented program.
  5. ##You have 2 submissions, for this project, I will look at the last file submitted.
  6. ##name your file lastnameProject1.Asm or it will not be graded. (example, my file would be lichtenthalProject1.Asm)
  7.  
  8. ##variables to use: t0 = store user number input, t1= counter of entries, t2 = non-decimal integer division storage, t3 = storage of remainder 1, t4 = storage of remainder 2, t5 = storage of remainder 3, t6 = total sum of the numbers input
  9. #we are going to prompt the user to input numbers until they enter 0. every time they do, we increment t1 by 1. at the termination, we divide t0 by t1 and get some integer, storing it in t2
  10. #then we do a modulo operation on that, storing it in t3. then we modulo t3, put the value into t4, and then modul0 t4, storing it in t5, giving us a value with 3 decimal places
  11. #s1-s3 stores our remainder stuff
  12.  
  13. .globl main
  14. .text
  15.  
  16. main:
  17.  
  18. #this is going to be our 0 sentinel/exit condition
  19. li $s0, 0
  20. li $t1, 0
  21. li $t6, 0
  22. li $s7, 10
  23.  
  24. loop:
  25.  
  26. li $v0, 4 #string output
  27. la $a0, number_entry
  28. syscall
  29.  
  30. li $v0, 5 #prep for user input
  31. syscall
  32. move $t0, $v0 #store in $t0
  33.  
  34. add $t6, $t6, $t0 #take the value in $t0, add it to the value already in $t6, and store in $t6
  35.  
  36. beqz $t0, leave #exit if the user input 0
  37.  
  38. addi $t1, $t1, 1 #increment the counter entry by 1
  39. jal loop #go back to the loop
  40.  
  41. leave:
  42. li $v0, 4 #string output
  43. la $a0, sum_of_all_numbers
  44. syscall
  45.  
  46. move $a0, $t6 #output the total sum of all numbers
  47. li $v0, 1
  48. syscall
  49.  
  50. li $v0, 4 #string output
  51. la $a0, iterations
  52. syscall
  53.  
  54. move $a0, $t1 #output the total sum of all numbers
  55. li $v0, 1
  56. syscall
  57.  
  58. li $v0, 4 #string output
  59. la $a0, spacing
  60. syscall
  61.  
  62.  
  63.  
  64. blez $t1, error #if we didn't input any numbers at all, the conter would be 0, and it could do a "divide by 0" error, so we'll warn people
  65.  
  66. #arithmetic block
  67. div $t2, $t6, $t1 #integer division result and storage in t2
  68.  
  69. #you never really think HOW decimals work until you have to, huh?
  70. #(remainder x 10)/divisor
  71.  
  72. rem $t3, $t6, $t1 #we get the remainder of the equation
  73. mul $s1, $t3, $s7 #multiply the remainder by 10
  74. div $t3, $s1, $t1 #divide the result by the original divisor
  75.  
  76. rem $t4, $s1, $t1 #same
  77. mul $s2, $t4, $s7
  78. div $t4, $s2, $t1
  79.  
  80. rem $t5, $s2, $t1 #same
  81. mul $s3, $t5, $s7
  82. div $t5, $s3, $t1
  83.  
  84. li $v0, 4 #string output
  85. la $a0, average
  86. syscall
  87.  
  88. move $a0, $t2 #output the integer-part
  89. li $v0, 1
  90. syscall
  91.  
  92. li $v0, 4 #output the string that's just a period
  93. la $a0, period
  94. syscall
  95.  
  96. move $a0, $t3 #output the .X decimal-part
  97. li $v0, 1
  98. syscall
  99.  
  100. move $a0, $t4 #output the .0X decimal-part
  101. li $v0, 1
  102. syscall
  103.  
  104. move $a0, $t5 #output the .00X decimal-part
  105. li $v0, 1
  106. syscall
  107.  
  108. li $v0, 10 #terminate the program
  109. syscall #go do it
  110.  
  111.  
  112. error:
  113.  
  114. li $v0, 4 #output an error message
  115. la $a0, no_zero
  116.  
  117. li $v0, 10 #terminate the program
  118. syscall #go do it
  119.  
  120. .data
  121.  
  122. number_entry:
  123. .asciiz "Please input your numbers, 0 terminates: \n"
  124.  
  125. iterations:
  126. .asciiz "\nTotal number of numbers entered: "
  127.  
  128. sum_of_all_numbers:
  129. .asciiz "\nThe sum of all the numbers is: "
  130.  
  131. period:
  132. .asciiz "."
  133.  
  134. no_zero:
  135. .asciiz "Cannot divide by 0, restart program.\n"
  136.  
  137. spacing:
  138. .asciiz "\n\n"
  139.  
  140. average:
  141. .ascii "\nThe average is: "
Advertisement
Add Comment
Please, Sign In to add comment