Maximum
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.
Below, we give you a C function that finds the largest value in an integer array.
An array is a collection of elements (such as bytes, words, double words, etc.) that are evenly spaced in memory.
As a result, the location of the nth element is always array starting address + (constant * n). The constant is determined by the amount of space required to store each array element in memory. In RISC-V, memory is addressed in bytes (8-bit chunks). An integer is 32-bits wide, or 4 bytes. This means that consecutive array elements in an integer array are 4 bytes apart. Thus, the nth element in the array is located at array starting address + (4 * n).
The maximum algorithm below has two starting values:
pthe pointer to the array (or the starting address of the array);- and
n, which is the number of elements in the array.
int *p; //assume points to array
int n; //assume contains length of array
int largest_so_far = 0; //assume maximum is non-negative
for (int i = 0; i < n; i++) {
int w = p[i];
if (w > largest_so_far) {
largest_so_far = w;
}
}
Your task is to translate this function into assembly. Assume the value of p is stored in register a0 and n in a1 when your code executes. Your answer (corresponding to largest_so_far) should be in register a7 when your code stops executing. You may use registers a0-a7 in your implementation.