程序代写代做代考 compiler The TINY Language

The TINY Language

The TINY Machine

CS106 — Compiler Principles and Construction
Fall 2014

MUST FIT
Zhiyao Liang

TINY Dr. Zhiyao Liang
1

Basic Architecture of the Tiny Machine
#define IADDR_SIZE …
/* size of instruction memory*/
#define DADDR_SIZE …
/* size of data memory */
#define NO_REGS 8 /* number of registers */
#define PC_REG 7

Instruction iMem[IADDR_SIZE];
int dMem[DADDR_SIZE];
int reg[NO_REGS];
TINY Dr. Zhiyao Liang
2

TM performs a conventional
fetch-execute cycle
do
/* fetch */
currentInstruction = iMem[reg[pcRegNo]++];
/*execute current instruction */

While (!(halt || error));
TINY Dr. Zhiyao Liang
3

RO Instructions
opcode r, s, t
Opcode Effect
HALT Stop execution
(operands ignored)
IN reg[r]  integer value read from the standard input
OUT reg[r]  the standard output (s and t ignored)
ADD reg[r] = reg[s] + reg[t]
SUB reg[r] = reg[s] – reg[t]
MUL reg[r] = reg[s] * reg[t]
DIV reg[r] = reg[s] / reg[t]
(may generate ZERO_DIC)

TINY Dr. Zhiyao Liang
4

RM instructions
opcode r, d(s) a = d + reg[s]
Opcode Effect
LD reg[r] = dMem[a] (load r with memory value at a)
LDA reg[r] = a (load address a directly into r)
LDC reg[r] = d (load constant d directly into r—s is ignored)
ST dMem[a] = reg[r] (store value in r to memory location a)
JLT If (reg[r] < 0) reg[PC_REG] = a (jump to instruction a if r is negative, similarly for the following) JLE If (reg[r] <= 0) reg[PC_REG] = a JGE If (reg[r] >= 0) reg[PC_REG] = a
JGT If (reg[r] > 0) reg[PC_REG] = a
JEQ If (reg[r] == 0) reg[PC_REG] = a
JNE If (reg[r] != 0) reg[PC_REG] = a

TINY Dr. Zhiyao Liang
5

example
* This program inputs an integer, computes
* Its factorial if it is positive
* And prints the result
0: IN 0,0,0 r0 = read
1: JLE 0,6(7) if 0 < r0 then 2: LDC 1,1,0 r1 = 1 3: LDC 2,1,0 r2 = 1 * repeat 4: MUL 1,1,0 r1 = r1 * r0 5: SUB 0,0,2 r0 = r0 – r2 6: JNE 0, -3(7) until r0 == 0 7: OUT 1,0,0 write r1 8: HALT 0,0,0 halt * End of program TINY Dr. Zhiyao Liang 6 Typical code arrangement for if-statement TINY Dr. Zhiyao Liang 7 Code before if-statement Code for if test Conditional jump (FALSE) (TRUE) Code for TRUE case Unconditional jump Code for FALSE case Code after if-statement Typical code arrangement for while-statement TINY Dr. Zhiyao Liang 8 Code before while-statement Code for while test Conditional jump (FALSE) (TRUE) Code for body of while Unconditional jump Code after while-statement /docProps/thumbnail.jpeg