At this point in time, you should have written a scanner and parser
for mPascal, should be working on the symbol
table and should be thinking about semantic processing and code generation.
The following information about the mMachine
and mCode is provided to assist you in your
design and implementation of the remaining parts of the mPascal
compiler project:
The mMachine is a virtual machine (simulated by a program) with the following hardware characteristics:The mMachine is a stack-based machine; all memory is allocated/deallocated on the data stack residing in RAM:
- Separate instruction space (for assembly code) and RAM (for data storage/retrieval)
- 10 general purpose registers (D0 - D9)
- Special stack pointer register (SP)
- The data stack consists of single, integer locations (size is 1)
- The data stack grows upwards (starts at 0, pushes increment the SP, pops decrement the SP)
mCode (assembly language) is based on QUADRUPLES. Each quadruple consists of an opcode and up to three operands.Opcodes:
At present there are 26 valid opcodes (instructions) in the mCode assembly language...HLT
RD WRT WRTS
MOV NEG ADD SUB MUL DIV
PUSH POP
NEGS ADDS SUBS MULS DIVS
BR BEQ BGE BGT BLE BLT BNE
CALL RETOperands:
MODE FORM SAMPLE DESCRIPTION IMMEDIATE #n #4 Literal value REGISTER Dn D6 Contents of register n INDEXED m(Dn) 5(D3) Address = Dn + m INDIRECT @m(Dn) @7(D1) Address = Contents of (Dn + m) STACK REGISTER SP SP Stack pointer STACK INDEXED m(SP) 6(SP) Address = SP + m STACK INDIRECT @m(SP) @2(SP) Address = Contents of (SP + m) Labels:
Labels are specified with either Ln: (defining a label) or Ln (using a label).
|
|
|
| HLT | Terminate program execution |
| RD dst | Read a value from the keyboard into dst |
| WRT src | Write a value in src to the screen |
| WRTS | Performs: POP A WRT A |
| MOV src dst | Performs: dst <- src |
| NEG src dst | Performs: dst <- -src |
| ADD src1 src2 dst | Performs: dst <- src1 + src2 |
| SUB src1 src2 dst | Performs: dst <- src1 - src2 |
| MUL src1 src2 dst | Performs: dst <- src1 * src2 |
| DIV src1 src2 dst | Performs: dst <- src1 / src2 |
| PUSH src | Push src onto the data stack |
| POP dst | Pop the stack top into dst |
| NEGS | Performs: POP A PUSH -A |
| ADDS | Performs: POP A POP B PUSH B + A |
| SUBS | Performs: POP A POP B PUSH B - A |
| MULS | Performs: POP A POP B PUSH B * A |
| DIVS | Performs: POP A POP B PUSH B / A |
| Ln: | Drop a label at the current line |
| BR Ln | Branch to label n |
| BEQ src1 src2 Ln | Branch to label n if src1 = src2 |
| BGE src1 src2 Ln | Branch to label n if src1 >= src2 |
| BGT src1 src2 Ln | Branch to label n if src1 > src2 |
| BLE src1 src2 Ln | Branch to label n if src1 <= src2 |
| BLT src1 src2 Ln | Branch to label n if src1 < src2 |
| BNE src1 src2 Ln | Branch to label n if src1 <> src2 |
| CALL Ln | Performs: PUSH PC BR Ln |
| RET | Performs: POP PC |