Calling Convention 2

The questions below are due on Sunday November 01, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Back to Exercises

RISC-V Calling Convention 2

Consider the following C function, f:
int f(int x, int y) {
    int w = y;
    int z = mul(x, 2) + y + w;
    return z;
}

Suppose that you wanted to implement the function, f, in RISC-V assembly.

Which register(s) should be used to pass the arguments to the function f? Select all correct answers.

When the function returns, which register(s) should be set to the returned value? Select all correct answers.

Consider the following incorrect RISC-V implementation of the function f.

Assume that its calling function believes that f is properly implemented and follows the RISC-V calling conventions. Also assume that the function mul, being called by f, is implemented correctly and follows the RISC-V calling convention. The function mul takes two unsigned integer arguments and returns the result of multiplying those inputs.

f:
	// (1)	
	mv a2, a1
	mv s0, a1
	li a1, 2
	// (2)
	call mul
	// (3)
	add a0, a0, a2
	add a0, a0, s0
	// (4)	
	ret
	// (5)		

Assume f is called with arguments (4 and 6), what will be the value in a0, a1, and a2 when execution reaches label (2) in the code?

Given that mul follows the calling convention, which of the following register(s) are guaranteed to hold the same value at both points (2) and (3) in the code execution? Select all correct answers.

Given that mul follows the calling convention, but f does not, which register(s) are not handled properly by the code above? Select all correct answers

If arguments of the function f are 6 and 7 respectively, which of the following statement(s) are true? Select all correct answers.

Using the Stack

We now want to modify the implementation of function f so that it follows the calling convention and works correctly. To do this we will make use of the stack and additional instructions that save and restore values from the stack.

The first instruction we need to add is an instruction that will allocate enough space on the stack to hold all the registers that we need to save.

Which of the following instructions should be added as the first line of function f?

Which of the following instruction(s) should be added to either points (1) or (2) in the code of function f? Select all missing instructions that would work correctly regardless of being placed at point (1) or point (2).

Which of the following instruction(s) must be added at point (1) in the code of function f? Select all correct answers.

Which of the following instruction(s) must be added at point (2) in the code of function f? Select all correct answers.

Which of the following instruction(s) can be added at either point (3) or (4) in the code of function f? Select all missing instructions that would work correctly regardless of being placed at point (3) or point (4).

Which of the following instruction(s) must be added at point (3) in the code of function f? Select all correct answers.

Which of the following instruction(s) must be added at point (4) in the code of function f? Select all correct answers.

In which of the following location(s) in the code, could the instruction that restores the stack pointer, sp, to its original value be placed assuming that all registers that must be restored are restored prior to this instruction? Select all correct answers.

Back to Exercises