CS 241 — Winter 2018 — Assignment 3

Assignments for CS 241
← Assignment 2 Assignment 3 Assignment 4 →
Monday, Jan 22, 2018 at 9:00 pm Monday, Jan 29, 2018 at 9:00 pm Monday, Feb 5, 2018 at 9:00 pm
P1P2P3P4

Assignments 3 may be done in either Racket or C++14 or Scala. See language-specific notes for each option at the end of this document.

In assignments 3, you will incrementally write an assembler for MIPS assembly language (CS241 dialect).

Note carefully: In order to do assignment 4, you must do assignment 3 first. We will not be distributing a solution to assignment 3 for you to use as a starting point for assignment 4.

Part I. Twice

Problem 1 — Racket/C++/Scala Review (15 marks of 66) (filename: twice.rkt or twice.cc or Twice.scala)

Write a Racket, C++ or Scala program that reads a list of integers from standard input, and prints the list twice, one integer per line. Your program should first print the list in the same order as given in the input; it should then print the numbers again in the same order. Your solution must be reasonably efficient.

Test your program.

Submit a file called twice.rkt, twice.cc, or Twice.scala containing the Racket, C++ or Scala source code for your program.

Click here to return to the top of Assignment 3.

Part II. Writing an Assembler

For the remaining problems in assignment 3 and assignment 4 you will implement an assembler for progressively larger subsets of MIPS assembly language. Subject to the assumptions stated in the problems, your assembler must report all errors, and must correctly translate all correct assembly language programs to MIPS machine language.

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 Part II 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 or .scala file depending on your choice of language.

For each problem, we ask you to implement support for additional instructions. 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 2 and 3, you may have to restructure your code to get a working solution to problem 4. Therefore, you may want to read and understand all of the problems (especially up to and including problem 4) before beginning problem 2. However, if you find this overwhelming, you may find it easier to just focus on solving problems 2 and 3 first, and deal with problem 4 when you come to it. The decision is yours.

Problem 2 — 17 marks of 66 (filename: asm.rkt or asm.cc or Asm.scala)

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, even if the input is not a valid assembly language program. Your assembler should not silently ignore errors in the input program. If the input contains a line that is not valid in MIPS assembly language, your assembler should print an appropriate error 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.

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.

If the input contains a correct MIPS assembly language program, your assembler should output the equivalent MIPS machine language to standard output.

Click here to return to the top of Assignment 3.

Problem 3 — 17 marks of 66 (filename: asm.rkt or asm.cc or Asm.scala)

Add support for label definitions to your assembler. Other than the inclusion of label definitions, the restrictions, assumptions and output requirements (including error-checking) stated in problem 2 apply to problem 3.

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.

In handling labels, you may use any data structure or data structures you choose, but be sure to take efficiency into account.

Click here to return to the top of Assignment 3.

Problem 4 — 17 marks of 66 (filename: asm.rkt or asm.cc or Asm.scala)

Modify your assembler to allow labels to be defined and also to be used as operands.

Other than the inclusion of label definitions and labels as operands, the restrictions, assumptions, and output requirements (including error-checking) stated in problem 2 apply to problem 4. (Note that you need not list the names and values of defined labels as in problem 3.)

Click here to return to the top of Assignment 3.

Language-Specific Details

Racket

The provided starter asm.rkt 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

Scala

The provided starter Asm_scala.zip has a Seq[Seq[Token]] called Asm.tokenLines which contains the all the tokens in the assembly program. You can see an example of its use in Asm.assemble.

When submitting to Marmoset, you should zip all of your .scala files into a .zip file and submit that to Marmoset.

The Using Scala in CS 241 document contains hints and techniques for using Scala to write the assembler.

Compile the starter code in Scala using the command scalac Asm.scala Scanning.scala. The command will create a number of .class files. You can then run the code via the command scala Asm from the current directory.

Scala has an extremely large number of members for each class in the standard library. You may find it helpful to use an IDE such as IntelliJ IDEA with autocompletion.

C++

The provided starter asm_cpp.zip has a method called assemble that is called with a vector of lines containing tokens.

When submitting to Marmoset, if you have chosen C++, you will need to add all of your files to a .zip (or similar archive) and submit that to Marmoset.

The STL Quick Reference for CS 241 document outlines the parts of the STL most likely to be of use in CS 241.

You are strongly advised to check for pointer-related errors by vetting your programs with valgrind. To do this, run "valgrind program optionsAndArguments" instead of just "program optionsAndArguments" on a Linux computer — valgrind is unreliable or unavailable on other environments. Marmoset will run your submissions with valgrind as well, and will reject any submission that is reported to leak memory. Be aware that running valgrind increases the execution time of your program by a factor of 5 to 20.

Compile a program in C++ using the command "g++ -std=c++14 -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