Question

In: Computer Science

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);

}

else

printf("%d\n", k);

return 0;

}

Solutions

Expert Solution

This conversion can be done in several ways and the best mode of converting the C code into RISC-V assembly code is using a gcc compiler and follow this command.Let suppose the name of the c program file is program.c,in ur gcc compiler type

gcc -S program.c

This will generate you program.s file that is your assembly code for the C code.Anyways C language does the same process in the background.Here is the assembly code for C programs.

1)

.LC0:
        .string "%d\n"
main:
        addi    sp,sp,-32
        sd      ra,24(sp)
        sd      s0,16(sp)
        addi    s0,sp,32
        li      a5,2
        sw      a5,-20(s0)
        lw      a5,-20(s0)
        addiw   a5,a5,2
        sw      a5,-24(s0)
        lw      a4,-20(s0)
        lw      a5,-24(s0)
        mulw    a5,a4,a5
        sw      a5,-28(s0)
        lw      a4,-28(s0)
        lw      a5,-24(s0)
        addw    a5,a4,a5
        sext.w  a5,a5
        mv      a1,a5
        lui     a5,%hi(.LC0)
        addi    a0,a5,%lo(.LC0)
        call    printf
        li      a5,0
        mv      a0,a5
        ld      ra,24(sp)
        ld      s0,16(sp)
        addi    sp,sp,32
        jr      ra

2)

.LC0:
        .string "%d\n"
.LC1:
        .string "%d\n|"
main:
        addi    sp,sp,-32
        sd      ra,24(sp)
        sd      s0,16(sp)
        addi    s0,sp,32
        li      a5,2
        sw      a5,-20(s0)
        lw      a5,-20(s0)
        addiw   a5,a5,2
        sw      a5,-24(s0)
        lw      a5,-24(s0)
        srliw   a4,a5,31
        addw    a5,a4,a5
        sraiw   a5,a5,1
        sw      a5,-28(s0)
        lw      a5,-28(s0)
        sext.w  a4,a5
        li      a5,1
        bne     a4,a5,.L2
        lw      a5,-24(s0)
        mv      a1,a5
        lui     a5,%hi(.LC0)
        addi    a0,a5,%lo(.LC0)
        call    printf
        lw      a5,-28(s0)
        slliw   a5,a5,1
        sw      a5,-28(s0)
        lw      a4,-28(s0)
        lw      a5,-24(s0)
        sext.w  a4,a4
        sext.w  a5,a5
        bne     a4,a5,.L3
        lw      a5,-24(s0)
        mv      a1,a5
        lui     a5,%hi(.LC1)
        addi    a0,a5,%lo(.LC1)
        call    printf
        j       .L4
.L3:
        lw      a5,-28(s0)
        mv      a1,a5
        lui     a5,%hi(.LC0)
        addi    a0,a5,%lo(.LC0)
        call    printf
.L4:
        li      a5,0
        j       .L5
.L2:
        li      a5,0
.L5:
        mv      a0,a5
        ld      ra,24(sp)
        ld      s0,16(sp)
        addi    sp,sp,32
        jr      ra

For any queries please comment.


Related Solutions

Convert the following C program into the RISC-V assembly code. You should look up a table...
Convert the following C program into the RISC-V assembly code. You should look up a table to convert the lines manually and you can use "ecall" when appropriate (Don't make it too complicated than it needs to be): #include int main() { int x = 30, y = 17; printf("x * y = "); printf("%d\n", x*y); return 0; }
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; }
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
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; }
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...
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; }
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...
*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...
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...
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative...
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative integer decimal string to an integer. ‘+’ and ‘-’ will appear optionally. And once they appear, they will only appear once in the first byte. If a non-digit character appears in the string, your program should stop and return -1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT