Lab 10

Hand Translations at Level B


Objectives

The primary objective of this lab is to continue your familiarization 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 B functionality.  Remaining time can be spent on your project.

Preparation

You will need the mMachine machine language definition at your disposal as well as access to the mMachine. 

To Do

Hand Translations at Level B

Translate the following mPascal programs into mMachine code.  As you do this, give special thought  to what you will need to do in your compiler to accomplish the same task.  Remember for each program to construct the symbol table with offsets for the variables and use display register 0 to address the variables in the program.  Make the symbol tables neat! After translating each program, run it on the mPascal machine emulator.

Try your hand with the following programs.  You may not get them all done.  Get as far as you can so that you understand as much about this process as possible while you have help in the lab.

Program 1
program Program1;
  var Apples: Integer;
       Cost:   Integer;
  begin
     Read(Apples);
     if Apples > 0
       then begin
         Cost := Apples * 5;
         Write(Cost)
       end
       else begin
         Cost := Apples * 7;
         Write(Cost)
       end
   end.
Program 2
program Program2;
    var I: Integer;
       N: Integer;
  begin
     Read(N);
     for I := 1 to N do
       Write(I);
   end.
Program 3
program Program3;
  var Sum: Integer;
       I:   Integer;
       N:   Integer;
  begin
     Read(N);
     I   := 1;
     Sum := 0;
     while (I <= N) or (I > 100) do begin
       Sum := Sum + I;
       I := I + 1;
       Write(I, Sum)
     end
   end.
Program 4
program Program4;
  var Sum, I, N : Integer;
  begin
     Read(N);
     I   := 0;
     Sum := 0;
     repeat
       Sum := Sum + I;
       I   := I + 1;
       Write(I, Sum);
     until (I > N) and (I <= 100)
   end.

To Turn In

Turn in a PDF file with your translations, symbol tables, and runs for each program.  After you have done this, use the remainder of your time to work on your project.  Be sure to have your team names and group numbers on what you turn in as usual.