1. (a) 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; for(i = 0; i != n; i++)
if(a[i] == x) res = res + 1;
return res;
}
Please find the required conversion as the following:
=================================================================
count:
daddiu $sp,$sp,-48
sd $fp,40($sp)
move $fp,$sp
sd $4,16($fp)
move $3,$5
move $2,$6
sll $3,$3,0
sw $3,24($fp)
sll $2,$2,0
sw $2,28($fp)
sw $0,0($fp)
sw $0,4($fp)
sw $0,4($fp)
b .L2
nop
.L4:
lw $2,4($fp)
dsll $2,$2,2
ld $3,16($fp)
daddu $2,$3,$2
lw $3,0($2)
lw $2,28($fp)
bne $2,$3,.L3
nop
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
.L3:
lw $2,4($fp)
addiu $2,$2,1
sw $2,4($fp)
.L2:
lw $3,4($fp)
lw $2,24($fp)
bne $3,$2,.L4
nop
lw $2,0($fp)
move $sp,$fp
ld $fp,40($sp)
daddiu $sp,$sp,48
j $31
nop
================================================================
Get Answers For Free
Most questions answered within 1 hours.