In: Computer Science
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.
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