Convert the following C procedure into MIPS assembly code: A.) int compare (int
ID: 3554143 • Letter: C
Question
Convert the following C procedure into MIPS assembly code:
A.)
int compare (int a, int b)
{
if (a>b)
return 1;
else
return 0;
}
B.)
int compare (int a, int b)
{
if ( subit ( a > b ) > 0)
return 1;
else
return 0;
}
C.)
int subit (int a, int b)
{
return a-b;
}
If anyone could give me a detailed answer with these I would very much appreciate it. I understand most everything but am kind of unclear on how to proceed with these. Thanks!
Explanation / Answer
A) #a is store in $a0 b is stored in $a1 compare: add $t0,$0,$0 slt $t0,$a1,$a0//set $t0=1 if b<a add $v0,$t0,$0//put value of $t0 in $v0 jr $ra//return to calling functionB) #a is store in $a0 b is stored in $a1 compare: addi $sp,$sp,-4 sw $ra,0($sp)//push $ra onto stack jal subit//call subit add $t3,$0,$0 slt $t3,$0,$v0//set $t3=1 if a-b>0 add $v0,$t3,$0 lw $ra,0($sp)//popping $ra addi $sp,$sp,4 jr $ra
C) #a is store in $a0 b is stored in $a1 subit: sub $t0,$a0,$a1 add $v0,$t0,$0 jr $ra