Project 2: ARMageddon ISA Interpreter
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Project 2: ARMageddon ISA Interpreter
Description
An Interpreter is a program that takes in a file of code, (in this case the human-readable mnemonic representations of machine instructions) and simulates their execution.
In this project, you will write an assembler for the ARMageddon CPU from 252 whose instructions are shown below. The input file format is also shown below.
ARMageddon ISA
Instruction |
Action |
add $RD, $RS, $RT |
$RD = $RS + $RT |
sub $RD, $RS, $RT |
$RD = $RS - $RT |
slt $RD, $RS, $RT |
$RD = $RS < $RT |
addi $RT, $RS, Immediate |
$RT = $RS + Immediate |
bne $RS, $RT, Label |
If $RS!=$RT then $PC=Label |
beq $RS, $RT, Label |
If $RS==$RT then $PC=Label |
nor $RD, $RS, $RT |
$RD = !($RS || $RT) |
sb $RT, Immediate($RS) |
Memory[$RS + Immediate] = $RT |
j Label |
$PC = Label |
lb $RT, Immediate($RS) |
$RT = Memory[$RS + Immediate] |
rand $RT, $RS |
$RT = random number [0, $RS-1] |
disp $RS |
Display $RS on stdout as 2-digit hex display |
halt |
Stops the CPU |
Example Program
This is a program to display a roll of a single die.
Instruction |
Comment |
addi $r1, $r0, 6 |
Loads 6 into $r1 |
rand $r2, $r1 |
Sets $r2 to be a random number between 0 and $r1 (=6) |
addi $r2, $r2, 1 |
Adds one to the random number (makes it [1,6]) |
disp $r2 |
Display $r2 on the hexadecimal display |
halt |
End the program |
Input Format
The input file format will only have a default .text section which contains the assembly code.
The opcodes can be labeled. The labels are valid C identifers (must start with letter or underscore, after that can also include numbers). They are suffixed by a colon.
Opcodes and registers are to be specified in lowercase. Registers are $r0-$r3.
Immediates are integers that can be in decimal or hexadecimal (with leading 0x).
Comments are line comments beginning with #.
label2: OPCODE $R0, $R1, 0x20 J label2 # this is a comment |
Approach
First, you will need to create a tokenizer in JFlex that returns symbols for the parser to consume. You may use the Symbol class from JavaCUP, read the JFlex manual about %cup and the CUP examples. Discard the comments and whitespace.
You have some decisions about how to structure the token types that you return from the lexer. You could return each opcode separately or group them. I’d suggest having at least a way to distinguish between R-type, I-type, and jump opcodes so that the parser has an easier job of knowing when you’ve put an immediate where you shouldn’t have, etc.
You have similar decisions about dealing with registers and integers. Try to plan this step with your grammar in mind.
For your grammar, make sure you’re only accepting legal operands. The parser should produce a List of Instructions. How you create the object hierarchy for that is up to you, but at least there should be an interface or base class Instruction. There is no tree necessary, so our intermediate representation is more of an “abstract syntax list”.
To do interpretation, iterate over your list, and do the work associated with each instruction.
Operation
There will be four 8-bit registers: $r0, $r1, $r2, and $r3. $r0 will always have the value 0 and never be writable. All other registers will hold readable and writeable data.
As you find labels in the parsing phase, use a hashtable to store the instruction number that had an associated label definition. During execution, turn that label into an instruction index for your $PC.
We support a disp instruction that displays the contents of the specified register as a 2-digit hexadecimal number on a line.
RAM is simulated as a bank of 8-bit values. Addresses are also 8-bits, meaning we have 256 memory locations to work with. This could be implemented as an array or a hashtable.
Requirements
• Use JFlex to implement an ARMageddon Assembly file lexer that returns symbols to the parser
• Use JavaCUP to implement a parser and construct a list of Instructions
• Support labels via a hashtable
• Consume files written in the input format above
• Support the instructions listed in the table above
Submission
By the deadline, you need to submit:
1. Your JFlex file containing your lexer
2. Your JavaCUP file containing your parser
3. Your java files containing main() and any auxiliary Java files you have used
4. A Makefile to build it all
5. A README text file describing how to run it
6. Any examples that you have tested your program on
Upload the files to D2L by the deadline.
2022-03-21