Question

In: Computer Science

The C function funsum below takes three arguments -- an integer val and two function pointers...

The C function funsum below takes three arguments -- an integer val and two function pointers to functions f and g, respectively. f and g both take an integer argument and return an integer result. The function funsum applies f to val, applies g to val, and returns the sum of the two results. Translate to RISC-V following RISC-V register conventions. Note that you do not know, nor do you need to know, what f and g do, nor do you know what registers f and g use.

long long int funsum(long long int val, long long int (*f) (long long int), long long int (*g) (long long int))

{

return(f(val) + g(val));

}

Solutions

Expert Solution

Answer : Given data

An integer val and two function pointers to functions f and g, respectively.

Translation to RISC-V :

funsum:

        addi    sp,sp,-64

        sd      ra,56(sp)

        sd      s0,48(sp)

        sd      s1,40(sp)

        addi    s0,sp,64

        sd      a0,-40(s0)

        sd      a1,-48(s0)

        sd      a2,-56(s0)

        ld      a5,-48(s0)

        ld      a0,-40(s0)

        jalr    a5

        mv      s1,a0

        ld      a5,-56(s0)

        ld      a0,-40(s0)

        jalr    a5

        mv      a5,a0

        add     a5,s1,a5

        mv      a0,a5

        ld      ra,56(sp)

        ld      s0,48(sp)

        ld      s1,40(sp)

        addi    sp,sp,64

        jr      ra

______________THE END___________________


Related Solutions

The C function funsum below takes three arguments -- an integer val and two function pointers...
The C function funsum below takes three arguments -- an integer val and two function pointers to functions f and g, respectively. f and g both take an integer argument and return an integer result. The function funsum applies f to val, applies g to val, and returns the sum of the two results. Translate to RISC-V following RISC-V register conventions. Note that you do not know, nor do you need to know, what f and g do, nor do...
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1 small, 2 medium, 3 large), one for the number of meat toppings, one for the number of other toppings and another for the quantity of pizzas ordered. It should calculate and return the total due for that pizza order based on the following information: Small pizza base price: $6.50 Medium pizza base price: $9.50 Large pizza base price: $11.50 The base pizza price includes...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
R programming language A) Create a function that accepts two arguments, an integer and a vector...
R programming language A) Create a function that accepts two arguments, an integer and a vector of integers. It returns the count of the number of occurrences of the integer in the input vector. 1]Input: num_count <-function ??? 2]Input: num_count(2,c(1,1,2,2,3,3)) 2] Output: 2 3] Input: num_count(1,c(1,1,2,2,3,1,4,5,5,2,2,1,3)) 3] Output : 4 B) Create a function that accepts 3 integer values and returns their sum. However, if an integer value is evenly divisible by 3, then it does not count towards the...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT