Can you translate this C code into MIPS assembly with comment?
#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
Hello dear student. I have solved your question in both MIPS64 and MIPS simple. You can use whatever you like according to your need. If you have any doubts futher , you can ask in comments.
Do hit LIKE if you find the anser useful because it shows your love and support which motivates us.
1. Here is the code in MIPS64 :-
.LC1:
.ascii "Please enter an angle (Unit: Degree): \000"
.LC2:
.ascii "%d\000"
.LC6:
.ascii "The value sine of angle %d is Sin(%d) = %f\012\000"
main:
daddiu $sp,$sp,-96
sd $31,72($sp)
sd $fp,64($sp)
sd $28,56($sp)
sdc1 $f24,88($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(main)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(main)))
sw $0,0($fp)
li $2,1 # 0x1
sw $2,4($fp)
ld $2,%got_page(.LC0)($28)
ldc1 $f0,%got_ofst(.LC0)($2)
sdc1 $f0,8($fp)
li $2,1000 # 0x3e8
sw $2,16($fp)
ld $2,%got_page(.LC1)($28)
daddiu $4,$2,%got_ofst(.LC1)
ld $2,%call16(printf)($28)
move $25,$2
nop
daddiu $2,$fp,40
move $5,$2
ld $2,%got_page(.LC2)($28)
daddiu $4,$2,%got_ofst(.LC2)
ld $2,%call16(__isoc99_scanf)($28)
move $25,$2
nop
lw $2,40($fp)
mtc1 $2,$f0
cvt.d.w $f1,$f0
ld $2,%got_page(.LC3)($28)
ldc1 $f0,%got_ofst(.LC3)($2)
mul.d $f1,$f1,$f0
ld $2,%got_page(.LC4)($28)
ldc1 $f0,%got_ofst(.LC4)($2)
div.d $f0,$f1,$f0
sdc1 $f0,24($fp)
.L2:
lw $2,4($fp)
addiu $2,$2,-1
dext $2,$2,0,32
dmtc1 $2,$f0
cvt.d.l $f1,$f0
ld $2,%got_page(.LC5)($28)
ldc1 $f0,%got_ofst(.LC5)($2)
mov.d $f13,$f1
mov.d $f12,$f0
ld $2,%call16(pow)($28)
move $25,$2
nop
mov.d $f24,$f0
lw $2,4($fp)
sll $2,$2,1
addiu $2,$2,-1
dext $2,$2,0,32
dmtc1 $2,$f0
cvt.d.l $f0,$f0
mov.d $f13,$f0
ldc1 $f12,24($fp)
ld $2,%call16(pow)($28)
move $25,$2
nop
mul.d $f24,$f24,$f0
lw $2,4($fp)
sll $2,$2,1
addiu $2,$2,-1
dext $2,$2,0,32
dmtc1 $2,$f0
cvt.d.l $f0,$f0
mov.d $f12,$f0
ld $2,%got_disp(fact)($28)
move $25,$2
nop
div.d $f0,$f24,$f0
sdc1 $f0,32($fp)
lwc1 $f0,0($fp)
cvt.d.s $f1,$f0
ldc1 $f0,32($fp)
add.d $f0,$f1,$f0
cvt.s.d $f0,$f0
swc1 $f0,0($fp)
lw $2,4($fp)
addiu $2,$2,1
sw $2,4($fp)
lw $2,16($fp)
lw $3,4($fp)
sltu $2,$2,$3
beq $2,$0,.L2
nop
lw $2,40($fp)
lw $3,40($fp)
lwc1 $f0,0($fp)
cvt.d.s $f0,$f0
dmfc1 $7,$f0
move $6,$3
move $5,$2
ld $2,%got_page(.LC6)($28)
daddiu $4,$2,%got_ofst(.LC6)
ld $2,%call16(printf)($28)
move $25,$2
nop
nop
move $sp,$fp
ld $31,72($sp)
ld $fp,64($sp)
ld $28,56($sp)
ldc1 $f24,88($sp)
daddiu $sp,$sp,96
j $31
nop
fact:
daddiu $sp,$sp,-48
sd $fp,40($sp)
move $fp,$sp
lui $3,%hi(%neg(%gp_rel(fact)))
daddu $3,$3,$25
daddiu $3,$3,%lo(%neg(%gp_rel(fact)))
sdc1 $f12,16($fp)
ld $2,%got_page(.LC0)($3)
ldc1 $f0,%got_ofst(.LC0)($2)
sdc1 $f0,0($fp)
ld $2,%got_page(.LC0)($3)
ldc1 $f0,%got_ofst(.LC0)($2)
sdc1 $f0,8($fp)
b .L4
nop
.L5:
ldc1 $f1,0($fp)
ldc1 $f0,8($fp)
mul.d $f0,$f1,$f0
sdc1 $f0,0($fp)
ldc1 $f1,8($fp)
ld $2,%got_page(.LC0)($3)
ldc1 $f0,%got_ofst(.LC0)($2)
add.d $f0,$f1,$f0
sdc1 $f0,8($fp)
.L4:
ldc1 $f1,8($fp)
ldc1 $f0,16($fp)
c.le.d $fcc0,$f1,$f0
bc1t $fcc0,.L5
nop
ldc1 $f0,0($fp)
move $sp,$fp
ld $fp,40($sp)
daddiu $sp,$sp,48
j $31
nop
.LC0:
.word 0
.word 1072693248
.LC3:
.word 1413754136
.word 1074340347
.LC4:
.word 0
.word 1080459264
.LC5:
.word 0
.word -1074790400
2. Here is your code in early simple MIPS :-
$LC1:
.ascii "Please enter an angle (Unit: Degree): \000"
$LC2:
.ascii "%d\000"
$LC7:
.ascii "The value sine of angle %d is Sin(%d) = %f\012\000"
main:
addiu $sp,$sp,-96
sw $31,84($sp)
sw $fp,80($sp)
swc1 $f20,92($sp)
swc1 $f21,88($sp)
move $fp,$sp
sw $0,32($fp)
li $2,1 # 0x1
sw $2,36($fp)
lui $2,%hi($LC0)
lwc1 $f0,%lo($LC0+4)($2)
nop
lwc1 $f1,%lo($LC0)($2)
nop
swc1 $f0,44($fp)
swc1 $f1,40($fp)
li $2,1000 # 0x3e8
sw $2,48($fp)
lui $2,%hi($LC1)
addiu $4,$2,%lo($LC1)
jal printf
nop
addiu $2,$fp,72
move $5,$2
lui $2,%hi($LC2)
addiu $4,$2,%lo($LC2)
jal __isoc99_scanf
nop
lw $2,72($fp)
nop
mtc1 $2,$f0
nop
cvt.d.w $f2,$f0
lui $2,%hi($LC3)
lwc1 $f0,%lo($LC3+4)($2)
nop
lwc1 $f1,%lo($LC3)($2)
nop
mul.d $f2,$f2,$f0
lui $2,%hi($LC4)
lwc1 $f0,%lo($LC4+4)($2)
nop
lwc1 $f1,%lo($LC4)($2)
nop
div.d $f0,$f2,$f0
swc1 $f0,60($fp)
swc1 $f1,56($fp)
$L5:
lw $2,36($fp)
nop
addiu $2,$2,-1
mtc1 $2,$f0
nop
cvt.d.w $f0,$f0
bgez $2,$L2
nop
lui $2,%hi($LC5)
lwc1 $f2,%lo($LC5+4)($2)
nop
lwc1 $f3,%lo($LC5)($2)
nop
add.d $f0,$f0,$f2
$L2:
lui $2,%hi($LC6)
lwc1 $f2,%lo($LC6+4)($2)
nop
lwc1 $f3,%lo($LC6)($2)
mov.d $f14,$f0
mov.d $f12,$f2
jal pow
nop
mov.d $f20,$f0
lw $2,36($fp)
nop
sll $2,$2,1
addiu $2,$2,-1
mtc1 $2,$f0
nop
cvt.d.w $f0,$f0
bgez $2,$L3
nop
lui $2,%hi($LC5)
lwc1 $f2,%lo($LC5+4)($2)
nop
lwc1 $f3,%lo($LC5)($2)
nop
add.d $f0,$f0,$f2
$L3:
mov.d $f14,$f0
lwc1 $f12,60($fp)
nop
lwc1 $f13,56($fp)
jal pow
nop
mul.d $f20,$f20,$f0
lw $2,36($fp)
nop
sll $2,$2,1
addiu $2,$2,-1
mtc1 $2,$f0
nop
cvt.d.w $f0,$f0
bgez $2,$L4
nop
lui $2,%hi($LC5)
lwc1 $f2,%lo($LC5+4)($2)
nop
lwc1 $f3,%lo($LC5)($2)
nop
add.d $f0,$f0,$f2
$L4:
mov.d $f12,$f0
jal fact
nop
div.d $f0,$f20,$f0
swc1 $f0,68($fp)
swc1 $f1,64($fp)
lwc1 $f0,32($fp)
nop
cvt.d.s $f2,$f0
lwc1 $f0,68($fp)
nop
lwc1 $f1,64($fp)
nop
add.d $f0,$f2,$f0
cvt.s.d $f0,$f0
swc1 $f0,32($fp)
lw $2,36($fp)
nop
addiu $2,$2,1
sw $2,36($fp)
lw $2,48($fp)
lw $3,36($fp)
nop
sltu $2,$2,$3
beq $2,$0,$L5
nop
lw $2,72($fp)
lw $3,72($fp)
lwc1 $f0,32($fp)
nop
cvt.d.s $f0,$f0
swc1 $f0,20($sp)
swc1 $f1,16($sp)
move $6,$3
move $5,$2
lui $2,%hi($LC7)
addiu $4,$2,%lo($LC7)
jal printf
nop
nop
move $sp,$fp
lw $31,84($sp)
lw $fp,80($sp)
lwc1 $f20,92($sp)
nop
lwc1 $f21,88($sp)
addiu $sp,$sp,96
j $31
nop
fact:
addiu $sp,$sp,-32
sw $fp,28($sp)
move $fp,$sp
swc1 $f12,36($fp)
swc1 $f13,32($fp)
lui $2,%hi($LC0)
lwc1 $f0,%lo($LC0+4)($2)
nop
lwc1 $f1,%lo($LC0)($2)
nop
swc1 $f0,12($fp)
swc1 $f1,8($fp)
lui $2,%hi($LC0)
lwc1 $f0,%lo($LC0+4)($2)
nop
lwc1 $f1,%lo($LC0)($2)
nop
swc1 $f0,20($fp)
swc1 $f1,16($fp)
b $L7
nop
$L8:
lwc1 $f2,12($fp)
nop
lwc1 $f3,8($fp)
lwc1 $f0,20($fp)
nop
lwc1 $f1,16($fp)
nop
mul.d $f0,$f2,$f0
swc1 $f0,12($fp)
swc1 $f1,8($fp)
lwc1 $f2,20($fp)
nop
lwc1 $f3,16($fp)
lui $2,%hi($LC0)
lwc1 $f0,%lo($LC0+4)($2)
nop
lwc1 $f1,%lo($LC0)($2)
nop
add.d $f0,$f2,$f0
swc1 $f0,20($fp)
swc1 $f1,16($fp)
$L7:
lwc1 $f2,20($fp)
nop
lwc1 $f3,16($fp)
lwc1 $f0,36($fp)
nop
lwc1 $f1,32($fp)
nop
c.le.d $f2,$f0
nop
bc1t $L8
nop
lwc1 $f0,12($fp)
nop
lwc1 $f1,8($fp)
move $sp,$fp
lw $fp,28($sp)
addiu $sp,$sp,32
j $31
nop
$LC0:
.word 1072693248
.word 0
$LC3:
.word 1074340347
.word 1413754136
$LC4:
.word 1080459264
.word 0
$LC5:
.word 1106247680
.word 0
$LC6:
.word -1074790400
.word 0
Thankyou!
Get Answers For Free
Most questions answered within 1 hours.