Description
Write a simple code generator for the C– language. Translate your Three Address Code into 8086 assembly language.
You will need to add the IOStatment productions to your parser
IO_Stat -> In_Stat | Out_Stat
In_Stat -> cin >> idt In_End
In_End -> >> idt In_End | e
Out_Stat -> cout << Out_Options Out_End
Out_Options -> idt | Literal | endl
Out_End -> << Out_Options Out_End | e
Use the intermediate instruction wr# for all cout’s in your program and rd# for all cin’s. The # is replaced by either the letter i (integer), c (character) or s (string). Omit real numbers for input and output. You will need to add a new class to your symbol table to handle string literals.
The file io.asm is on the class web page and may be copied to your data disk for use in this assignment. It contains the assembly procedures
writech – write a single ASCII char
writeint – write an integer value
writestr – write a $ delimited string
writeln – write a newline
readch – read a single character
readint – read an integer value
The template for all procedures will be as follows
procname PROC
push bp
mov bp,sp
sub sp, SIZE OF LOCALS FROM SYM TABLE
; translated code
add sp, SIZE OF LOCALS FROM SYM TABLE
pop bp
ret SIZE OF PARAMETERS FROM SYM TABLE
procname ENDP
Variables in functions at depth > 1 will be stored in the stack. You will have to convert your offsets to the appropriate 8086 offsets if you have not done so already. The 8086 uses negative offsets. Your first variable ( _BP+0 ) would occur at [BP-2] in assembly. The instruction
mov ax, [BP-2]
would copy your first local variable into the AX register. Remember that you cannot do a memory to memory move in 8086 assembly.
All temporaries will be stored on the stack, you must use [bp] notations for all temps.
Your code generator needs to generate the following main procedure
_startproc PROC
mov ax, @data
mov ds, ax
call main
mov ax, 4c00h
int 21h
_startproc ENDP
END _startproc
The program beginning is as follows:
.model small
.stack 100h
.data
VAR1 DW ? ; for integer variables at depth 1
VAR2 DB ? ; for characters at depth 1
_S0 DB “String”,”$” ; for strings with appended $
.code
include io.asm
The following will be from your three address code file:
one PROC
one ENDP
etc…
main PROC
; as above
main ENDP
_startproc PROC
;; from above
_startproc ENDP
END _startproc