Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##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.
- ##Output how many numbers were entered, remember the 0 is not "a number"
- ##Output the average of the numbers, to 3 decimal places. No floating point instructions allowed
- ##be sure the code is well commented, I will not grade any uncommented program.
- ##You have 2 submissions, for this project, I will look at the last file submitted.
- ##name your file lastnameProject1.Asm or it will not be graded. (example, my file would be lichtenthalProject1.Asm)
- ##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
- #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
- #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
- #s1-s3 stores our remainder stuff
- .globl main
- .text
- main:
- #this is going to be our 0 sentinel/exit condition
- li $s0, 0
- li $t1, 0
- li $t6, 0
- li $s7, 10
- loop:
- li $v0, 4 #string output
- la $a0, number_entry
- syscall
- li $v0, 5 #prep for user input
- syscall
- move $t0, $v0 #store in $t0
- add $t6, $t6, $t0 #take the value in $t0, add it to the value already in $t6, and store in $t6
- beqz $t0, leave #exit if the user input 0
- addi $t1, $t1, 1 #increment the counter entry by 1
- jal loop #go back to the loop
- leave:
- li $v0, 4 #string output
- la $a0, sum_of_all_numbers
- syscall
- move $a0, $t6 #output the total sum of all numbers
- li $v0, 1
- syscall
- li $v0, 4 #string output
- la $a0, iterations
- syscall
- move $a0, $t1 #output the total sum of all numbers
- li $v0, 1
- syscall
- li $v0, 4 #string output
- la $a0, spacing
- syscall
- 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
- #arithmetic block
- div $t2, $t6, $t1 #integer division result and storage in t2
- #you never really think HOW decimals work until you have to, huh?
- #(remainder x 10)/divisor
- rem $t3, $t6, $t1 #we get the remainder of the equation
- mul $s1, $t3, $s7 #multiply the remainder by 10
- div $t3, $s1, $t1 #divide the result by the original divisor
- rem $t4, $s1, $t1 #same
- mul $s2, $t4, $s7
- div $t4, $s2, $t1
- rem $t5, $s2, $t1 #same
- mul $s3, $t5, $s7
- div $t5, $s3, $t1
- li $v0, 4 #string output
- la $a0, average
- syscall
- move $a0, $t2 #output the integer-part
- li $v0, 1
- syscall
- li $v0, 4 #output the string that's just a period
- la $a0, period
- syscall
- move $a0, $t3 #output the .X decimal-part
- li $v0, 1
- syscall
- move $a0, $t4 #output the .0X decimal-part
- li $v0, 1
- syscall
- move $a0, $t5 #output the .00X decimal-part
- li $v0, 1
- syscall
- li $v0, 10 #terminate the program
- syscall #go do it
- error:
- li $v0, 4 #output an error message
- la $a0, no_zero
- li $v0, 10 #terminate the program
- syscall #go do it
- .data
- number_entry:
- .asciiz "Please input your numbers, 0 terminates: \n"
- iterations:
- .asciiz "\nTotal number of numbers entered: "
- sum_of_all_numbers:
- .asciiz "\nThe sum of all the numbers is: "
- period:
- .asciiz "."
- no_zero:
- .asciiz "Cannot divide by 0, restart program.\n"
- spacing:
- .asciiz "\n\n"
- average:
- .ascii "\nThe average is: "
Advertisement
Add Comment
Please, Sign In to add comment