CSCI 435: Compiler Construction
Assignment 2

Overview

Modify the author's Tiny Machine (TM) interpreter contained in tm.c. Add support for the two instructions described below. Once you implement the instructions, write a TM program that inputs two integers, "a" and "b", and computes and outputs a % b, 2b, and 230. Use ONLY SHL instructions to compute the powers of 2. COMMENT each instruction of your TM program.

Once you're familiar with the interpreter and its source code, add support for the two instructions below. Carefully COMMENT your changes, using a comment of the form "/* MODIFIED: ... */" as close to the change as possible. When I grade I will literally search for the string "MODIFIED" to see your changes.

Next, fix the interpreter code so that it compiles WITHOUT warnings, but ensure you don't change the meaning of the code. Input commands like "i" and "d" should work as they did before — CAREFULLY TEST THIS!

Lastly, answer the questions below.

Input/Output Specification

Input two integers "a" and "b".

Output a % b, 2b, and 230 IN ORDER (see the session below). If the input causes an error, print an appropriate error message (see the precise messages to use above). An error should cause the program to halt.

Sample TM Session

TM  simulation (enter h for help)...
Enter command: go
Enter value for IN instruction: 17
Enter value for IN instruction: 5
OUT instruction prints: 2
OUT instruction prints: 32
OUT instruction prints: 1073741824
HALT: 0,0,0
Halted
Enter command: clear
Enter command: go
Enter value for IN instruction: 17
Enter value for IN instruction: 0
Modulus non-positive
Enter command: quit
Simulation done.

Questions

Answer these questions in a PLAIN TEXT file named "README.txt". Include the QUESTIONS in the file as well. Use whitespace for readability, and keep lines to AT MOST 80 characters.

  1. Give several examples of tokens and lexemes in the TM language. Discuss some of the lexical conventions of TM (e.g., comments and whitespace).

  2. Discuss the grammar of the TM language. What does a program consist of? What are valid statement/instruction formats?

  3. Discuss some of the semantic analysis that the TM interpreter performs.

Submission

Submit your modified TM interpreter ("tm.c"), your TM program (use a ".tm" extension), and "README.txt".

Hints

Copy "tm.c" and fact.tm into a directory. Compile "tm.c" with "gcc", without modifying the source code yet. Execute the Tiny Machine interpreter and pass "fact.tm" as a command-line argument. The interpreter will load the instructions from "fact.tm" into its instruction memory. Experiment with running the program and using the commands described by the "help" command.

You can learn more about the TM virtual machine by reading section 8.7 in the text.


Gary M. Zoppetti, Ph.D.