Please describe in one sentence what this code does. Assume that $a0 and $a1 are
ID: 3862104 • Letter: P
Question
Please describe in one sentence what this code does. Assume that $a0 and $a1 are used for input and both initially contain the integers a and b, respectively. Assume that $v0 is used for the output. Also, convert this MIPS code to C & add comments to the code.
Add $t0, $zero, $zero
loop: beq $a1, $zero, finish
add $t0, $t0, $a0
sub $a1, $a1, 1
j loop
finish; addj $t0, $t0, 100
add $v0, $t0, $zero
Explanation / Answer
It does a0*a1+100
int func(int a0,int a1,int a2,int a3)//In mips $a0 to $a4 are arguments to function
{
const int zero = 0;// $zero is register whose value is always 0
int t0 = zero;//t0 is temporary register
while(a1!=0)//beq is like a1!= 0 go out of loop
{
t0 = t0 + a0;//add a0 to "to" a1 times which is like a0*a1
a1 = a1 - 1;
}
t0 = t0 + 100;//a0*a1+100
return t0; // return this value
}
int main()
{
int a0,a1;
printf("Enter a0 and a1 ");
scanf("%d %d",&a0,&a1);
printf("%d*%d+100=%d ",func(a0,a1,0,0));
}