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

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...
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; }
1. For the following C statement, write the corresponding RISC-V assembly code. Assume that the C...
1. For the following C statement, write the corresponding RISC-V assembly code. Assume that the C variables a, b, and c, have already been placed in registers x10, x11, and x12 respectively. Use a minimal number of RISC-V assembly instructions. a = b + (c − 2); 2. Write a single C statement that corresponds to the two RISC-V assembly instructions below. add e, f, g add e, h, e 3. Assume that registers x5 and x6 hold the values...
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; }
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.
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly...
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What is the RISC-V instruction format and specific assembly language instruction? 0x00156A33
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0) return 0; else if (n <= 2) return 1; else { temp = fib(n – 1); return temp + fib(n-2); } } int main() { int num; cout << "Which fibonacci number? "; cin >> num; cout << fib(num) << endl; return 0; } You must use equates to access the stack and follow the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT