I have an assembly code containing an array, and I simply cannot understand what actually the result in the $s2 register shows. If someone could help and explain or simplify it for me that would be great. Here's the code:
.data arr: .word 3 2 -6 1 4 10 530 115 2231 1422
arrSize: .word 10
.text
.global main
main:
la $s0, arr
la $t0, arrSize
lw $s1, 0($t0)
add $s2, $zero, $zero
loop:
lw $t1, 0($s0)
andi $t2, $t1, 1
bne $t2, $zero, skip
addi $s2, $s2, 1
skip:
addi $s0, $s0, 4
addi $s1, $s1, -1
bne $s1, $zero, loop
end:
add $v0, $zero, $s2
I've added some pseudo-code comments to the code:
It seems that this just iterates through the elements of
arr
, testing each element to see if it is even, and incrementing a count of even elements found. The final result (inv0
ands2
) will be 6, since there are 6 even elements in the array.