Question

In: Computer Science

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.

Solutions

Expert Solution

Answer 1: Translate to a recursive RISC-V procedure.

c language program: ---------------

long long int fun(long long int x, long long int y)

{if (x == 0)

return (y);

else

return(fun(x-1, x+y));}

RISC-V language program: --------------

_PROCEDURE_LINKAGE_TABLE_:

.#..3..A......C.

.....S......g...

fun(long long, long long):

bnez   a0,10580 <fun(long long, long long)+0x6>

mv a0,a1

ret

addi   sp,sp,-16

sd ra,8(sp)

add    a1,a1,a0

addi   a0,a0,-1

jal    ra,1057a <fun(long long, long long)>

ld ra,8(sp)

addi   sp,sp,16

ret

main:

li a0,0

ret

Answer 2:  translate to a non-recursive RISC-V version.

c language program: ---------------

long long int fun(long long int x, long long int y)

{

    while(true){

        if(x == 0)

            return(y);

    

        x = x - 1;

        y = x + y;

    }

}

RISC-V language program: --------------

_PROCEDURE_LINKAGE_TABLE_:

.#..3..A......C.

.....S......g...

fun(long long, long long):

beqz   a0,10582 <fun(long long, long long)+0x8>

addi   a0,a0,-1

add    a1,a1,a0

bnez   a0,1057c <fun(long long, long long)+0x2>

mv a0,a1

ret

main:

li a0,0

ret


Related Solutions

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...
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 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);
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...
2. Let the function fun be defined as int fun(int*k) {       *k += 4;       return...
2. Let the function fun be defined as int fun(int*k) {       *k += 4;       return 3 * (*k) - 1; } Suppose fun is used in a program as follows: void main() {       int i = 10, j = 10, sum1, sum2;       sum1 = (i / 2) + fun(&i);       sum2 = fun(&j) + (j / 2); } What are the values of sum1 and sum2 a. operands in the expressions are evaluated left to right? b. operands...
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); }...
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; }
What does the RISC-V code below do? Write the C equivalent. Assume that i is in...
What does the RISC-V code below do? Write the C equivalent. Assume that i is in register x5 and that the base address of array A that holds doubleword integers is in x20. addi x5, x0, 0 addi x6, x0, 50 addi x28, x20, 0 loop: bge x5, x6, end ld x7, 0(x28) bge x7, x0, next sub x7, x0, x7 sd x7, 0(x28) next: addi x5, x5, 1 addi x28, x28, 8 jal x0, loop end: Can you rewrite...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT