CS 70

Compiler

What You’re Given…

You have (edited) C++ code from the preprocessor.

What You’ll Do…

As the compiler, your job is to translate the given C++ code to the “CS 70 Classroom Language”. This simple language encodes instructions for the Prof to do various things with the props in the classroom!

In your group's directory, make a file called assembly-01.txt (or -02.txt, etc. for subsequent attempts).

Feel free to exercise your creativity in deciding how to do the translation! Here are the valid operations of the “CS 70 Classroom Language” that you can use (Do not include the parentheses in your final output, those are just to indicate placeholders you need to fill in). Hopefully the command names are self-explanatory, but if you need clarification on what a command does, feel free to ask the prof!

  • put (value) in (boxN)
  • add (boxN) to (boxM)
  • Note: for this and the other “arithmetic” commands, the result will be stored back into boxM
  • subtract (boxN) from (boxM)
  • multiply (boxM) by (boxN)
  • divide (boxM) by (boxN)
  • call (function name) on (boxN, … , boxM)
    • Note: function name must be a name you have seen somewhere in the C++ code, but it’s okay if you don’t know how the function is defined. Also, note that function name also refers to the parameter types of the function! (For example, void foo(int x) is different from void foo())
    • The (boxN, … , boxM) refers to function parameters. So a function that takes one parameter should have one box in the list, a function that takes two parameters should have two boxes, and so on.

Using some combination of these commands (again, use your creativity!), you must go through the C++ code line by line and write down a sequence of commands in the “CS 70 Classroom Language” that corresponds to the C++ code.

Possible errors

If you encounter a line that you don’t know how to translate (for example, a call to a function you don’t recognize), you must immediately stop translating and submit “Compiler error”. The process will start over and you will receive a new piece of code from the preprocessor.

Hint

Did you notice that none of the “CS 70 Classroom Language” commands involve variable names? That’s on purpose! Your job is just to produce a series of commands that does the same thing as the original C++ code, even though it will not be a direct 1-to-1 translation! It’s your job to decide how you want to keep track of variables, but one helpful bit of advice is to think back to how things are labeled in a memory diagram.

When You've Finished

  • If you successfully created the object file, you can move on to the next stage, assembly.
  • If however there was an error, you need to fix just that error in the source file and create a new copy of that source file in your group directory, number that too, so it becomes prog-01.cpp (or whatever the next number is).

(When logged in, completion status appears here.)