THE COMPILER PROJECT

advertisement
THE COMPILER PROJECT
Write a compiler which translates the airplane language
described below into assembler.
main
end main
identifier = arithmetic expression
plane(arithmetic expression, arithmetic expression)
do identifier = number to number
end do
fire
delay(arithmetic expression)
int list of identifiers separated by commas
All statements should end in a semicolon, and each
statement should be on a separate single line.
The arithmetic expression referred to above should be
ones constructed from numbers and/or identifiers using:
()+-*
The first statement of any program in this language should
be:
main
and the last statement:
end main
These should be the only occurrences of these two
statements. The translated program should end when a
key is pressed, and it should then restoring the video
mode to 3 before finally ending up.
All int declarations should follow the main statement
Any number of statements may be coded between a
do statement
and its corresponding
end do
including additional nested do loops
Produce in advance two separately compiled subroutines
(produced by yourself, not the above compiler) as follows:
1. a subroutine that blanks the screen1 and then draws
a plane at column x, row y - and let the translated code
for plane(x, y) include a call to this subroutine, as well
as setting a global variable called cc to x and a global
variable called rr to y.
2. a subroutine that fires the airplane’s guns for 2 seconds,
and let the translated code for fire call this subroutine.
Fire makes use of the global variables cc and rr in order
to determine the position of the plane.
The subroutines should be coded with the following
characteristics:
external
near
fixes up the stack
called by value
arguments are left to right (i.e. its leftmost argument is
pushed onto the stack first, and the rightmost one
last) coded in the standard way employed by all
commercial compilers.
Your compiler does not have to do any error checking.
You can freely ask for help about the above format for
subroutines. The subroutines will not be graded, and points
will not be awarded for how impressive your plane looks
(since these have already been graded in the Airplane
Project). Any recognizable plane drawing will do equally well.
The sample compiler, which only implemented addition, only made use
of register AX. For the arithmetic expressions of the Project, it would be
advisable (but is not a requirement) to make use of two registers e.g. AX
1
This can optionally be done by changing the plane in it’s previous position to the background color
and BX. In this case, employ two flags, AxFree and BxFree and set
AxFree or BxFree to represent ”false” when you put something in it, and
set both of them to represent “true” when you carry out a reduction which
has “statement” (or the equivalent symbol you employ) as left hand side.
Almost all arithmetic expressions that occur in practice can be evaluated
using just two registers. It is, however, possible to construct arithmetic
expressions that require more than any given number of registers, such
as one of the form X1 + (X2 * (X3 +X4 * ( … )…). But your compiler is
not required to work on expressions that require more registers than it
makes use of.
Example.
Here is a sample program written in the Airplane language.
It displays an airplane moving slowly 100 pixels in a
horizontal direction. The airplane fires its guns after every 20
pixels.
main;
int col, row, j, k
col = 100;
row = 160;
do j = 1 to 5;
do k = 1 to 20;
plane((j-1)*20+col+k, row);
delay(200);
end do;
fire;
end do;
end main;
A possible version (haven’t tested it yet) of the translated
program is as follows:
public cc, rr
.model small
.stack 100
; these variables are to be declared as extern in
; the subroutine fire
.data
cc
dw
?
rr
dw
?
col
dw
?
row
dw
?
k
dw
?
.code
main proc
mov ax, @data
mov ds, ax
mov ah, 0
mov al, 13h
int
10h
mov j, 1
dec j
do1:
inc
j
cmp j, 5
jg
enddo1
mov k, 1 ‘put initial value into k. It happens to be 1 in
‘ this case
dec k
do2:
inc
k
cmp k, 20
jg
enddo2
mov ax, j
sub ax, 1
imul 20
add ax, col
add ax, k
mov cc, ax
push ax
mov ax, row
mov rr, ax
push ax
call
plane
mov ax, 200
push ax
call
delay
jmp
do2
enddo2:
call
fire
jmp
do1
enddo1:
mov ah, 1
int
21h ;wait for a key to be pressed
mov al, 3 ;restore video mode 3
mov ah, 0
int
10h
mov ah, 4c
int
21h
main endp
end main
Demonstration
To demonstrate your compiler, write a program in the
Airplane language which causes the airplane to move
slowly in a large Star of David circuit, traversing the circuit
a large no. (say 1000) times. It
should take about
a ½ second to get from vertex
to vertex, and it
should pause briefly (say
for ¼ second) at
each vertex and fire its
guns there.
Use your compiler to translate this program from the
Airplane language into Assembler, and then use ml to
produce an executable from the Assembler program. This
process is illustrated in the description of the sample
compiler.
What to submit
Submit listings of the lex and yacc definition files you
employed for the compiler, the above Airplane language
program, and the Assembler program your compiler
translated it into. In addition submit executables for your
compiler and the one obtained from the Assembler program.
Download