Question

In: Computer Science

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

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

Solutions

Expert Solution

$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

Related Solutions

Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
Translate the following C code to MIPS assembly. The main function and subfunction are translated to...
Translate the following C code to MIPS assembly. The main function and subfunction are translated to two separate .asm files. Finish the assembly code segment for the above requirement. int main() { int x=2; int y=1; int z=0; z=Subfunc(x,y); printf(“Value of z is: %d”, z); } int Subfunc(int x, int y) { int t1=0; t1=x+y+100; return t1;} File 1: .data str: .asciiz "The value of z:" .text #.globl main main: addi $s0, $0,2 #x addi $s1, $0,1 #y addi $s2,...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0 and initially containing 0 . Also assume base of array Arr is in $s3 while ( k < = 10 ) { Arr[k] = k ; k = k + 1; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT