Run your Level C compiler on the following program from last week's lab. Print the symbol table and the mMachine code generated by your compiler. If you are not this far along, just note this on your sheet.
program Program3;var A: Integer; B: Integer; C: Integer;begin Read(A, B, C); A := 3*A*A*A + 5*B*B - C; Write(A, B, C); end.
You may need to ask the TA for guidance on how to implement Boolean values (True and False) if you can't figure out a good way on your own. There will often be situations when you are writing a compiler in which you must use code to accomplish something for which there are no handy machine language instructions available. In the case of your project, there are no built in TRUE or FALSE values. You are to use 0 for FALSE and 1 for TRUE. There are also no stack direct stack compare operations. So you will have to use instructions that allow you to look at different places in the stack without actually popping the values from the stack. You will also have to force pops on the stack by subtracting appropriate values from the stack pointer.
Look at the following example and its translation for guidance.
program Main;var a, b: Integer;begin {Main} read(a, b); if a < b then write(a) else write(b) end.;set up AR for the main program PUSH D0 MOV SP D0 ADD SP #2 SP ;read in values for a and b RD 0(D0) RD 1(D0) ;push a and then b onto the stack. (Why not just access a and b directly ;at their locations 0(D0) and 1(D0) instead of pushing them on the stack? ;In general, we must remember that the compiler is going to expect both ;operands of a relational operator to be on the stack. For example, the ;relational expression might be (a+b) < (b+c). The values of the two operands ;in this case would not be in the AR for main, but on top of the stack.) PUSH 0(D0) PUSH 1(D0) ;compare the top two stack elements with a BLT and jump to L1 if less BLT -2(SP) -1(SP) L1 ;if the comparison was false (a not less than b) execution proceeds ;at this point. The top two elements of the stack must be popped ;which can be done by subtracting 2 from the stack pointer, and ;FALSE (0) must be pushed onto the stack SUB SP #2 SP PUSH #0 BR L2 ;if the comparison was true (a less than b) execution proceeds ;at this point. The top two elements of the stack must be popped ;which can be done by subtracting 2 from the stack pointer, and ;TRUE (1) must be pushed onto the stack L1: SUB SP #2 SP PUSH #1 ;at this point the result of the relational operation (TRUE or FALSE) is ;on top of the stack. L2: ;check the result of the relational operation and branch false to the else ;part of the if statement or drop through to the then part BEQ -1(SP) #0 L3 ;then part: first pop the TRUE value from the top of the stack. ; then execute the instructions of the then part SUB SP #1 SP WRT #10 BR L4 ;else part: first pop the FALSE value from the top of the stack. ;then execute the instructions of the else part L3: SUB SP #1 SP WRT #20 ;end of the if statement. Pop the activation record for main off the ;stack and halt L4: SUB SP #2 SP POP D0 HLT
Now 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 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 Program2; var I: Integer; N: Integer;begin Read(N); for I := 1 to N do Write(I); end.
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 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.