.data #text strings used for output. newLine and inPrompt are used multiple times. inPrompt: .asciiz "Enter a number> " initTxt: .asciiz "This program will find the smallest number you enter.\nHow many numbers would you like to compare? " newLine: .asciiz "\n" retVal: .asciiz "The smallest number is: " #array used to store the data that is input by the user. .align 4 arr: .space 100 .text main: li $v0, 4 #this block prints out the string initTxt la $a0, initTxt syscall li $v0, 5 #this block gets the input on how many numbers are to be entered syscall move $s0, $v0 #s0 will hold the number and be used in loop1 add $s5, $s0, $0 #s5 will also hold the number and be used in loop2 #this loop will gather user input and store each entry in an array named 'arr' loop1: jal s_newLine #s_newLine subroutine prints out a new line. li $v0, 4 la $a0, inPrompt #each time it loops, we should print out the message. syscall li $v0, 5 #syscall for getting int input syscall move $s1, $v0 #move the result into s1 sw $s1, arr($s3) #store the value from s1 into the array, using $s3 as an index counter addi $s3, $s3, 4 #increment the index by 1 word (4 bytes) addi $s0, $s0, -1 #decrement the loop1 counter bne $s0, $0, loop1 #continue looping until loop1 counter is 0 addi $s3, $0, 0 #reset the index counter $s3 to 0 again lw $s1, arr($s3) #load the first two array values into $s1 and $s2 lw $s2, arr($s3) #$s1 and $s2 will be used to compare the entered values. #loop2 will find the smallest value in arr. #The smaller value will always be in $s1, if $s2 < $s1, it will be moved to $s2 loop2: blt $s2, $s1, lthan #branch to the lthan subloop if $s2 < $s1 loop2r: addi $s3, $s3, 4 #label is for loop2 return. Return label when coming back from lthan. increment the array index by 1 word (4 bytes). lw $s2, arr($s3) #load the next array value into register $s2 addi $s5, $s5, -1 #decrement the loop counter $s5 bne $s5, $0, loop2 #loop again if the counter is not 0 beq $s5, $0, final #if the counter is 0, skip the lthan loop and go to final lthan: move $s1, $s2 #moves the smaller value into s1 j loop2r #jump to where we left off in the loop final: jal s_newLine #print new line li $v0, 4 la $a0, retVal #print the return value string syscall li $v0, 1 #print int code is 1 move $a0, $s1 #print the return value integer syscall li $v0, 10 #exit! syscall s_newLine: li $v0, 4 la $a0, newLine #print a newline character syscall jr $ra #return to where program execution left off