# File: hmwk01-4-soln.asm # Written by: Archana Chidanandan, Dec. 16, 2003 # # Given an array A in memory and element NUM, to find the number of occurences of NUM in A. # .text .globl main main: la $t0, NUM lw $t0, 0($t0) # Read number to count from memory la $t1, SIZE lw $t1, 0($t1) # Read size of array from memory. li $t2, 0 # count = 0 li $t3, 0 # i = 0 la $t4, A # Determine address of first element in array loop: beq $t3, $t1, done # Continue while (i != SIZE) # Read A[i] from memory sll $t5, $t3, 2 # i * 4 (determine byte value of index) add $t6, $t4, $t5 # Determine address of A[i] lw $t7, 0($t6) # Read A[i] from memory bne $t7, $t0, next # If (A[i] == NUM), continue addi $t2, $t2, 1 # count = count + 1 next: addi $t3, $t3, 1 # i = i + 1 j loop done: la $t0, COUNT sw $t2, 0($t0) # Write COUNT to memory li $v0, 10 syscall # Get ready to exit .data NUM: .word 2 SIZE: .word 6 COUNT: .word 0 A: .word 3 5 5 5 6 5