For each line of code, give a brief meaningful comment to indicate what the line
ID: 3747222 • Letter: F
Question
For each line of code, give a brief meaningful comment to indicate what the line of code is doing relative to the overall algorithm. (Do not merely restate the assembly language instruction in words.) Then describe in one sentence what result is computed and stored in $v0, (i.e., what the code fragment does).
Consider the MIPS assembly language code fragment below that processes two arrays, Array1 and Array2, using these registers $a2 $a3 Sa0 Sa1 $VO Base address of Array1 Base address of Array2 Result Number of elements in Array1 Number of elements in Array2 For each line of code, give a brief meaningful comment to indicate what the line of code is doing relative to the overall algorithm. (Do not merely restate the as- sembly language instruction in words.) Then describe in one sentence what result is computed and stored in SvO, (i.e., what the code fragment does) s1l Sa2, $a2, 2 sl1 Sa3, $a3, 2 add Sv0, Szero, Szero sub St0, $t0, $t0 outer: add St4, $a0, $t0 and $tl, $tl, Szero inner: add $t3, Sal, $tl lw $t3, 0($t3) bne St3, $t4, skip addi $vO, $v0, 1 skip: addi Stl, Stl, 4 bne $tl, $a3, inner addi $t0, $t0, 4 bne St0, $a2, outerExplanation / Answer
sll $a2, $a2, 2 # $a2 gets the bits in $a2
# shifted left logical by
# shift position '2'
# means bits in $a2 shifted left by 2 position
sll $a3,$a3,2 # bits in $a3 shifted left by 2 position
add $v0,$zero,$zero # adding $zero and $zero, and stroing in $v0
sub $t0,$t0,$t0 # substract $t0 from $t0 and stroing in $t0
outer: add $t4,$a0,$t0 # adding $a0 and $t0 storing in $t4
lw $t4, 0($t4) # loading the value in $t4 from the address in $t4
and $t1,$t1,$zero # logical and $zero with $t1 and stroing in $t1
inner: add $t3,$a1,$t1 # add $a1 with $t1 and stroing result in $t3
lw $t3,0($t3) # load value in $t3 from address in $t3
bne $t3,$t4,skip # if $t3 and $t4 is not equal then branch to skip
addi $v0,$v0,1 # adding immidiate value 1 in $v0 and storing result in $v0
skip: addi $t1, $t1, 4 # adding immidiate value 4 in $t1 and storing result in $t1
bne $t1, $a3, inner # branch not equal means if $t1 and $a3 is not equal
# branch to inner
addi $t0, $t0, 4 # adding immidiate value 4 in $t0 and storing result in $t0
bne $t0, $a2, outer # branch not equal means if $t0 and $a2 is not equal
# branch to outer
At end the result in $v0 is sum of number of same values in Array1 corresponding to each values in Array2,vice-versa
Example1.
Array1: 4 3 2 4
Array2: 2 3
result is: 2
Example2.
Array1: 4 3 2 4 2
Array2: 2 3
result is: 3
Example3:
Array1: 2 2 2 2 2
Array2: 2 3
result is: 5
Example3:
Array1: 2 2 2 2 2
Array2: 2 2
result is: 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.