# ECE3055 SPIM Programming Assignment 1 # Name: YOUR NAME HERE. Spring 2003 # Section: YOUR SECTION HERE # Global Data Segment .data array1: .word 10,10,20,20,30,30,40,40,0 array2: .word 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,0 array3: .word 0 array4: .word 0xf, 0xf0, 0xf00, 0xf000, 0xf0000 .word 0xf00000, 0xf000000, 0xf0000000, 1, 0 array5: .word 0xf0, 0xf00, 0xf000, 0xf0000 .word 0xf00000, 0xf000000, 0xf0000000, 1, 0 Array1R: .asciiz "Results from array1\n" Array2R: .asciiz "Results from array2\n" Array3R: .asciiz "Results from array3\n" Array4R: .asciiz "Results from array4\n" Array5R: .asciiz "Results from array5\n" PressKey: .asciiz "Press any key to continue\n" AllDone: .asciiz "Exiting....\n" # Global variables for results sum: .word 0 # Store the SUM here count: .word 0 # Store the count of items here avg: .word 0 # Store the average here min: .word 0 # Store the minimum value here max: .word 0 # Store the maximum value here Ret: .asciiz "\n" SumString: .asciiz "Sum is " CountString: .asciiz "Number entries is " AvgString: .asciiz "Average is " MinString: .asciiz "Min is " MaxString: .asciiz "Max is " .text .globl main main: la $a0 array1 # Address of array1 jal Compute la $a0,Array1R li $v0,4 # Output string syscall code syscall jal PrintResults jal GetKey la $a0 array2 # Address of array2 jal Compute la $a0,Array2R li $v0,4 # Output string syscall code syscall jal PrintResults jal GetKey la $a0 array3 # Address of array3 jal Compute la $a0,Array3R li $v0,4 # Output string syscall code syscall jal PrintResults jal GetKey la $a0 array4 # Address of array4 jal Compute la $a0,Array4R li $v0,4 # Output string syscall code syscall jal PrintResults jal GetKey la $a0 array5 # Address of array5 jal Compute la $a0,Array5R li $v0,4 # Output string syscall code syscall jal PrintResults jal GetKey # Notify operating system program is done la $a0,AllDone # Address of all done prompt li $v0,4 # Output string syscall code syscall li $v0,10 # exit syscall # Subroutine GetKey # Wait for a user keystroke GetKey: la $a0,PressKey # Address of press key prompt li $v0,4 # Output string syscall code syscall # li $v0,12 # Read character li $v0,5 # Read integer syscall # Wait for user input jr $ra # Subroutine Compute # Compute the count, sum, average, min and max of values in an array # Entry: a0 = address of array # The array is of variable length, terminated with a 0 word # Compute: # This version uses only one branch, the backwards branch # at the end of the main loop li $s0,0 # Initialize sum li $s1,0 # Initialize count lw $t0,0($a0) # First word from array move $s2,$t0 # Initilaize maximium move $s3,$t0 # Initialize minimum Loop1: lw $t0,0($a0) # Next word from array sltiu $t1,$t0,1 # t1 = 1 iff t0 == 0, zero otherwise xori $t1,$t1,1 # t1 = 0 iff t0 == 0, 1 otherwise sll $t1,$t1,31 # t1 = 0 iff t0 == 0, 0x80000000 otherwise sra $t1,$t1,31 # t1 = 0 iff t0 == 0, 0xffffffff otherwise addu $s0,$t0,$s0 # Add to total addi $s1,$s1,1 # Count number of entries # Calculate new minimum sltu $t2,$t0,$s3 # t2 = 1 if new minimum sll $t2,$t2,31 sra $t2,$t2,31 # t2 = 0xffffffff if new minimum, 0 otherwise subu $t3,$s3,$t0 # t3 = difference between old and new min and $t3,$t3,$t2 # t3 = zero if not new min, difference if so and $t3,$t3,$t1 # Also zero out difference if new value = 0 subu $s3,$s3,$t3 # New or old minimum # Calculate new maximum sgtu $t2,$t0,$s2 # t2 = 1 if new maximum sll $t2,$t2,31 sra $t2,$t2,31 # t2 = 0xffffffff if new maximum, 0 otherwise subu $t3,$t0,$s2 # t3 = difference between old and new max and $t3,$t3,$t2 # t3 = zero if not new max, difference if so addu $s2,$s2,$t3 # Advance address and loop addi $a0,$a0,4 # Advance to next word bne $t0,$zero,Loop1 # Process next word # End of array found addi $s1,$s1,-1 # Counted the zero entry by mistake la $t0,sum sw $s0,0($t0) # Store sum la $t0,count sw $s1,0($t0) # Store count la $t0,max sw $s2,0($t0) # Store max la $t0,min sw $zero,0($t0) # Store zero for now, set later if count > 0 la $t0,avg sw $zero,0($t0) # And store zero avg in case empty list # Compute average beq $s1,$zero,Loop5 # Insure no divide by zero la $t0,min sw $s3,0($t0) # Store min divu $t1,$s0,$s1 # Average in t1 la $t0,avg sw $t1,0($t0) # Store the average Loop5: jr $ra # Return # Subroutine PrintResults # This should print: # "Sum is xxx" # "Number entries is xxx" # "Average is xxx" # "Minimum is xxx" # "Maximum is xxx" PrintResults: # YOUR CODE HERE la $a0,SumString li $v0,4 # See SPIM ref. man. for syscall values syscall # Output the sum value la $t0,sum lw $a0,0($t0) # Load the sum value li $v0,1 syscall # End of line la $a0,Ret li $v0,4 syscall # Output the count la $a0,CountString li $v0,4 # See SPIM ref. man. for syscall values syscall # Output the count value la $t0,count lw $a0,0($t0) # Load the sum value li $v0,1 syscall # End of line la $a0,Ret li $v0,4 syscall # Output the average la $a0,AvgString li $v0,4 # See SPIM ref. man. for syscall values syscall # Output the average value la $t0,avg lw $a0,0($t0) # Load the average value li $v0,1 syscall # End of line la $a0,Ret li $v0,4 syscall # Output the minimum prompt la $a0,MinString li $v0,4 # See SPIM ref. man. for syscall values syscall # Output the minimum value la $t0,min lw $a0,0($t0) # Load the minimum value li $v0,1 syscall # End of line la $a0,Ret li $v0,4 syscall # Output the maximum prompt la $a0,MaxString li $v0,4 # See SPIM ref. man. for syscall values syscall # Output the maximum value la $t0,max lw $a0,0($t0) # Load the maximum value li $v0,1 syscall # End of line la $a0,Ret li $v0,4 syscall jr $ra