Lab 9 - Hand Translations at Levels C and B


Objectives

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

Preparation

You will need the

all of which you can find on the Resources page.

The executable should run on esus, and on a Mac from the terminal window.

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 μMachine

Log in to esus, type in the following program, and store it in a file named test.il in the same directory where the μMachine execute interpreter is (downloaded above).


PUSH #4
WRTS
HLT

In the directory where execute and test.pas are, run the virtual machine interpreter on this program by typing:

./execute test.il

at the command prompt.

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

Hand Translations

Translate the following μPascal programs into μMachine 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 lectures and then by creating a translation in the way your compiler would do it.  Try to do it with compiling in mind:
  1. Use the stack instructions of the μMachine (there are also register instructions) for your translation.

  2. Remember that to keep the spirit of the translation true to what you would do in a compiler. At the beginning of your program (which only contains a main program--no procedures or functions) you should insert code to save the contents of D0 (push it onto the stack), then set D0 to the same value as the stack pointer, then add the size of the activation record for the program you are compiling to SP so that it points to the first free enty above the activation record.  Then you can generate all of the code necessary to translate your program, using the stack for all accesses to variables (by way of an offset from D0) and for arithmetic.  Before the HLT instruction at the end, you should restore D0 and SP to their original values.  That is, you should subtract the size of the activation record from SP, then pop the stack top into D0 (that is where you saved the original D0 value).  When your program quits, SP and D0 then have the original values they had before your translated program ran.

  3. print out the μMachine code you generate during your translation.

  4. run your μMachine code on the μMachine.

Program 1


program Program1;
  var A : integer;
    begin
      A := 1;
      Write('A = ', A)
    end.

Program 2


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

Program 3


program Program3;
  var A : integer;
      B : float;
      C : integer;
   begin
     Write('Enter an integer for A: ');
     Read(A);
     Writeln ('A = ', A);
     Write('Enter a float for B: ');
     Read(B);
     Writeln('B = ', B);
     Write('Enter an integer for C: ');
     Read(C);
     Writeln('C = ', C);
     A := 3*A*A*A + 5*B*B - C;
     Write('A = ', A, ', B = ', B,', and C = ', C);
  end.

Program 4

Write a simple Pascal program with an if statement. Translate it into μMachine code as you have done for the examples prepared above.

To Turn In

Copy your hand translatons for each program into a Word document followed by a copy of the output from running execute on your code.

Submit the document as a PDF to the lab 9 dropbox in D2L