My code is supposed to calculate and print the sum, average, min, max, num of integers above avg, and numb of integers below avg in that order. It is having an issue with the registers passing them in.
I've tried utilizing the $v1 register as I believed my li $v0, 1 commands were causing wrong output. This did change my output from all 1's to 2 0's and the rest 1's. here is an example of the current output with the input values numbers 1-10
Enter integer: 1
Enter integer: 2
Enter integer: 3
Enter integer: 4
Enter integer: 5
Enter integer: 6
Enter integer: 7
Enter integer: 8
Enter integer: 9
Enter integer: 10
Sum: 55
Average: 55
Minimum: 1
Maximum: 10
Number of integers above: 0
Number of integers below: 0
here is the code
.data
array: .space 40 # 10 integers * 4 bytes = 40 byte array
newline: .asciiz "\n"
userPrompt: .asciiz "Enter integer: "
print_sum_statement: .asciiz "Sum: "
print_avg_statement: .asciiz "Average: "
print_max_statement: .asciiz "Maximum: "
print_min_statement: .asciiz "Minimum: "
print_above_statement: .asciiz "Number of integers above: "
print_below_statement: .asciiz "Number of integers below: "
.text
main:
# COLLECT USERINPUT
#-------------------------
la $a0, array # Setting the array to be passing in through $a_
jal intRead # Jumping to function to read in array
move $s0, $v0 # Passes number of elements to univerally stored register
# Since this value should never change.
#-------------------------
# PRINT SUM BY CALLING SUM_CALC
#-------------------------
sum_print:
li $v0, 4
la $a0, print_sum_statement # Print statement
syscall
la $a0, array # Passing in the array
move $a1, $s0 # Passing in num elements
jal sum_calc # Call the sum_calc function
move $a0, $v0
li $v0, 1
#move $a0, $v1 # Print the sum ($v0)
syscall
li $v0, 4
la $a0, newline
syscall
#------------------------
# PRINT AVERAGE BY CALLING AVG_CALC
#---------------------------
avg_print:
li $v0, 4
la $a0, print_avg_statement # Print statement
syscall
la $a0, array
move $a1, $s0
jal avg_calc # Call avg_calc function
move $v1, $v0
move $a0, $v1
li $v0, 1
syscall
#li $v0, 4
#move $a0, $v1 # Print the sum ($s1 = $t3 = average holder)
#syscall
#---------------------------
# PRINT MINIMUM VALUE BY CALLING MIN_INT
#---------------------------
min_print:
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, print_min_statement # Print statement
syscall
la $a0, array
move $a1, $s0
jal min_int # Call min_int function
move $a0, $v0
li $v0, 1
#move $a0, $v1 # Print integer minimum
syscall
#--------------------------
# PRINT MAXIMUM VALUE BY CALLING MAX_INT
#--------------------------
max_print:
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, print_max_statement # Print statement
syscall
la $a0, array
move $a1, $s0
jal max_int # Call max_int function
move $a0, $v0
li $v0, 1
#move $a0, $v1 # Print integer maximum
syscall
#--------------------------
# PRINT NUM OF INTEGERS ABOVE AVG BY CALLING ABOVE_AVG
#--------------------------
above_avg_print:
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, print_above_statement # Print statement
syscall
la $a0, array
move $a1, $s0 # $s0 holds the average
move $a2, $v1
jal above_avg # Call above_avg function
move $a0, $v0
li $v0, 1
#move $a0, $v0 # Print integer count
syscall
#------------------------
# PRINT NUM OF INTEGERS BELOW AVG BY CALLING BELOW_AVG
#--------------------------
below_avg_print:
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, print_below_statement # Print statement
syscall
la $a0, array
move $a1, $s0
move $a2, $v1
jal below_avg # Call below_avg function
move $a0, $v0
li $v0, 1
#move $a0, $v0 # Print integer count
syscall
#------------------------
# EXIT PROGRAM
#------------------------
exit:
li $v0, 10
syscall
#------------------------
# FUNCTIONS
#------------------------
# FUNCTION 1 - READ IN ARRAY LOOP
#------------------------
intRead:
addi $sp, $sp, -4 # Allocating space on the stack to save the $ra
sw $ra, 0($sp) # Save return address to stack
# Setting up loop
li $t0, 0 # Loop counter
move $t1, $a0 # Pass the base address of the array into $t1
li $t2, 0 # Integer counter
intRead_loop:
beq $t2, 10, intRead_exit # Break when count reaches 10
# Prompt for userinput
li $v0, 4
la $a0, userPrompt
syscall
# Read in userinput
li $v0, 5
syscall
beq $v0, -9, intRead_exit # Break when userinput is equal to -9
sw $v0, 0($t1) # Store word(sw) fron register $v0 (userinput) into the array at the first open location 0
#Increment variables
addi $t1, $t1, 4 # Increment array address by 4 (4 bytes)
addi $t0, $t0, 1 # Increment loop counter by 1
addi $t2, $t2, 1 # Increment integer count by 1
j intRead_loop
intRead_exit: # Exit function to save variables and stops loop to be called for printing
move $s0, $t2
lw $ra, 0($sp) # Load in the previous return address
addi $sp, $sp, 4 # Deallocating space on the stack - moving $sp back/down
jr $ra # Return to register address
#------------------------
# FUNCTION 2 - COMPUTE INTEGER SUM
#------------------------
sum_calc:
addi $sp, $sp, -4 # Allocating space on the stack to save the $ra
sw $ra, 0($sp) # Save return address to stack
move $t1, $a0 # Moving array address back to temp register
move $t2, $a1 # Moving numb of integers back to temp register
li $t0, 0 # Redeclare $t0 as 0 for sum_loop counter
li $t3, 0 # declare $t3 as 0 for sum of array
la $t1, array
sum_loop:
beq $t0, $t2, sum_exit # When $t0 is equal to $s0 ($t2 - integer count) break and go to sum_exit
lw $t4, 0($t1) # Loading the integer value into $a0
#Increment variables
add $t3, $t3, $t4 # Add $t3(0) to $t4(first num from array)
addi $t1, $t1, 4
addi $t0, $t0, 1
j sum_loop
sum_exit: # Exit function to save variables and stops loop to be called for printing
move $v0, $t3 # Saving the sum into accessable register $s1
lw $ra, 0($sp) # Load in the previous return address
addi $sp, $sp, 4 # Deallocating space on the stack - moving $sp back/down
jr $ra # Return to register address
#------------------------
# FUNCTION 3- AVERAGE FUNCTION (CALLING SUM_CALC)
#------------------------
avg_calc:
addi $sp, $sp, -4 # Allocating space on the stack to save the $ra
sw $ra, 0($sp) # Save return address to stack
li $t0, 0 # Redeclare $t0 as 0 for sum_loop counter
la $a0, array # Redeclaring the array in the $a0 register to be passed in
move $a1, $s0 # Moving number of intagers to $a1 so sum_calc can recieve
jal sum_calc # Call in the sum function rather than passing in the value through registers
# $v0 should contains sum
move $t2, $s0 # Using the saved integer count
div $t3, $t3, $t2 # $v0 should contains sum
#mflo $v0
avg_exit: # Exit function to save variables and stops loop to be called for printing
#move $s1, $t3 # Saving the sum into accessable register $s1 = $t3 = Average of the integers
#move $s0, $t2 # Saving the sum into accessable register $s0
move $v1, $t3
lw $ra, 0($sp) # Load in previous return address
add $sp, $sp, 4 # Deallocating space on the stack - moving $sp back/down
jr $ra # Return to register address
#------------------------
# FUNCTION 4 - COMPUTE MIN OF INTEGER ARRAY
#------------------------
min_int:
addi $sp, $sp, -4 # Allocating space on the stack to save the $ra
sw $ra, 0($sp) # Save return address to stack
move $t1, $a0 # Passing in $a0
move $t2, $a1
li $t0, 0 # redeclare $t0 as 0
lw $t4, 0($t1) # $t4 to hold initial array val ($t4 = MINIMUM)
min_int_loop:
beq $t0, $t2, min_exit # When $t0 is equal to $s0 ($t2 - integer count) break and go to min_exit
lw $t5, 0($t1) # $t5 = current val in array
bge $t5, $t4, same_min # Break if min is greater than or equal to current val
move $t4, $t5 # Set MIN to current (minimum) value
#Increment variables
addi $t1, $t1, 4 # Next value in array
addi $t0, $t0, 1 # Increment min_int_loop count
j min_int_loop
same_min:
#Increment variables
addi $t1, $t1, 4 # Next value in array
addi $t0, $t0, 1 # Increment min_int_loop count
j min_int_loop
min_exit: # Exit function to save variables and stops loop to be called for printing
move $v0, $t4 # Saving the sum into accessable register $s2 = $t4 = Minimum of the integers
lw $ra, 0($sp) # Load in previous return address
add $sp, $sp, 4 # Deallocating space on the stack - moving $sp back/down
jr $ra # Return to register address
#-------------------------
# FUNCTION 5 - COMPUTE MAX OF INTEGER ARRAY
#-------------------------
max_int:
addi $sp, $sp, -4 # Allocating space on the stack to save the $ra
sw $ra, 0($sp) # Save return address to stack
move $t1, $a0
move $t2, $a1
li $t0, 0 # Redeclare $t0
lw $t4, 0($t1) # $t4 to hold initial array val ($t4 = MAXIMUM)
max_int_loop:
beq $t0, $t2, max_exit
lw $t5, 0($t1) # $t5 = current val in array
#bgt $t5, $t4, new_max # Break if max is greater than current val
blt $t4, $t5, new_max
#Increment variables
addi $t1, $t1, 4 # Next value in array
addi $t0, $t0, 1 # Increment max_int_loop count
j max_int_loop
new_max:
move $t4, $t5 # Set MAX to current (maximum) value
#Increment variables
addi $t1, $t1, 4 # Next value in array
addi $t0, $t0, 1 # Increment max_int_loop count
j max_int_loop
max_exit: # Exit function to save variables and stops loop to be called for printing
move $v0, $t4 # Saving the sum into accessable register $s2 = $t4 = Maximum of the integers
lw $ra, 0($sp) # Load in previous return address
add $sp, $sp, 4 # Deallocating space on the stack - moving $sp back/down
jr $ra # Return to register address
#-----------------------------
# FUNCTION 6 - NUMBER OF INTEGERS ABOVE AVERAGE
#-----------------------------
above_avg:
addi $sp, $sp, -4 # Allocating space on the stack to save the $ra
sw $ra, 0($sp) # Save return address to stack
move $t1, $a0
move $t2, $a1
move $t3, $a2
li $t0, 0 # Redeclare $t0
li $t5, 0 # Declare $t5 to hold above_count
above_avg_loop:
beq $t0, $t2, above_avg_exit # Break when $t0 and $s0 ($t2) are equal, go to above_avg_exit
lw $t4, 0($t1) # $t4 = current val in array
bgt $t4, $t3, is_above # Break if less than
#Increment variables
addi $t1, $t1, 4 # Next value in array
addi $t0, $t0, 1 # Increment loop count
j above_avg_loop
is_above:
#Increment variables
addi $t5, $t5, 1 # Increment above_count
addi $t1, $t1, 4 # Next value in array
addi $t0, $t0, 1 # Increment loop count
j above_avg_loop
above_avg_exit: # Exit function to save variables and stops loop to be called for printing
move $v0, $t5 # Saving the sum into accessable register $s4 = $t5 = Number of integers above avg
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#-------------------------------
# FUNCTION 7 - NUMBER OF INTEGERS BELOW AVERAGE
#-----------------------------
below_avg:
addi $sp, $sp, -4 # Allocating space on the stack to save the $ra
sw $ra, 0($sp) # Save return address to stack
move $t1, $a0
move $t2, $a1
move $t3, $a2
li $t0, 0 # Redeclare $t0
li $t5, 0 # Declare $t5 to hold above_count
below_avg_loop:
beq $t0, $t2, below_avg_exit # Break when $t0 and ($t2) are equal, go to exit
lw $t4, 0($t1) # $t4 = current val in array
blt $t4, $t3, is_below # break if less than
#Increment variables
addi $t1, $t1, 4 # Next value in array
addi $t0, $t0, 1 # Increment loop count
j below_avg_loop
is_below:
#Increment variables
addi $t5, $t5, 1 # Increment below_count
addi $t1, $t1, 4 # Next value in array
addi $t0, $t0, 1 # Increment loop count
j below_avg_loop
below_avg_exit: # Exit function to save variables and stops loop to be called for printing
move $v0, $t5 # Saving the sum into accessable register $s3 = $t5 = Number of integers below avg
lw $ra, 0($sp)
add $sp, $sp, 4
jr $ra
#-------------------------------