In: Computer Science
write a mips program to find the number of elements of the array that are divisible by 3.
Please up vote ,comment if any query . Thanks for question . Be safe .
Note : check attached image for output ,code compiled and tested in MARS MIPS simulator .
Program Plan :
Program :
.data #vaariable section
#array of word and its length is 10
array : .word 100,99,98,97,96,95,80,81,82,51
#null terminated string
result :.asciiz " elements of array divisible by
3."
.text #code section
main:
la $t0,array #$t0=address of array
in $t0
li $t1,10 #t1= length of array
10
li $t2,0 #loop count index
,$t2(i)=0
li $a0,0 #number of element
divisible by 3 count store in $a0
loop: #loop label
lw $t4,0($t0) #$t4= value at
address stored in $t0 = array[$t2]
div $t3,$t4,3 #
quotient($t3)=array[$t2]($t4)/3
mfhi $t3 #store reminder in $t3
from mflo
seq $t3,$t3,0 #if $t3 equal to 0
than set 1 into $t3
add $a0,$a0,$t3 #if t3 is 0 than 0
will add in $a0 else 1 will be add in $a0
addi $t2,$t2,1 #increment loop
count
addi $t0,$t0,4 #point to next array
element 4 is word offset
bne $t2,$t1,loop #if $t2(loop
index) not equal to 10($t1)length of array jump to loop
#when loop completes program
control comes here
li $v0,1 #syscall 1 to print
integer $a0 already have divisible count
syscall
la $a0,result #a0=address of
result
li $v0,4 #syscall 4 to print
string
syscall
#syscall 10 to terminate
program
li $v0,10
syscall
Output : array : .word 100,99,98,97,96,95,80,81,82,51
Please up vote ,comment if any query .