Question

In: Computer Science

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 file called myfun.asm. You do not need to run it on the simulator.

Solutions

Expert Solution

Hi, I guess there is bit wrong in quesion. If fun only accepts 1 arguement then in return statement outer fun function is accepting two arguements. So,I feel it would myfun over there I corrected that as return myfun(fun(a-b), a+b);

And, gave the code accordingly otherwise there would be compilation error

RISC Assembly Program :-

myfun:

        addi    sp,sp,-32

        sd      ra,24(sp)

        sd      s0,16(sp)

        addi    s0,sp,32

        sd      a0,-24(s0)

        sd      a1,-32(s0)

        ld      a4,-24(s0)

        ld      a5,-32(s0)

        sub     a5,a4,a5

        mv      a0,a5

        call    fun

        mv      a3,a0

        ld      a4,-24(s0)

        ld      a5,-32(s0)

        add     a5,a4,a5

        mv      a1,a5

        mv      a0,a3

        call    myfun

        mv      a5,a0

        mv      a0,a5

        ld      ra,24(sp)

        ld      s0,16(sp)

        addi    sp,sp,32

        jr      ra


Related Solutions

Translate the recursive C function below to a recursive RISC-V procedure. long long int fun(long long...
Translate the recursive C function below to a recursive RISC-V procedure. long long int fun(long long int x, long long int y) {if (x == 0) return (y); else return(fun(x-1, x+y));} Write a non-recursive C version of the function from the previous question by eliminating the tail recursion using a goto statement. Then translate to a non-recursive RISC-V version.
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 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 code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
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 following C# code into MIPS assembly language int recursive(int x) { return (x >= 0xFF)...
Translate following C# code into MIPS assembly language int recursive(int x) { return (x >= 0xFF) ? (recursive(x - 3) + recursive(x - 2)) : ((x >= 0xF) ? recursive(x - 1) + 1 : 1);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT