Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- PROMPTA: .asciiz "Enter how many integers you will be entering: "
- PROMPTB: .asciiz "Enter an integer: "
- NEWLINE: .asciiz "\n"
- LARGEST: .asciiz "The largest integer is: "
- SMALLEST: .asciiz "The smallest integer is: "
- SUM: .asciiz "The sum of the integers is: "
- AVERAGE: .asciiz "The average of the integers is: "
- .text
- main:
- la $a0 PROMPTA #Get the number of integers to be entered
- li $v0 4
- syscall
- li $v0 5
- syscall
- move $a0 $v0 #Put the number into a0 for the first function call
- add $s0 $s0 $a0 #Put it into a register that saves across function calls as well for the second call
- LS:
- jal largestSmallest #Get the largest and smallest ($v0 and $v1, respectively) integers from what the user enters
- move $t1 $v0
- move $t2 $v1
- la $a0 LARGEST #Print out the largest integer
- li $v0 4
- syscall
- move $a0 $t1
- li $v0 1
- syscall
- la $a0 NEWLINE
- li $v0 4
- syscall
- la $a0 SMALLEST #Print out the smallest integer
- li $v0 4
- syscall
- move $a0 $t2
- li $v0 1
- syscall
- la $a0 NEWLINE
- li $v0 4
- syscall
- move $a0 $s0 #Put the number of integers to enter back into $a0 for the second function call
- SA:
- jal sumAverage #Get the sum and average ($v0 and $v1, respectively) of what the user enters, without the decimal
- move $t1 $v0
- move $t2 $v1
- la $a0 SUM #Print out the sum of the integers
- li $v0 4
- syscall
- move $a0 $t1
- li $v0 1
- syscall
- la $a0 NEWLINE
- li $v0 4
- syscall
- la $a0 AVERAGE #Print out the average of the integers
- li $v0 4
- syscall
- move $a0 $t2
- li $v0 1
- syscall
- EXIT:
- li $v0 10 #Exit the program
- syscall
- largestSmallest:
- addi $t8 $a0 0 #$t8 is our counter, as a0 is used for printing we need to move it
- addi $t1 $t1 -32768 #$t1 is for our largest integer, starting at the minimum it can be
- addi $t2 $t2 32767 #$t2 is for our smallest integer, starting at the maximum it can be
- LOOPLS:
- la $a0 PROMPTB #Print out the get integer prompt
- li $v0 4
- syscall
- li $v0 5
- syscall
- move $t0 $v0 #Store the input into $t0 to check against max/min
- addi $t8 $t8 -1 #Decrease our counter
- blt $t0 $t2 LESS #If $t0 (input) < $t2 (smallest), set $t2 <- $t0
- bge $t0 $t1 GREATER #Elif $t0 (input) >= $t1 (largest), set $t1 <- $t0
- GREATER:
- move $t1 $t0 #Set $t1 <- $t0 (largest)
- blez $t8 RET #If all integers have been input, break and return the largest and smallest
- b LOOPLS
- LESS:
- move $t2 $t0 #Set $t2 <- $t0 (smallest)
- blez $t8 RET #If all integers have been input, break and return the largest and smallest
- b LOOPLS
- RET:
- move $v0 $t1 #$v0 <- $t1; $v0 is the largest integer
- move $v1 $t2 #$v1 <- $t2; $v1 is the smallest integer
- jr $ra #Return
- sumAverage:
- addi $t9 $a0 0 #$t9 is our counter, as $a0 is used for printing we need to move it
- LOOPSA:
- blez $t9 RETURN #Check before anything to see if we need to return the sum and average
- la $a0 PROMPTB #Print out the get integer prompt
- li $v0 4
- syscall
- li $v0 5
- syscall
- add $t3 $t3 $v0 #$t3 is our summation register
- addi $t9 $t9 -1 #Decrement our counter
- b LOOPSA
- RETURN:
- move $v0 $t3 #$v0 <- $t3; $v0 is the sum
- div $t3 $s0 #$t3/$s0; $s0 still contains the number of integers per line
- mflo $v1 #The quotient of the above division is in LOW, so $v1 <- LOW; $v1 is the average
- jr $ra #Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement