Skip to content
Snippets Groups Projects
Commit 7e6e7575 authored by JKCai's avatar JKCai
Browse files

Jiankun Cai 877109

Assignment 1
parent 8a9ad830
No related branches found
No related tags found
No related merge requests found
...@@ -3,59 +3,40 @@ package swen90006.machine; ...@@ -3,59 +3,40 @@ package swen90006.machine;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
public class Machine { public class Machine
{
/** arithmetic instructions each take three registers as arguments with the /** arithmetic instructions each take three registers as arguments with the
* destination register appearing first * destination register appearing first
*/ */
/** /** add rd rs1 rs2 =~ rd = rs1 + rs2 */
* add rd rs1 rs2 =~ rd = rs1 + rs2
*/
public static final String INSTRUCTION_ADD = "add"; public static final String INSTRUCTION_ADD = "add";
/** /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */
* sub rd rs1 rs2 =~ rd = rs1 - rs2
*/
public static final String INSTRUCTION_SUBTRACT = "sub"; public static final String INSTRUCTION_SUBTRACT = "sub";
/** /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */
* mul rd rs1 rs2 =~ rd = rs1 * rs2
*/
public static final String INSTRUCTION_MULT = "mul"; public static final String INSTRUCTION_MULT = "mul";
/** /** div rd rs1 rs2 =~ rd = rs1 / rs2 */
* div rd rs1 rs2 =~ rd = rs1 / rs2
*/
public static final String INSTRUCTION_DIVIDE = "div"; public static final String INSTRUCTION_DIVIDE = "div";
/** /** ret rs =~ return rs */
* ret rs =~ return rs
*/
public static final String INSTRUCTION_RETURN = "ret"; public static final String INSTRUCTION_RETURN = "ret";
/** /** ldr rd rs offs =~ rd = rs[offs] */
* ldr rd rs offs =~ rd = rs[offs]
*/
public static final String INSTRUCTION_LOAD = "ldr"; public static final String INSTRUCTION_LOAD = "ldr";
/** /** str ra offs rb =~ ra[offs] = rb */
* str ra offs rb =~ ra[offs] = rb
*/
public static final String INSTRUCTION_STORE = "str"; public static final String INSTRUCTION_STORE = "str";
/** /** mov rd val =~ rd = val */
* mov rd val =~ rd = val
*/
public static final String INSTRUCTION_MOVE = "mov"; public static final String INSTRUCTION_MOVE = "mov";
/** /** jmp offs =~ pc = pc + offs */
* jmp offs =~ pc = pc + offs
*/
public static final String INSTRUCTION_JUMP = "jmp"; public static final String INSTRUCTION_JUMP = "jmp";
/** /** jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1 */
* jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1
*/
public static final String INSTRUCTION_JZ = "jz"; public static final String INSTRUCTION_JZ = "jz";
public static final int NUM_REGS = 32; public static final int NUM_REGS = 32;
...@@ -68,25 +49,30 @@ public class Machine { ...@@ -68,25 +49,30 @@ public class Machine {
private int count = 0; /* counts number of instructions executed so far */ private int count = 0; /* counts number of instructions executed so far */
public Machine() { public Machine()
{
memory = new int[MEMORY_SIZE]; memory = new int[MEMORY_SIZE];
regs = new int[NUM_REGS]; regs = new int[NUM_REGS];
count = 0; count = 0;
} }
private void do_add(int dest, int src1, int src2) { private void do_add(int dest, int src1, int src2)
{
regs[dest] = regs[src1] + regs[src2]; regs[dest] = regs[src1] + regs[src2];
} }
private void do_sub(int dest, int src1, int src2) { private void do_sub(int dest, int src1, int src2)
{
regs[dest] = regs[src1] - regs[src2]; regs[dest] = regs[src1] - regs[src2];
} }
private void do_mult(int dest, int src1, int src2) { private void do_mult(int dest, int src1, int src2)
{
regs[dest] = regs[src1] * regs[src2]; regs[dest] = regs[src1] * regs[src2];
} }
private void do_div(int dest, int src1, int src2) { private void do_div(int dest, int src1, int src2)
{
if (regs[src2] == 0){ if (regs[src2] == 0){
/* no op */ /* no op */
}else{ }else{
...@@ -118,7 +104,8 @@ public class Machine { ...@@ -118,7 +104,8 @@ public class Machine {
regs[rd] = val; regs[rd] = val;
} }
private int parseReg(String s) throws InvalidInstructionException { private int parseReg(String s) throws InvalidInstructionException
{
if (s.length() < 2){ if (s.length() < 2){
throw new InvalidInstructionException(); throw new InvalidInstructionException();
} }
...@@ -137,7 +124,8 @@ public class Machine { ...@@ -137,7 +124,8 @@ public class Machine {
} }
private int parseOffset(String s) private int parseOffset(String s)
throws InvalidInstructionException { throws InvalidInstructionException
{
int num = 0; int num = 0;
try { try {
num = Integer.parseInt(s); num = Integer.parseInt(s);
...@@ -150,21 +138,22 @@ public class Machine { ...@@ -150,21 +138,22 @@ public class Machine {
private void validate_reg(int reg) private void validate_reg(int reg)
throws InvalidInstructionException { throws InvalidInstructionException
{
if (reg < 0 || reg > MAX_REG) { if (reg < 0 || reg > MAX_REG) {
throw new InvalidInstructionException(); throw new InvalidInstructionException();
} }
} }
private void validate_offset(int offset) private void validate_offset(int offset)
throws InvalidInstructionException { throws InvalidInstructionException
{
if (offset < -MAX_ADDR || offset > MAX_ADDR) { if (offset < -MAX_ADDR || offset > MAX_ADDR) {
throw new InvalidInstructionException(); throw new InvalidInstructionException();
} }
} }
/** /** Execute an assembly program.
* Execute an assembly program.
* *
* @param prog is the program to execute as an iterable collection of strings, * @param prog is the program to execute as an iterable collection of strings,
* each of which is a single instruction. * each of which is a single instruction.
...@@ -174,7 +163,8 @@ public class Machine { ...@@ -174,7 +163,8 @@ public class Machine {
*/ */
int execute(List<String> instructions) int execute(List<String> instructions)
throws InvalidInstructionException, throws InvalidInstructionException,
NoReturnValueException { NoReturnValueException
{
int instructionsExecuted = 0; int instructionsExecuted = 0;
int pc = 0; int pc = 0;
......
...@@ -71,7 +71,7 @@ public class BoundaryTests { ...@@ -71,7 +71,7 @@ public class BoundaryTests {
Machine m = new Machine(); Machine m = new Machine();
final List<String> lines = readInstructions("./examples/B2_1.s"); final List<String> lines = readInstructions("./examples/B2_1.s");
int expected = m.execute(lines); int expected = m.execute(lines);
assertEquals(expected,0); assertEquals(expected,5);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment