1. The following MIPS function processes an array and returnstwo values in $v0 a
ID: 3609780 • Letter: 1
Question
1. The following MIPS function processes an array and returnstwo values in $v0 and
$v1. Assuming that the array consists of 5000 words indexed 0through 4999, and its base
address is stored in $a0 and its size (5000) in $a1, comment alllines and describe in a
sentence or two what this code does. Specifically, what will bereturned in $v0 and $v1?
add $a1, $a1, $a1 #
add $a1, $a1, $a1 #
add $v0, $zero, $zero #
add $t0, $zero, $zero #
outer: add $t4, $a0, $t0 #
lw $t4, 0($t4) #
add $t5, $zero, $zero #
add $t1, $zero, $zero #
inner: add $t3, $a0, $t1 #
lw $t3, 0($t3) #
bne $t3, $t4, skip #
addi $t5, $t5, 1 #
skip: addi $t1, $t1, 4 #
bne $t1, $a1, inner #
slt $t2, $t5, $v0 #
bne $t2, $zero, next #
add $v0, $t5, $zero #
add $v1, $t4, $zero #
next: addi $t0, $t0, 4 #
bne $t0, $a1, outer #
Explanation / Answer
The following MIPS function processes an array and returns twovalues in $v0 and
$v1. Assuming that the array consists of 5000 words indexed 0through 4999, and its base
address is stored in $a0 and its size (5000) in $a1, comment alllines and describe in a
sentence or two what this code does. Specifically, what will bereturned in $v0 and $v1?
add $a1, $a1, $a1# a1=10000
add $a1, $a1, $a1# a1=20000 (byte index into array)
add $v0, $zero, $zero# v0=0
add $t0, $zero, $zero# t0=0
outer: add $t4, $a0, $t0# t4=address of element up to in array starts at array[0] and goes toend
lw $t4, 0($t4)# t4=array[t4] get next element of array
add $t5, $zero, $zero# t5=0
add $t1, $zero, $zero# t1=0
inner: add $t3, $a0, $t1# t3=address of element up to in array always starts at 1stelement
lw $t3, 0($t3)# t3=array[t3] also starts at array[0], but keeps getting nextelement each time through inner loop
bne $t3, $t4, skip# array[t3]!=array[t4] go to skip otherwise nextinstruction
addi $t5, $t5, 1# t5++
skip: addi $t1, $t1, 4# t1+=4 index of next element ofarray
bne $t1, $a1, inner# t1!=a1 done going through array? no go toinner yes continue
slt $t2, $t5, $v0# t5<v0 yes t2=1 otherwise t2=0 if t5 <v0 will go to next otherwise willcontinue
bne $t2, $zero, next# t5<v0 go tonext
add $v0, $t5, $zero# v0=t5 counter how many times elements equal
add $v1, $t4, $zero# v1=t4 value of array
next: addi $t0, $t0, 4# go to next element of array (add 4 to index)
bne $t0, $a1, outer# done with array? if yes exit, otherwise go to outer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.