, ,

[SOLVED] Cse3666 homework 2

$25

File Name: Cse3666_homework_2.zip
File Size: 169.56 KB

Categories: , , Tags: , ,
5/5 - (1 vote)

1. Suppose A and B are word arrays. The following C loop increments elements in A by 4 and
saves the results into B.
for (i = 0; i < 100; i += 1)
B[i] = A[i] + 4;
The following table shows the mapping between variables and registers.
Register s1 s2 s3
Variable/value i Address of A Address of B
We will study two implementations in RISC-V.
a) The first implementation is based on the array copy code we discussed in lecture. We just
need to revise it slightly. What changes do we need? How many instructions will be
executed for the loop? Note that we do not need to jump to the condition test before the
first iteration because we are sure the condition is true at the beginning.
b) Loop unrolling is an optimization technique to improve the performance of programs. In
the second implementation, we unroll the loop and process four array elements in A in
each iteration. The unrolled loop in C is shown below. Translate the loop to RISC-V
instructions. Try to minimize the number of instructions that are executed. Explain your
code. How many instructions will be executed for the new loop?
for (i = 0; i < 100; i += 4) {
B[i] = A[i] + 4;
B[i+1] = A[i+1] + 4;
B[i+2] = A[i+2] + 4;
B[i+3] = A[i+3] + 4;
}
CSE3666
2. A two-dimensional array in C (and some other languages) can be considered as an array of
one-dimensional array. For example, the following define T as an 16×8 array in C.
int T[16][8];
The two-dimensional array can be considered as an array of 16 elements, each of which is a
one-dimensional array of 8 integers/words. The words are stored in memory in the following
order:
T[0][0], T[0][1], …, T[0][6], T[0][7],
T[1][0], T[1][1], …, T[1][6], T[1][7],

T[14][0], T[14][1], …, T[14][6], T[14][7],
T[15][0], T[15][1], …, T[15][6], T[15][7]
Row 0, consisting of T[0][0], T[0][1],…, and T[0][7], goes first. Row i is stored right after
row i – 1, for i = 1, 2, …, 15. For example, T[1][0] is stored right after T[0][7]. If T[0][0] is
located at address 1000, T[0][7] is located at address 1028 = 1000 + 7 * 4. And T[1][0] is
located at address 1032. Similarly, we can calculate that T[2][0] is located at 1064, T[3][0] is
located at 1096, and so on.
Translate the following C code to RISC-V instructions. Assume T’s address is already in s9.
As a practice of accessing two-dimensional arrays, do not use pointers. Explain your code,
especially how you implement the loops and how you calculate T[i][j]’s address.
for (i = 0; i < 16; i += 1)
for (j = 0; j < 8; j += 1)
T[i][j] = 256 * i + j;

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] Cse3666 homework 2[SOLVED] Cse3666 homework 2
$25