# hmwk1-p2.asm # Author: Archana Chidanandan # This program uses a function CountSmallerInt to traverse through an array # and determine the count of array values that are smaller than a certain # number # It is meant to give students practice in using loop constructs, procedure # handling and array handling . # The program adheres to MIPS register conventions. # The main procedure that calls the procedure and then writes the result # to memory .globl main .globl continue .text main: la $t0, A # $t0 has the address of array A la $t2, size lw $t2, 0($t2) # $t2 has the size of the array la $a0, message # Print the message li $v0, 4 syscall li $v0, 5 # Read the integer syscall move $t1, $v0 # Input that is in $v0 is moved to $t1 # Assign the input parameters to the argument registers move $a0, $t0 move $a1, $t1 move $a2, $t2 jal CountSmallerInt # call the procedure move $t0, $v0 # $t0 = count of smaller values # Write the result to memory la $t1, count sw $t0, 0($t1) # Quit li $v0, 10 syscall # Procedure: CountSmallerInt # Input arguments: # $a0 - address of the start of array # $a1 - number to compare with # $a2 - size of the array # # Procedure will count the number of array elements smaller than $a1 # Will then return the count CountSmallerInt: move $t0, $a0 # $t0 has the address of the array move $t1, $a1 # $t1 has the number to compare with move $t2, $a2 # $t2 has the size of the array #Set up the values to search the array li $t3, 0 # $t3 is the index to traverse the array li $t4, 0 # $t4 will keep a count of the smaller values #Loop to traverse the array loop: beq $t3, $t2, doneReading sll $t5, $t3, 2 # $t5 = index x 4 add $t6, $t5, $t0 # $t6 = address of A[index] lw $t7, 0($t6) # $t7 = A[index] slt $t8, $t7, $t1 # $t8 = 1 if A[index] < number beq $t8, $0, continue # If $t8 !=1, then do not increment count addi $t4, $t4, 1 continue: addi $t3, $t3, 1 # increment index j loop doneReading: move $v0, $t4 # Move count to $v0 jr $ra # Return to calling procedure .data A: .word 2 3 -5 -6 8 4 -1 7 5 size: .word 9 count:.word 0 message: .asciiz "Enter the number to compare with:"