23.8k views
1 vote
For the following MIPS code write the C equivalent: The following code fragment processes two arrays and produces an important value in register $v0. Assume that each array consists of 2500 words indexed 0 through 2499, that the base addresses of the arrays are stored in $a0 and $a1 respectively, and their sizes (2500) are stored in $a2 and $a3 , respectively. Add comments to the code and describe in one sentence what this code does. Specifically, what will be returned in $v0 ? Also, convert this MIPS code to C (you may find it easier to understand if you do this first!).

User RWendi
by
8.1k points

1 Answer

6 votes

Answer:

//get size of array one

int size1 =2500; // sll $a2,$a2,2

int array1[size1];

//get size of array two

int size2 = 2500; // sll $a3,$a3,2

int array2[size2];

int v0=0 ; // $v0,$zero,$zero

int t0 = 0; //$t0,$zero,$zero

do{ //outer:

//add $t4,$a0,$t0

//lw $t4,0($t4)

int t4 = array1[t0];

int t1 = 0; //$t1,$zer0,$zer0

do{

//add $t3,$a1,$t1

//lw $t3,0($t3)

int t3 = array2[t1];

if(t3==t4){ //bne $t3,$t4,skip

v0 = v0+1; //addi $v0,$v0,1

}

//skip:

t1 = t1+1; //addi $t1,$t1,4

}while(t1!=size2);

t0 = t0+1; //addi $to,$to,4

}while(t0!=size1);

return 0;

Given code loops through the two array. It will count the number of duplicates found in both array.

User Tanmay
by
8.1k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.