Question

In: Computer Science

Translate following function from C into RISC-V: Display Prime Numbers Between Two Intervals #include<stdio.h> int main(){...

Translate following function from C into RISC-V: Display Prime Numbers Between Two Intervals

#include<stdio.h>

int main(){

int low, high, i, flag;

printf("Entertwonumbers(intervals):");

scanf("%d%d",&low,&high);

printf("Primenumbersbetween%dand%dare:",low,high);

//iteration until low is not equal to high

while(low<high)

{

flag=0;

//ignore numbers less than 2

if(low<=1)

{

++low;

continue;

}

//if low is a non-prime number, flag will be 1

for(i=2;i<=low/2;++i)

{

if(low%i==0)

{

flag=1;

break;

}

}

if(flag==0)

printf("%d",low);

//to check prime for the next number

//increase low by 1

++low;

}

return0;

}

Solutions

Expert Solution

C to RISC-V :

.LC0:

        .string "Entertwonumbers(intervals):"

.LC1:

        .string "%d%d"

.LC2:

        .string "Primenumbersbetween%dand%dare:"

.LC3:

        .string "%d"

main:

        addi    sp,sp,-32

        sd      ra,24(sp)

        sd      s0,16(sp)

        addi    s0,sp,32

        lui     a5,%hi(.LC0)

        addi    a0,a5,%lo(.LC0)

        call    printf

        addi    a4,s0,-32

        addi    a5,s0,-28

        mv      a2,a4

        mv      a1,a5

        lui     a5,%hi(.LC1)

        addi    a0,a5,%lo(.LC1)

        call    scanf

        lw      a5,-28(s0)

        lw      a4,-32(s0)

        mv      a2,a4

        mv      a1,a5

        lui     a5,%hi(.LC2)

        addi    a0,a5,%lo(.LC2)

        call    printf

.L9:

        lw      a4,-28(s0)

        lw      a5,-32(s0)

        bge     a4,a5,.L2

        sw      zero,-24(s0)

        lw      a5,-28(s0)

        mv      a4,a5

        li      a5,1

        bgt     a4,a5,.L3

        lw      a5,-28(s0)

        addiw   a5,a5,1

        sext.w  a5,a5

        sw      a5,-28(s0)

        j       .L4

.L3:

        li      a5,2

        sw      a5,-20(s0)

.L7:

        lw      a5,-28(s0)

        srliw   a4,a5,31

        addw    a5,a4,a5

        sraiw   a5,a5,1

        sext.w  a4,a5

        lw      a5,-20(s0)

        sext.w  a5,a5

        bgt     a5,a4,.L5

        lw      a5,-28(s0)

        mv      a4,a5

        lw      a5,-20(s0)

        remw    a5,a4,a5

        sext.w  a5,a5

        bnez    a5,.L6

        li      a5,1

        sw      a5,-24(s0)

        j       .L5

.L6:

        lw      a5,-20(s0)

        addiw   a5,a5,1

        sw      a5,-20(s0)

        j       .L7

.L5:

        lw      a5,-24(s0)

        sext.w  a5,a5

        bnez    a5,.L8

        lw      a5,-28(s0)

        mv      a1,a5

        lui     a5,%hi(.LC3)

        addi    a0,a5,%lo(.LC3)

        call    printf

.L8:

        lw      a5,-28(s0)

        addiw   a5,a5,1

        sext.w  a5,a5

        sw      a5,-28(s0)

.L4:

        j       .L9

.L2:

        li      a5,0

        mv      a0,a5

        ld      ra,24(sp)

        ld      s0,16(sp)

        addi    sp,sp,32

        jr      ra


Related Solutions

Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main()...
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main() { int i = 2, j = 2 + i, k; k = i * j; printf("%d\n", k + j); return 0; } 2) #include <stdio.h> int main() { int i = 2, j = 2 + i, k = j / 2; if (k == 1) { printf("%d\n", j) k = k * 2; if ( k == j) { printf("%d\n|, j); }...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number;...
Translate the following C program to PEP/9 assembly language. #include <stdio.h> Int main (){ int number; Scanf (“%d”, & number); if (number % 2 ==0) { printf (“Even\n”); } else { printf(“Odd\n”); } Return 0; }
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum;...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum; scanf("%d", &numitms); sum=0; for (j=1;j<=numitms;j++) { scanf("%d", &data); sum+=data; } printf("sum: %d\n",sum); return0; } I got an answer with an error. Please debug the answer or have a new correct answer (don't copy and paste others' answer) main: SUBSP 2,i DECI numItms,i DECI j,j DECI data,d DECI sum,s LDWA numItms,i sum: .EQUATE 0 LDWA 1,i STWA j,j for: CPWA numItms, j BRGE endFor STRO...
Can anyone translate this to flowgorith. #include<stdio.h> int main()    {    int seatingChart[10];       //array...
Can anyone translate this to flowgorith. #include<stdio.h> int main()    {    int seatingChart[10];       //array for seating chart       int fc = 0, ec = 5,i;       //starting positions for first class and economy class       printf("Welcome to the Airline Reservation System (ARS)\n\n");       for(i=0;i<10;i++)       //initializing the array with zero        {        seatingChart[i] = 0;        }    char choice;       do        {        int ch;   ...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
Translate the following procedure to RISC-V assembly long long int myfun(long long int a, long long...
Translate the following procedure to RISC-V assembly long long int myfun(long long int a, long long int b) { return fun(fun(a-b), a+b); } Assume that function fun exists and accepts a single long long int argument and returns a long long int argument. Make use of the tail call optimization if possible, and use the least number of stack operations (reading and writing to the stack). You do not need to write comments or a main program. Submit a regular...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
#include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");...
#include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to the program above what...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT