View Single Post
Pro contributor
18Feb2010,14:06  
virxen's Avatar
here is a small example that i made for you.
i do not use it as a function but as a single program.
modify it to your needs.

Code:
.data
str1: .asciiz "count="
.align 2
a: .space 5

.text 
.globl main

main:
         #create the array
         la $9,a #load address  of the array
         addi $8,$0,5 #value to store
         sw $8,0($9) #store data in array a[0]=5
         addi $9,$9,4 #next element
         addi $8,$0,5 #value to store
         sw $8,0($9) #store data in array a[1]=5
         addi $9,$9,4 #next element
         addi $8,$0,5 #value to store
         sw $8,0($9) #store data in array a[2]=5
         addi $9,$9,4 #next element
         addi $8,$0,6 #value to store
         sw $8,0($9) #store data in array a[3]=6
         addi $9,$9,4 #next element
         addi $8,$0,7 #value to store
         sw $8,0($9) #store data in array a[4]=7
         addi $9,$9,4 #next element
        add $14,$0,$0 #res=0;
        addi $16,$0,10 #n=5;
        addi $17,$0,5 #x=5;
        add $18,$0,$0#i=0; in order to start loop from 0
        la $9,a #load address  of the array
        loop: #for 
        lw $15 ,0($9) #a[i]
        addi $9,$9,4 #next element
        bne $15,$17,notequal
        addi $14,$14,1 #res=res+1;
        notequal: 
        addi $18, $18, 1 # i=i+1;
        bne $18, $16, loop #next
        #print result
           addi $2,$0,4
           la $4,str1
           syscall #count=
           addi $2,$0,1
           add $4,$0,$14 
           syscall# res