Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
lab3
- Loading branch information
Jerry Shi
committed
Jan 31, 2024
1 parent
adc0ded
commit 9a9d5a7
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Perfect Shuffle | ||
|
||
**Deadline:** Friday, 2/9/2024 | ||
|
||
## Learning Objectives | ||
|
||
Access word arrays in our code, more specifically, read/write word arrays in a | ||
loop. | ||
|
||
## Description | ||
|
||
Perfect shuffle is a method of shuffling cards. It includes two steps. Suppose | ||
there are $n$ cards, and $n$ is even. | ||
|
||
* Divide the cards into halves, left and right. | ||
|
||
* Starting from the left half, take one card from each half alternately until | ||
all cards are taken. | ||
|
||
Let us take $n = 8$ as an example. Supposed the cards are numbered from 0 to 7 | ||
and saved in an word array (of 8 words). The following shows how cards are | ||
drawn from left and right halves and placed in the destination array. In the | ||
original array, numbers 4, 5, 6, and 7 are in the right half. | ||
|
||
``` | ||
The original array: 0 1 2 3 4 5 6 7 | ||
The destination array: 0 4 # one card is taken from each half | ||
The destination array: 0 4 1 5 # two cards are taken from each half | ||
The destination array: 0 4 1 5 2 6 # three cards are taken from each half | ||
The destination array: 0 4 1 5 2 6 3 7 # four cards are taken from each half | ||
``` | ||
|
||
Write RISC-V code to shuffle the words/cards in the array `src` and place the | ||
shuffled words in another word array `dst`. In `lab3.s`, the address of `src` | ||
is in `s1` and the address of `dst` is in `s3`. | ||
|
||
Although `src` has 128 words, we do not need to shuffle all of them. The number | ||
of words to be shuffled is entered by the user. We assume the user always | ||
enters a positive even number that is not larger than 128. | ||
|
||
The program does not print anything to the console. To check the results, we | ||
examine the memory content in the "Data Segment" window. For example, if we | ||
enter 8, we should see `0 4 1 5 2 6 3 7` in `dst`. To find `dst`, make sure we | ||
are viewing ".data" section. We may also need to use the green arrows to adjust | ||
the starting addresses. | ||
|
||
## Deliverables | ||
|
||
Please submit `lab3.s` and take lab3-test in HuskyCT by the deadline. | ||
|
||
Note that the auto grader checks values in some registers and in some memory | ||
areas, in addition to any output. | ||
|
||
## Pseudocode | ||
|
||
``` | ||
// s1 is the starting address of src | ||
// s1 is also the address of the left half | ||
// s3 is the starting address of dst | ||
Place the starting address of the right half in s2 | ||
for (i = 0; i < n/2; i += 1) { | ||
dst[i+i] = left[i] | ||
dst[i+i+1] = right[i] | ||
} | ||
``` | ||
|
||
## Tips | ||
|
||
Contruct a table to keep track of mapping between variable and registers. | ||
Here is an example. | ||
|
||
| Variables | Registers | | ||
| ------------- | --------- | | ||
| src | s1 | | ||
| right | s2 | | ||
| dst | s3 | | ||
| ... | ... | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# CSE 3666 | ||
|
||
.data #data segment | ||
.align 2 | ||
|
||
src: .word | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, | ||
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, | ||
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, | ||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, | ||
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, | ||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, | ||
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, | ||
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, | ||
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, | ||
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, | ||
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, | ||
120, 121, 122, 123, 124, 125, 126, 127 | ||
|
||
dst: .space 1024 | ||
|
||
.text | ||
.globl main | ||
|
||
main: | ||
lui s1, 0x10010 # hard-coded src address | ||
addi s3, s1, 512 # s3 is the destination array | ||
|
||
# read n, the number of words to shuffle | ||
# n is even and 2 <= n <= 128 | ||
addi a7, x0, 5 | ||
ecall | ||
|
||
# n is in a0 | ||
|
||
# TODO: | ||
# write a loop to shuffle n words | ||
# the address of the source array src is in s1 | ||
# the address of the destination array dst is in s3 | ||
# register s2 will store the address of the second half of src | ||
# the folloiwng code can use any t and a registers | ||
|
||
|
||
exit: addi a7, x0, 10 # syscall to exit | ||
ecall |