Question

Consider the C code below which reverses the elements in the array A in place. Both...

Consider the C code below which reverses the elements in the array A in place. Both array indexing and pointer versions are given. Assume that A holds doubleword integers and that size is the number of elements in the array. Translate to RISC-V using as few instructions as possible. For your RISC-V code, assume the base address of A is initially in x10 and that the number of elements in the array (i.e., size) is initially in x11.

long long int temp;

for (i = 0; i < size/2; i++) {

temp = A[i];

A[i] = A[size - i - 1];

A[size - i - 1] = temp;

}

Pointer version:

long long int *p = A;

long long int *q = A + size - 1;

while (p < q) {

temp = *p;

*p = *q;

*q = temp;

p++;

q--;

}

Homework Answers

Answer #1

Greetings!!

Code:

#DATA SEGMENT

.data

array: .dword 0x1111111111111111,0x2222222222222222,0x3333333333333333,0x4444444444444444,0x5555555555555555,0x6666666666666666,0x7777777777777777,0x8888888888888888

#CODE SEGMENT

.text

main:

la a0,array         #loading address of the array

li a1,7                #load length-1

li t0,8                #word size

li t3,1                #constant

mul t1,a1,t0       #calculating the offset of the last element

add t1,a0,t1       #calculating the address of the last element

srl a1,a1,t3         #(length-1)/2 for number of iterations

addi a1,a1,1       #adjusting the number of iterations

loop:

beq a1,zero,end #whether the iteration count is 0?

lw a3,0(a0)        #else load the word from the dword from the starting

lw t2,0(t1)q       #load the word from the dword from the end of array

sw t2,0(a0)                 #store word from the end as word to start

sw a3,0(t1)                 #store word from the start as word to end

addi a0,a0,4       #increment for start index for next word

addi t1,t1,4        #increment for end index for next word

lw a3,0(a0)        #load the 2nd word from dword from start

lw t2,0(t1)         # load the 2nd word from dword from end

sw t2,0(a0)                 #store

sw a3,0(t1)                 #store

addi a1,a1,-1     #reduce loop count

addi a0,a0,4       #increment start index

addi t1,t1,-12     #decrement end index

j loop                 #repeat

end:

li a7,10              #parameter for termination ecal

ecall

Output screenshot:even length array

Before reversing:

After reversing:

Odd length array:

Before:

After:

Hope this helps

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
1. In the following C code, elements of each row are stored contiguously in memory. Assume...
1. In the following C code, elements of each row are stored contiguously in memory. Assume each element in an array is a 32-bit integer. p = 0; for (j=0; j<8; ++j) for(i=500; i>0; --i ) {     A[ i] = 2 * B[j]; ++p; } a. How many 32-bit integers can be stored in a 32-byte cache block? b. Which variable references exhibit temporal locality? c. Which variable references exhibit spatial locality?
Prelab: Week of September 9th You’ve created a general-purpose function for allocating an array with elements...
Prelab: Week of September 9th You’ve created a general-purpose function for allocating an array with elements of any data type: array = createArray(numElems, elemSize); Now we want to generalize the function so that the result secretly stores the size (number of elements) with the array so that we can implement the following function size = getArraySize(array); which allows the user to determine the number of elements in a given array whenever its needed. You’ll want to declare the parameter to...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code without testing it. I will downvote your answer and mark as spam.** I have posted this question several times, do not copy / paste the same code that has been posted, IT DOESN'T WORK!! In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Given the following code: public void expand(Object a) {     // assume element has enough capacity...
Given the following code: public void expand(Object a) {     // assume element has enough capacity     for (int i = size - 1; i >= 0; i--) {         element[4 * i + 3] = a;         element[4 * i + 2] = a;         element[4 * i + 1] = a;         element[4 * i] = element[i];      }      size = 4 * size; } element is a one-dimensional array that stores elements of the type Object....
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using linked lists. Currently I am able to input processes and store / display them properly. The issue begins somewhere after I have displayed the processlist (I get a segmentation fault (core dumped) or the code doesnt seem to run). I have marked the location of where I think the issue begins with a comment. Please fix the code so that it is working properly....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT