辅导案例-CS 241-Assignment 3

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
2020/9/28 CS 241 — Fall 2020 — Assignments 3
https://www.student.cs.uwaterloo.ca/~cs241/a3/ 1/6
CS 241 — Fall 2020 — Assignment 3
Assignments for CS 241
← Assignment 2 Assignment 3 Assignment 4 →
Friday, September 25th at 5:00
pm
Friday, October 9th at 5:00 pm
Friday, October 23rd at 5:00
pm
P1 • P2 • P3 • P4 • P5 • P6 • P7 • P8 • P9 • P10 • Comprehensive Test
In Assignment 3, you will incrementally write an assembler for MIPS assembly language (CS241
dialect).
Reminder: For this and future assignments, be sure to run the command source /u/cs241/setup
to gain access to the CS 241 tools.
What Is An Assembler?
So far in the course, you have used various tools like cs241.wordasm, cs241.binasm, mips.twoints
and mips.array.
The tools cs241.wordasm and cs241.binasm are MIPS assemblers. They convert ASCII text
commands into MIPS machine language that can be executed by the MIPS emulators, which are
mips.twoints and mips.array.
This is important to understand: an assembler does not execute code. If the user writes
something like div $0, $0, this will obviously cause an error if it is executed. However, the
assembler's job is just to translate this instruction into machine language. When the emulator
executes it, the error will happen, but you are not writing an emulator, just an assembler.
The cs241.wordasm assembler you used on Assignment 1 was a primitive assembler that only
supports the .word directive and nothing else. The cs241.binasm assembler you used on
Assignment 2 supports all features of MIPS assembly language. Your task on Assignment 3 is
essentially to write your own version of cs241.binasm.
This means that, for valid assembly programs, you can check that your output is correct by
comparing it to cs241.binasm using the diff command:
./my-assembler < input.asm > my-output.mips
cs241.binasm < input.asm > expected-output.mips
diff my-output.mips expected-output.mips
If the files are the same, diff will produce no output, and this means your output is correct.
Otherwise it will probably say something like Binary files my-output.mips and expected-
output.mips differ.
For invalid assembly programs, the only requirement is that your assembler produce an error
message containing the string ERROR to standard error. The error message does not need to
2020/9/28 CS 241 — Fall 2020 — Assignments 3
https://www.student.cs.uwaterloo.ca/~cs241/a3/ 2/6
match the one produced by cs241.binasm.
Marmoset Notes
For each question, Marmoset has public and release tests, and often has secret tests as well.
When you release test a submission, Marmoset will only report on public and release tests. No
information about secret tests will be visible until after the assignment deadline.
If you are using C++ for this assignment, Marmoset will run your programs with Valgrind, a tool
that checks for memory-related errors. If Valgrind detects an error in your program, you will fail
the Marmoset tests. A guide for debugging Valgrind errors is available.
Marmoset is not yet available for this assignment. An announcement will be made on Piazza
when the Marmoset tests are available. The mark weightings for the problems will be made
available at the same time as the Marmoset tests.
Writing An Assembler
In the following problems, you will implement an assembler for progressively larger subsets of
MIPS assembly language.
Be sure to read the MIPS Assembly Language Specification. It should answer most questions
you have about what is considered valid MIPS assembly language syntax.
The problems may be done in either Racket or C++14. See language-specific notes for each
option at the end of this document.
We have provided a scanner (also called a tokenizer) for MIPS assembly language for each
available language option (see language-specific notes). You should use this scanner as a
starting point for your assembler.
Each problem in requires you to submit a program that reads from standard input and writes to
standard output as well as standard error. The input and output specifications are identical
regardless of which language you choose. The only difference is that you must submit the
appropriate .rkt or .cc file depending on your choice of language.
For each problem, we ask you to implement support for additional instructions or features. You
may submit the same assembler for all the problems. We encourage you to submit to Marmoset
early. As soon as you implement support for the instructions specified by a problem, submit the
current version of your assembler to Marmoset. That way, if you do not complete all of the
problems before the deadline, you will still get credit for those that you did complete.
Hint: Depending on the design decisions you make in your solutions to Problems 1 and 2, you
may have to restructure your code to get a working solution to Problem 3. You may have to do
further restructuring for Problem 4 and onwards. Therefore, you may want to read and
2020/9/28 CS 241 — Fall 2020 — Assignments 3
https://www.student.cs.uwaterloo.ca/~cs241/a3/ 3/6
understand all the problems before beginning Problem 1. However, if you find this overwhelming,
you may find it easier to just focus on the problems in order. The decision is yours.
Problem 1 (filename: asm.rkt or asm.cc)
Begin by writing an assembler that correctly translates input containing no labels and no
instructions other than .word. You may assume that the input to your assembler contains no
labels and no instructions other than .word.
Your assembler should never crash or leak memory, even if the input is not a valid assembly
language program; it should produce an error message and return gracefully if the input is
invalid. If the input is not a valid MIPS assembly language program, your assembler should print a
message containing the word ERROR in all capitals to standard error and stop. It is good
practice, but not a requirement, to embed ERROR within a meaningful error message.
If the input contains a correct MIPS assembly language program, your assembler should output
the equivalent MIPS machine language to standard output.
The error checking and output requirements above apply to this and all future problems on the
assignment.
Hint: there are relatively few ways in which an assembly language program can be valid (and all
the valid forms are spelled out here), but many ways in which it can be invalid. You will find it
much easier to write code that looks for valid input and rejects everything unexpected, rather
than code that explicitly looks for all the different ways in which the input could be invalid.
Problem 2 (filename: asm.rkt or asm.cc)
Add support for label definitions to your assembler.
In addition, if the input is a correct MIPS assembly program, your assembler should output a
symbol table: a listing of the names and values of all defined labels to standard error. The list
should be printed on several lines, one line for each label in the input. Each line should consist of
the label (without the trailing colon), followed by a space, followed by the value of the label (in
decimal). The labels may appear in the symbol table in any order. For example, the following
input:
begin: .word 2
middle: .word 0
.word 0
end:
Should print the following to stderr (but possibly with the lines reordered):
begin 0
middle 4
end 12
2020/9/28 CS 241 — Fall 2020 — Assignments 3
https://www.student.cs.uwaterloo.ca/~cs241/a3/ 4/6
In handling labels, you may use any data structure or data structures you choose, but be sure to
take efficiency into account.
Problem 3 (filename: asm.rkt or asm.cc)
Modify your assembler to allow labels to be defined and also to be used as operands.
Henceforth, you no longer need to output a symbol table as in Problem 2 (although you will not
be penalized if you do so).
Problem 4 (filename: asm.rkt or asm.cc)
Modify your assembler to correctly handle jr and jalr instructions.
Problem 5 (filename: asm.rkt or asm.cc)
Modify your assembler to correctly handle add, sub, slt, and sltu instructions.
Problem 6 (filename: asm.rkt or asm.cc)
Modify your assembler to correctly handle beq and bne instructions with an integer or hex
constant as the branch offset.
Problem 7 (filename: asm.rkt or asm.cc)
Modify your assembler to correctly handle beq and bne instructions with a label as the branch
target operand.
Problem 8 (filename: asm.rkt or asm.cc)
Modify your assembler to correctly handle the lis, mflo, and mfhi instructions.
Problem 9 (filename: asm.rkt or asm.cc)
Modify your assembler to correctly handle the mult, multu, div, and divu instructions.
Problem 10 (filename: asm.rkt or asm.cc)
Modify your assembler to correctly handle the sw and lw instructions.
Comprehensive Test (filename: asm.rkt or asm.cc)
For this problem, you are not required to implement any new features. At this point, your
assembler should correctly translate all valid MIPS assembly language programs, and write
ERROR to standard error for any input that is not a valid MIPS assembly language program.
2020/9/28 CS 241 — Fall 2020 — Assignments 3
https://www.student.cs.uwaterloo.ca/~cs241/a3/ 5/6
When you are confident that your assembler is complete and correct, submit your assembler to
the project A3Comprehensive on Marmoset. Marmoset will test it thoroughly with many large
and complex programs, both valid and invalid.
This comprehensive test involves large programs which may have as many as 100,000 lines. If
your assembler is inefficient (runs in quadratic time or worse in terms of the input size) it will
likely exceed Marmoset's time limit and fail some of the test cases.
Disable debugging output from your assembler before submitting to this problem. If you
produce too much debugging output and exceed Marmoset's file size limit, you will automatically
fail the tests.
After you submit to the comprehensive test, you may wish to re-submit your assembler to
Problems 1 through 10 to ensure that these problems have the most up-to-date version of your
code. Your Marmoset mark is based on your best on-time submission, so you will not be
penalized for extra submissions to these problems.
Click here to return to the top of Assignment 3.
Language-Specific Details
Racket
The provided starter asm_rkt.zip has a function called scan that takes as input a string and
returns a list of tokens.
The Using Racket in CS 241 document contains hints and techniques for using Racket to write
the assembler. See also the comments in the provided scanner.
Run a Racket program using the command: racket asm.rkt
Click here to return to the top of the page.
C++
The provided starter asm_cpp.zip has a method called scan that takes as input a string and
returns a vector of tokens.
When submitting to Marmoset, if you have chosen C++, you will need to add all your files to a
.zip file and submit that to Marmoset. The top level directory of your .zip file must contain the
asm.cc file. For example, if your zip file contains a directory called a3 and the asm.cc file is stored
under this directory, Marmoset will not be able to find the file.
The STL Quick Reference for CS 241 document outlines the parts of the STL most likely to be of
use in CS 241.
2020/9/28 CS 241 — Fall 2020 — Assignments 3
https://www.student.cs.uwaterloo.ca/~cs241/a3/ 6/6
You are strongly advised to check for memory-related errors by vetting your programs with
Valgrind. To do this, run "valgrind program optionsAndArguments" instead of just "program
optionsAndArguments". Marmoset will run your submissions with Valgrind as well, and will
reject any submission that contains memory-related errors. Be aware that running Valgrind
increases the execution time of your program by a factor of 5 to 20.
See the following page on Debugging Valgrind Errors for a discussion of common Valgrind errors
and how to resolve them.
Compile a program in C++ using the command "g++-6 -g -std=c++14 -lm -Wl,--warn-common,--
fatal-warnings -o asm asm.cc scanner.cc".
This command will create a file called asm containing the compiled code.
Run the program using the command: ./asm
Or use valgrind ./asm to run with Valgrind.
Click here to return to the top of the page.

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468