Hi all I'm working on a problem involving translating an example C array code into the MIPS version, but I'm not quite sure how to do it right. Here is the sample code: example: int count(int a[], int n, int x) { int res=0; int i; for(i=0;i!=n;i++) if(a==x) res=res+1; return res; } Here is what I have so far with my code according to what I understand. Any assistance would be appreciated! move $t0, $zero # register t0 = 0 move $t1, $zero # register t1 = 0 loop: sll $t2,$t0,2 #t2 = i * 4 add $t3,$a0,$t2 # $t3 = address of array a[] sw $zero, 0($t2) # array a[] = 0 bne $ # check the end of the loops
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
You can do the MIPS conversion using ctomips.com I used it to check against my homework. It works pretty well.