In: Computer Science
Can you translate this C code into MIPS assembly?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double fact (double);
void
main ()
{
int angle_in_D;
double term, angle_in_R;
float sine = 0;
unsigned int i = 1;
double sign = 1;
int n = 1000;
printf ("Please enter an angle (Unit: Degree): ");
scanf ("%d", &angle_in_D);
angle_in_R = angle_in_D * M_PI / 180.0;
do
{
term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i -
1);
sine = sine + term;
i++;
}
while (i <= n);
printf ("The value sine of angle %d is Sin(%d) = %f\n",
angle_in_D,
angle_in_D, sine);
}
double
fact (double x)
{
double fac = 1;
for (double i = 1; i <= x; i++)
{
fac = fac * i;
}
return fac;
}
I coded and tested the C code and seem like it's working well. NOt sure if there's any logical error, but when I tested for popular cases, it's correct
$LC1:
.ascii "%d\000"
$LC6:
.ascii "The value sine of angle %d is Sin(%d) = %f\012\000"
main:
addiu $sp,$sp,-136
sw $31,84($sp)
sw $23,80($sp)
sw $22,76($sp)
sw $21,72($sp)
sw $20,68($sp)
sw $19,64($sp)
sw $18,60($sp)
sw $17,56($sp)
sw $16,52($sp)
swc1 $f30,132($sp)
swc1 $f31,128($sp)
swc1 $f28,124($sp)
swc1 $f29,120($sp)
swc1 $f26,116($sp)
swc1 $f27,112($sp)
swc1 $f24,108($sp)
swc1 $f25,104($sp)
swc1 $f22,100($sp)
swc1 $f23,96($sp)
swc1 $f20,92($sp)
swc1 $f21,88($sp)
addiu $5,$sp,32
lui $4,%hi($LC1)
addiu $4,$4,%lo($LC1)
jal scanf
nop
lwc1 $f0,32($sp)
nop
cvt.d.w $f0,$f0
lui $2,%hi($LC2)
lwc1 $f26,%lo($LC2+4)($2)
nop
lwc1 $f27,%lo($LC2)($2)
nop
mul.d $f0,$f0,$f26
lui $2,%hi($LC3)
lwc1 $f26,%lo($LC3+4)($2)
nop
lwc1 $f27,%lo($LC3)($2)
nop
div.d $f0,$f0,$f26
swc1 $f0,44($sp)
swc1 $f1,40($sp)
li $17,1 # 0x1
move $16,$0
mtc1 $0,$f24
lui $2,%hi($LC4)
lwc1 $f0,%lo($LC4+4)($2)
nop
lwc1 $f1,%lo($LC4)($2)
nop
mfc1 $21,$f0
mfc1 $20,$f1
lui $3,%hi($LC5)
lwc1 $f28,%lo($LC5+4)($3)
nop
lwc1 $f29,%lo($LC5)($3)
mtc1 $21,$f0
nop
mtc1 $20,$f1
nop
mfc1 $23,$f0
mfc1 $22,$f1
lui $18,%hi($LC0)
lwc1 $f26,%lo($LC0+4)($18)
nop
lwc1 $f27,%lo($LC0)($18)
nop
mov.d $f30,$f26
li $19,1000 # 0x3e8
$L7:
mtc1 $16,$f0
bgez $16,$L2
cvt.d.w $f14,$f0
mtc1 $21,$f0
nop
mtc1 $20,$f1
nop
add.d $f14,$f14,$f0
$L2:
mov.d $f12,$f28
jal pow
nop
mov.d $f22,$f0
mtc1 $17,$f0
bgez $17,$L3
cvt.d.w $f20,$f0
mtc1 $23,$f0
nop
mtc1 $22,$f1
nop
add.d $f20,$f20,$f0
$L3:
mov.d $f14,$f20
lwc1 $f12,44($sp)
nop
lwc1 $f13,40($sp)
jal pow
nop
c.le.d $f26,$f20
nop
bc1f $L12
mul.d $f0,$f22,$f0
lwc1 $f4,%lo($LC0+4)($18)
nop
lwc1 $f5,%lo($LC0)($18)
nop
mov.d $f2,$f4
$L6:
mul.d $f4,$f4,$f2
add.d $f2,$f2,$f30
c.le.d $f2,$f20
nop
bc1t $L6
nop
b $L14
div.d $f4,$f0,$f4
$L12:
lwc1 $f4,%lo($LC0+4)($18)
nop
lwc1 $f5,%lo($LC0)($18)
nop
div.d $f4,$f0,$f4
$L14:
cvt.d.s $f24,$f24
add.d $f24,$f4,$f24
cvt.s.d $f24,$f24
addiu $16,$16,1
bne $16,$19,$L7
addiu $17,$17,2
cvt.d.s $f24,$f24
swc1 $f24,20($sp)
swc1 $f25,16($sp)
lw $6,32($sp)
nop
move $5,$6
lui $4,%hi($LC6)
addiu $4,$4,%lo($LC6)
jal printf
nop
move $2,$0
lw $31,84($sp)
lw $23,80($sp)
lw $22,76($sp)
lw $21,72($sp)
lw $20,68($sp)
lw $19,64($sp)
lw $18,60($sp)
lw $17,56($sp)
lw $16,52($sp)
lwc1 $f30,132($sp)
nop
lwc1 $f31,128($sp)
lwc1 $f28,124($sp)
nop
lwc1 $f29,120($sp)
lwc1 $f26,116($sp)
nop
lwc1 $f27,112($sp)
lwc1 $f24,108($sp)
nop
lwc1 $f25,104($sp)
lwc1 $f22,100($sp)
nop
lwc1 $f23,96($sp)
lwc1 $f20,92($sp)
nop
lwc1 $f21,88($sp)
j $31
addiu $sp,$sp,136
fact(double):
lui $2,%hi($LC0)
lwc1 $f0,%lo($LC0+4)($2)
nop
lwc1 $f1,%lo($LC0)($2)
nop
c.le.d $f0,$f12
nop
bc1f $L23
mov.d $f2,$f0
mov.d $f4,$f0
$L18:
mul.d $f0,$f0,$f2
add.d $f2,$f2,$f4
c.le.d $f2,$f12
nop
bc1t $L18
nop
j $31
nop
$L23:
lwc1 $f0,%lo($LC0+4)($2)
nop
lwc1 $f1,%lo($LC0)($2)
j $31
nop
$LC0:
.word 1072693248
.word 0
$LC2:
.word 1074340347
.word 1413754136
$LC3:
.word 1080459264
.word 0
$LC4:
.word 1106247680
.word 0
$LC5:
.word -1074790400
.word 0