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
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++)...
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...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program that displays all the prime numbers in the given array with the following constraint. Constraint: Only those prime numbers should be displayed whose location is a composite number. Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0....
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...