Hand Translations at Level C


Objectives

The primary objective of this lab is to familiarize you with the process of translation.  To this end you will be required to do some hand translations of mPascal programs into machine language at level C functionality.  Remaining time can be spent on your project.

You will also have opportunity to demonstrate that your symbol table printing method works.

Preparation

You will need the mMachine machine language definition at your disposal. Your TA will describe how you can access the mMachine for running translated programs. You will also need a clean sheet of paper with your team names and group numbers at the top as usual.  

To Do

Test the microMachine

Type in and run the following program and store it in a file named "test". 

PUSH #4
WRTS
HLT

An executable version of the microMachine, called "labexecute," can be found at

/students/sross/public/labexecute

Copy labexecute to the directory where you have saved file "test".  Run the virtual machine on this program by typing

labexecute test

at the command prompt.

You will repeat this process as you do the hand translations of the programs below in completing this laboratory exercise.

You can also find a version of the microMachine that you can use as you want as you finish the project on our resources page by following the mircoMachine link.  This version includes the source code (so that you can compile and install it on any machine you desire) and directions for doing this.  In the near future we hope to have a visual version of the micromachine for your use.

 

Hand Translations

Translate the following mPascal programs into mMachine code by hand.  As you do it, remember this:  You could translate any of this code just to get it to work, as if your assignment was to write an assembly language program from scratch.  However, you will get the most benefit from this by thinking about what we have talked about in lecture and then by creating a translation in the way your compiler would do it.  Try to do it with compiling in mind:
  1. construct the symbol table with offsets for the variables and use display register 0 to address the variables in the program
  2. show an activation record for the program as it might appear on the run time stack just before the program ends; be sure to show where D0 and SP point.
  3. give the mMachine code you generate during your translation; use the stack instructions of the mMachine code.
  4. run your mMachine code on the mMachine.

Program 1

program Program1;

  var A : integer;
  begin
    A := 1;
    Write(A)
  end.

Program 2

program Program2;
  var Apples : integer; 
      Fred ; integer;
      X : integer;
  begin
    Apples := 0;
    X := 10;
    Fred := Apples + Apples * X;
    Write(Apples, X, Fred)
  end.

Program 3

program Program3;
  var A : integer;
      B : integer;
      C : integer;
  begin
    Read(A, B, C);
    A := 3*A*A*A + 5*B*B - C;
    Write(A, B, C);
  end.

Symbol Table Printing

Run each of the above programs through your current compiler.  Insert a call to print the symbol table in your parser at the point where the keyword begin is matched.  Print  the symbol tables printed by your compiler.  If your compiler is capable of constructing symbol tables for procedures and functions as well, you can include a sample program and symbol table printout for a program of your choosing that includes procedures and functions to demonstrate how cool your compiler is.

To Turn In

Turn in your hand translations and hand constructed symbol tables for each program.  Also turn in the symbol tables printed by your compiler for these programs. After you have done this, use the remainder of your time to work on your project.