Summary: In today's lab, you will explore PAL, the pseduo-assembly language that I've written (okay, started to write) as an intermediate language for your compilers.
Warning: Both PAL and this lab are under development. Both probably have many bugs. Let me know which ones you notice.
Collaboration: Feel free to work on this lab in pairs or trios.
Turning It In: Save your answers in a plain text file and submit it using the ECA.
Grading: I expect that you will gain more from doing this lab than from me grading this lab. I may simply scan through your answers to see if you had any particularly valuable insights.
1. Make copies of
2. Make a copy of the
pal library in a directory on your
$CLASSPATH. For example, you could execute the following
% cp -r /home/rebelsky/Web/Courses/CS362/2002F/Examples/pal . % export CLASSPATH="`pwd`:$CLASSPATH"
PAL, the pseudo-assembly language, is a set of Java classes intended
to provide a simple assembly-language-like intermediate code. PAL
consists of a model of a computer,
Computer, a number of
Container subclasses that hold values, and a number of
Instruction implementations that represent instructions.
Computer class provides four key methods.
Computer(int memsize), which builds a computer whose memeory holds memsize non-code values.
setCode(InstructionSequence code), which sets the program code for the machine.
dump(java.io.PrintWriter out), which prints the program to the selected destination.
run(boolean trace), which runs the program (starting at instruction 0). If trace is true, it also prints out each instruction as it executes it.
Container subclass provides four key procedures:
iget(Computer hal)gets the integer associated with the container.
fget(Computer hal)gets the float associated with the container. (PAL is untyped, so each container can store either kind of value.)
iset(Computer hal, int newval)tells the container to store a new integer value.
fset(Computer hal, int newval)tells the container to store a new float value.
There are many kinds of containers.
IConstantcontainers store integer constants. Once created, they cannot change.
Registercontainers refer to the key registers. You cannot create new registers. Instead, you must use the built-in registers,
Temporarycontainers are temporary locations. You should eventually eliminate all temporaries from the program.
MemLoccontainers are positions in memory. Each has an index (which can be any kind of container) which specifies where in memory the location resides.
Labelobjects are also treated as containers because it simplifies the implementation.
Instructions provide one key method,
execute(Computer hal), which executes the given instruction on that particular computer.
As you may have noted, a number of the methods take
objects as parameters. I've made that choice so that they can access or
modify the state of the computer.
PAL provides an ever-growing set of instructions which you can identify by looking at the source code.
Note that the
InstructionSequence instruction is a special
instruction designed for building trees of instructions, as you would
create while traversing a parse tree.
a. Scan through
TestPAL.java. See if you can determine
what it does.
b. Compile and execute
TestPAL.java. Observe its output.
c. Why was the program able to use
What was the output?
d. Turn off program tracing and execute the program again.
TestPAL.java to count down from 5 to 1 (instead
of up from 1 to 10).
a. Scan through
SumPAL.java. As you can see, the program
repeatedly reads values and shoves them on the stack. When 0 is entered,
it repeatedly pops values off of the stack and adds them to the counter.
b. Run the program and enter three non-zero values followed by zero. Does it do what you expect?
c. Add an instruction to print the stack pointer at each step and rerun the program with three non-zero values. Does it do what you expect? When you're done observing the stack pointer, you can either keep or remove the line to print the stack pointer.
d. Run the program and enter seven non-zero values followed by zero. What does it do? WHy?
e. Run the program and enter five non-zero values followed by zero. What does it do? Why?
f. Run the program and enter four non-zero values followed by zero. What does it do? Why?
Write a PAL program that reads three numbers, initial, step, and final,
counts (by printing results) from initial to final using
increments of step.
Monday, 18 November 2002 [Samuel A. Rebelsky]
Tuesday, 19 November 2002 [Samuel A. Rebelsky]
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Dec 10 08:53:34 2002.
The source to the document was last modified on Tue Nov 19 09:08:41 2002.
This document may be found at