Question: C to RISC - V assembly conversion. Consider each of the following high - level ( C ) code snippets. Convert them to RISC -

C to RISC-V assembly conversion. Consider each of the
following high-level (C) code snippets. Convert them to RISC-V assembly. Translate the
code directly as it is (do not rewrite the high-level code).
For this problem only, do not use the stack you will correct this (i.e., use the stack) in the
next problem.
Function calls:
a) C code:
int main(){
...
int y = subtract(7,3); // y is in s5
...
}
int subtract(int x, int w){
int result; // result is in s7
result = x w;
return result;
}
b) C code:
//(same code as above, except main makes 2 function calls)
int main(){
...
int y = subtract(7,3); // y is in s5
int w = subtract(8,20); // w is in s6
...
}
int subtract(int x, int w){
int result; // result is in s7
result = x w;
return result;
}1.[40 pts total, 20 pts each] C to RISC-V assembly conversion. Consider each of the following high-level (C) code snippets. Convert them to RISC-V assembly. Translate the code directly as it is (do not rewrite the high-level code).
For this problem only, do not use the stack - you will correct this (i.e., use the stack) in the next problem.
Function calls:
a)\(\mathbf{C}\) code:
```
int main(){
...
int y = subtract(7,3); // y is in s5
...
}
int subtract(int x, int w){
int result; // result is in s7
result = x - w;
return result;
}
```
b)\(\mathbf{C}\) code:
```
//(same code as above, except main makes 2 function calls)
int main(){
...
int y = subtract(7,3); // y is in s5
int w = subtract(8,20); // w is in s6
...
}
int subtract(int x, int w){
``````
int result; // result is in s7
result = x - w;
return result;
}
```
2.[40 pts total, 20 pts each] Functions and the stack - saving preserved registers on the stack. Rewrite the RISC-V assembly code from problem 1, but this time use the stack as needed to save and restore registers. This is the correct way to write a function.
3.[20 pts total] Saving preserved and non-preserved registers on the stack. Consider the following two function calls. Notice that func1 uses one of its arguments (b-i.e., a1) after it makes a function call to func2.
a)[\(\mathbf{5}\mathbf{~ p t s ]}\) What value does func1 return when it is called as: func110,20
b)[15 pts] Perform the following steps to convert this high-level code to assembly.
1. Write the code without worrying about the stack or registers that get overwritten.
2. Now, modify the code to use the stack to save/restore registers in the code.
int func1(int a, int b){
int val =7; // val is in s2
if (val a)
val = val + func2(b, a);
val = val - b;
return val;
}
int func2(int x, int w){
int result; // result is in s2
result = x | w;
return result;
}
C to RISC - V assembly conversion. Consider each

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!