Question: Converting the C program to RISC-V assembly and simulate its execution using RARS. To use array in assembly code, your code needs to reserve space
Converting the C program to RISC-V assembly and simulate its execution using RARS. To use array in assembly code, your code needs to reserve space in the data section. Check the memory.s file in RARS repo (https://github.com/TheThirdOne/rars/blob/master/test/memory.s) for using .space to reserve memory for an array identified by a symbol, and how to use la instruction to load the memory address (first element of the array) to a register.
#include
#include
int main(void){ srand(time(0));
int i; int sum = 0; int num[100]; //declare an int array of 100 elements int average=0; //and use a for loop to generate 100 integers and store them in the array for(i = 0; i <=99; i++){ num[i] = rand() % 100 + 1; } //use another for loop to accumulate those numbers by reading them from the array and adding up to a variable for(i=0; i<=99; i++) { sum+= num[i]; } //) calculate the average by dividing the accumulated sum with 100 average = (sum/100); printf( "sum: %d ", sum); //print the average and return the average printf( "average: %d ", average); return average; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
