Question

Can you translate this C code into MIPS assembly with comment? #include <stdio.h> #include <math.h> #include...

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

Homework Answers

Answer #1

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!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Expand the code below to display both the value of the loop variable and this value...
Expand the code below to display both the value of the loop variable and this value squared. Put the pairs of values on a new line. #include <stdio.h> #include <stdlib.h> int main(void) { int i; int a; int b; do { printf("Please enter the lower value: "); scanf("%d", &a); printf("Please enter the upper value: "); scanf("%d", &b); if (a>b) printf("The upper value must be greater than the lower value\n\n"); } while (a>b); for(i=a; i<=b; i++) printf("%d ", i); return 0;...
Translate C code into MIPS. Do not include an exit syscall int proc1( int a, int...
Translate C code into MIPS. Do not include an exit syscall int proc1( int a, int b ) { if ( proc2( a, b ) >= 0 ) return 1; else return -1; } int proc2( int a, int b ) { return (a*10) & (b*10); } int main() { int a = 9; int b = -10; int c = a + b + proc1( a, b ); printf("%d\n", c ); return 0; }
Run the below code and shift binary left and right and understand the output. #include <stdio.h>...
Run the below code and shift binary left and right and understand the output. #include <stdio.h> int main() {     int num=212, i;     for (i=0; i<=2; i++)         printf("Right shift by %d: %d\n", i, num>>i);      printf("\n");      for (i=0; i<=2; i++)         printf("Left shift by %d: %d\n", i, num<<i);             return 0; } Output:
How can i make this lunix code print 3 numbers in reverse it must be in...
How can i make this lunix code print 3 numbers in reverse it must be in printStars format and no loops Here is the code i have but can figure out how to reverse the numbers #include<stdio.h> void printStars(int n) { if (n>0){ printf(""); printStars(n-1); } } int main() { int n; int b; int c; printf("Enter 3 numbers to reverse "); scanf("%d",&n,&b,&c); printf("your reversed numbers are %d",n); printStars(n); return 0;
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...
Considering the following code fragment: #include<stdio.h> #include <stdlib.h> int base = 0; static int bonus; int*...
Considering the following code fragment: #include<stdio.h> #include <stdlib.h> int base = 0; static int bonus; int* demage (int dmg) { int* ptr = (int*) malloc(sizeof(int)); static int constant = 1;   *ptr = dmg + constant; return ptr; } int main() { int HP = 0; int* dm; base = 30; bonus = 20; for(int i=1; i<4; i++) { HP += i * base; } dm = demage(10); HP += bonus - *dm; printf("Total HP: %i", HP); return 0; } Please...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
would someone kindly convert the following C code to java? thanks so much # include <stdio.h>...
would someone kindly convert the following C code to java? thanks so much # include <stdio.h> int main(void) { int changeOwed; int check; char invisibleChar; int count = 0; int numQ=0, numD=0, numN=0, numP=0; do{ printf("How much change is owed (in cents)?\n"); check = scanf("%d", &changeOwed); // returns 1 if the input is a number and returns 0 otherwise //Get's rid of any extra invisible characters if the input is a char/String do{ scanf("%c",&invisibleChar); }while(invisibleChar != '\n'); }while(check == 0...
#include <stdio.h> #include <stdlib.h> int main () { int k; int aux; int size = 8;...
#include <stdio.h> #include <stdlib.h> int main () { int k; int aux; int size = 8; int x[8] = {2, 3, 5, 7, 11, 13, 17, 19}; scanf("%d",&k); aux = x[k]; for (int i = k; i < size - 1; i++) x[i] = x[ i + 1]; x[ size - 1] = aux;   for (int i = 0; i < size; i++) printf("%d ", x[i]); } change this program to Write a program to remove an element from an...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...