In: Computer Science
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));
}
Given below is the 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
Thank You.