Csc 4330/6330, Programming Language Concepts (Spring 2020)

Homework 2 (Due: 11 February (Tuesday))

Interpreter for RAM Programs

Consider the RAM Model of Computation as described in RAMComputer.pdf. Write a Python program using the PLY package to implement an interpreter for RAM programs. Here are some sample RAM programs:

Addition (p1.ram)

# Program to add two numbers
# inputs are in r1 and r2
# output, as always, is in r1
#
# initialize input registers
r1 = 9
R2 = 3
#
# begin program
n0 R2 JMP N1b
   INC R1
   DEC R2
   JMP N0a
N1 CONTINUE

Integer Division (p2.ram)

R1=7
R2=3

        MOV R3, R2
N1      R1 JMP N4b
        MOV R2, R3
N2      R2 JMP N3b
        DEC R1
        R1 JMP N1a
        DEC R2
        JMP N2a
N3      INC R4
        JMP N1a
N4      MOV R1, R4
        CONTINUE

Exponentiation (p3.ram)

R1=3			
R2=5

	MOV R3, R1		
N1	DEC R2			
	R2 JMP N4b
	MOV R6, R3		
	MOV R5, R1		
N2	DEC R5			
	R5 JMP N1a		
	MOV R4, R6		
N3	R4 JMP N2a		
	INC R3			
	DEC R4			
	JMP N3a
N4	MOV R1, R3		
	CONTINUE

Sample Runs

mirage:ram-python raj$ python3 RAM.py p1.ram 
Input:
R1 ==> 9
R2 ==> 3


Output:
R1 = 12


mirage:ram-python raj$ python3 RAM.py -d p1.ram 
Input:
R1 ==> 9
R2 ==> 3

Executing: N0	R2 JMP N1B
Executing: 	INC R1
Executing: 	DEC R2
Executing: 	JMP N0A
Executing: N0	R2 JMP N1B
Executing: 	INC R1
Executing: 	DEC R2
Executing: 	JMP N0A
Executing: N0	R2 JMP N1B
Executing: 	INC R1
Executing: 	DEC R2
Executing: 	JMP N0A
Executing: N0	R2 JMP N1B
Executing: N1	CONTINUE

Output:
R1 = 12

Submit the following files: RAM.py, RAMLexer.py, RAMParser.py, and README