

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


sll $t0, $t3, 9 #shift $t3 by 9 bits and store in $t srl $t0, $t0, 15 # shift $t0 right by 15 bits
sll $a2, $a2, 2 #max I = 2500 * 4 sll $a3, $a3, 2 #max j = 2500 * 4 add $v0, $zero, $zero #$v0 = 0 add $t0, $zero, $zero # i= outer: add $t4, $a0, $t0 # $t4 = address of array1[i] lw $t4, 0($t4) # $t4 =array1[i] add $t1, $zero, $zero #j= inner: add $t3, $a1, $t1 #$t3= address of arrary2[j] lw $t3, 0($t3) # $t3=array2[j] bne $t3, $t4, skip # if (array1[i]!=array2[j]) skip $v0+=
addi $v0, $v0, 1 # $v0++ skip addi $t1, $t1, 4 # j++ bne $t1, $a3, inner # loop if j != 2500 * 4 addi $t0, $t0, 4 # i++ bne $t0, $a2, outer # loop if i != 2500 * 4 The code determines the number of matching elements between the two arrays and returns this number in register $v0.
Bugs in the MIPS program: Bug 1: Count ($v0) is initialized to zero, not –1 to avoid counting zero word. Bug 2: Count ($v0) is not incremented. Bug 3: Loops if word copied is equal to zero rather than not equal. Bug-free version: addi $v0, $zero, -1 # Initialize to avoid counting zero word loop: lw, $v1, 0($a0) # Read next word from source addi $v0, $v0, 1 # Increment count words copied sw $v1, 0($a1) # Write to destination addi $a0, $a0, 4 # Advance pointer to next source addi $a1, $a1, 4 # Advance pointer to next destination bne $v1, $zero, loop # Loop if word copied != zero
Pseudoinstruction Solution