In: Computer Science
Solve this comp architecture problem:
Supposed you have an array int* arr = {3,7,6,4,5}. The values in arr store in the memory of 0(x21), 8(x21), .... , 32(x21). Transform the following RISC-V code into a C program.
ld x5, 16(x21)
addi x6, x0, 3
sll x5, x5, x6
sd x5, 8(x21)
I have adde the conerted code along with the comments at each line and printing the final output just to show you and please take it as c program and also find the attached screenshot of the execution
#include <stdio.h>
int main(void) {
//initializing the array with the given values by you
int* arr[] = {3,7,6,4,5};
//now loading the value of ld x5, 16(x21)
int x5 = arr[2];
//addi x6, x0, 3 i.e adding the int 3 with 0 and storing in variable in x6
int x6 = 0 + 3;
// sll x5, x5, x6 , now left shifting the value stored in x5 to 3 places which is tored in x6
x5 = x5 << x6 ;
//sd x5, 8(x21) now storing the value at arr[1] which is at 2nd position
arr[1] = x5;
//below line is not for the conversion i have added for your reference
printf("%d", arr[1]);
return 0;
}
Screenshot: