################################################################################ ## ## LSU EE 4720 Spring 2003 Homework 1 Problem 4 Solution ## ## # Assignment:http://www.ece.lsu.edu/ee4720/2003/hw01.pdf # Solution:http://www.ece.lsu.edu/ee4720/2003/hw01_sol.pdf ################################################################################ # The solution, the complete lookup routine, appears further below. # The code immediately below calls the lookup routine with a sample # array and displays the count that lookup returns. .data array: .word 1 6 4 8 3 1 6 4 5 1 3 1 array_end: msg: .asciiz "The array holds %/t0/d elements equal to %/a2/d." .text .globl __start __start: la $a0, array la $a1, array_end sub $a1, $a1, $a0 srl $a1, $a1, 2 jal lookup addi $a2, $0, 1 add $t0, $v0, $0 addi $v0, $0, 11 la $a0, msg syscall addi $v0, $0, 10 syscall ################################################################################ ## Problem 4 Solution lookup: # Call Arguments # # $a0: Address of first element of array. Array holds 32-bit integers. # $a1: Number of elements in array. # $a2: Element to count. # # Return Value # # $v0: Number of times $a2 appears in the array starting at $a0 # [ ] Fill as many delay slots as possible. # [ ] Avoid using too many instructions. # [ ] Avoid obviously unnecessary instructions. # Solution addi $v0, $0, 0 sll $t0, $a1, 2 add $t1, $t0, $a0 LOOP: beq $a0, $t1, DONE lw $s0,0($a0) bne $s0,$a2 LOOP addi $a0, $a0, 4 j LOOP addi $v0, $v0, 1 DONE: jr $ra nop