In: Computer Science
Convert the following C function to the corresponding MIPS
assembly procedure:
int count(int a[], int n, int x)
{
int res = 0;
int i = 0;
int j = 0;
int loc[];
for(i = 0; i != n; i++)
if(a[i] == x) {
res = res + 1;
loc [j] = i;
j = j+1}
return res, loc;
}
int loc[]; array size is not defined
so,taking int loc[];
MIPS Code
count: .frame $fp,40,$31 # vars= 24, regs= 1/0, args= 0, gp= 8 .mask 0x40000000,-4 .fmask 0x00000000,0 .set noreorder .set nomacro addiu $sp,$sp,-40 sw $fp,36($sp) move $fp,$sp sw $4,40($fp) sw $5,44($fp) movz $31,$31,$0 sw $6,48($fp) move $4,$sp move $6,$4 sw $0,8($fp) sw $0,12($fp) sw $0,16($fp) lw $4,48($fp) nop addiu $5,$4,-1 sw $5,20($fp) move $5,$4 move $13,$5 move $12,$0 srl $5,$13,27 sll $8,$12,5 or $8,$5,$8 sll $9,$13,5 move $5,$4 move $11,$5 move $10,$0 srl $5,$11,27 sll $2,$10,5 or $2,$5,$2 sll $3,$11,5 move $2,$4 sll $2,$2,2 addiu $2,$2,3 addiu $2,$2,7 srl $2,$2,3 sll $2,$2,3 subu $sp,$sp,$2 move $2,$sp addiu $2,$2,3 srl $2,$2,2 sll $2,$2,2 sw $2,24($fp) sw $0,12($fp) b $L2 nop $L4: lw $2,12($fp) nop sll $2,$2,2 lw $3,40($fp) nop addu $2,$3,$2 lw $3,0($2) lw $2,48($fp) nop bne $3,$2,$L3 nop lw $2,8($fp) nop addiu $2,$2,1 sw $2,8($fp) lw $3,24($fp) lw $2,16($fp) nop sll $2,$2,2 addu $2,$3,$2 lw $3,12($fp) nop sw $3,0($2) lw $2,16($fp) nop addiu $2,$2,1 sw $2,16($fp) $L3: lw $2,12($fp) nop addiu $2,$2,1 sw $2,12($fp) $L2: lw $3,12($fp) lw $2,44($fp) nop bne $3,$2,$L4 nop move $2,$0 move $sp,$6 move $sp,$fp lw $fp,36($sp) addiu $sp,$sp,40 j $31 nop