Homework 4 - Working with a 2D array
As you are aware, we have lab assignments due every week. We also will
have several homework assignments this semester, and this is one of them.
While the labs are directed, the homework assignments present larger problems
that you should break down into smaller problems. You should spend some
time thinking about and planning your homework solutions before coding
them.
Program Description
You are to create an assembly language program for RISC-V.
We have seen how to work with arrays as well as input and output in assembly.
Refer to the recent labs as needed.
In this assignment, we will work with 2D data.
Higher level programming languages store 2D data as 1D data, with the added
convenience of allowing the programmer to select a value using 2 indices.
Thus, an 8x8 array of values in C could be accessed as array[r][c]
with index r
for the row and c
for the column.
But it's also possible to access the value as array[r*MAXCOL + c]
,
where MAXCOL
is the maximum column, as the name implies.
In assembly language, we do not have the option of accessing an array
the first way (unless you create way to do this, such as with a macro).
We will use the second way of accessing array data for this homework.
For this assignment, the data will be an 8x8 array of characters (bytes).
This is an example array that we will use.
chess_board: .byte
'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R',
'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
'-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '-', '-', '-', '-', '-', '-',
'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p',
'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'
As you might guess, this is a representation of a chess board, with the
pieces in their starting positions.
Part 1 - Print the chess board
Create a function (subroutine) to print the data at "chess_board".
Use a loop to do this, print one character at a time,
and print a new-line character after each row.
Part 2 - Convert a string to an offset
Given a string such as "a1", compute the offset into the array.
(You can hard-code the string into the data section.)
Print the string, then a space.
Next, print the offset into the array, followed by a space.
Then print the character of the piece that is there, followed by a new-line
character.
Bonus Part 3 - Make a move
Given a string such as "a1b2",
move the piece from square "a1" and put it
on square "b2".
On the first square, put a symbol indicating that it is empty.
Show the initial board, and display it again after the move.
Bonus Part 4 - Make moves based on input
Read the input from the command line, and make the corresponding move.
First, get this to work with one move only.
Then scale your solution to work for as many inputs as there are in the
input stream. This should work using input redirection and piping.
Show the initial board, and display it again after each move.
Assumptions
- Assume that the moves are valid
- Assume that a move is always in the form: letter digit letter digit.
For example, "a1b2".
This is called "Algebraic Notation".
- Assume that the letters are always lower-case and between a and h
- Assume that the digits are always between 1 and 8
- Assume that the pieces start on their initial squares
Notes
- Remember that arrays in assembly language start with an offset of 0
- "a1b2" means to move the piece at square a1 to square b2
- a1 means column a and row 1, corresponding to the upper left corner
If you play chess, you might realize that this is different
from the way this is normally done. This is to make things simpler.
Here is an example input file.
e2e4
e7e5
g1f3
d7d5
Make sure that your program works for other inputs, that is,
use different inputs to test your program.
More Notes:
-
Make one large program that does this.
Use subroutines to make this easier.
- You may be asked to meet with your TA to demonstrate your solution.
- Follow the instructions from previous homeworks/labs
for turning in your work.
-
As you are aware certain types of assistance are inappropriate in
this class. Read the collaboration policy given to you at the beginning
of the semester, if you have any questions.
You do not have to do all 4 parts
You might find that this assignment is more difficult than you expect.
Here is how we will grade this.
- Part 1 counts for 80/100 of this homework.
If you only do part 1, you can still get 80%.
- Part 2 counts for another 20/100 points
- You may have noticed that 80 + 20 = 100. Yes, this is correct that
it is possible to get 100% on this homework by completing only
parts 1 and 2. You only need to do parts 1 and 2.
- Bonus parts 3 and 4 will count toward replacing a lab score.
If a student completes parts 3 and 4, perfectly, they would then have a 100 to
replace one of their lab scores. If they do part 3 perfectly but do not do part 4,
they would then have a 50 to replace one of their lab scores.