I cant get the code to work. Any help would be appreciated.
Instruction.java
package simmac;
public class Instruction {
public static final int DW = 0x0000;
public static final int ADD = 0x0001;
public static final int SUB = 0x0002;
public static final int LDA = 0x0003;
public static final int LDI = 0x0004;
public static final int STR = 0x0005;
public static final int BRH = 0x0006;
public static final int CBR = 0x0007;
public static final int HLT = 0x0008;
/* if the given word corresponds to a valid opcode returns the
numerical
opcode, if it is not a valid opcode returns -1*/
public static int getOpcode(String word){
String opcode=word.toUpperCase();
switch (opcode) {
case “DW”:
return DW;
case “ADD”:
return ADD;
case “SUB”:
return SUB;
case “LDA”:
return LDA;
case “LDI”:
return LDI;
case “STR”:
return STR;
case “BRH”:
return BRH;
case “CBR”:
return CBR;
case “HALT”:
return HLT;
default:
return -1;
}
}
/* if the string operand corresponds to a valid operand for the
opcode
it returns the numerical value, otherwise it returns null
*/
private static Integer parseOperand(int opcode,String
operand,String filename,int nline) {
switch(opcode) {
case ADD:
case SUB:
case LDA:
case STR:
case BRH:
case CBR:
if (operand.matches(“\\d+”)) { // if it’s a valid unsigned
number
int val=Integer.parseInt(operand);
if(val>=0 && val<32767) // if it’s at most 16
bits
return val;
else {
System.out.println(“Error [“+filename+”(“+nline+ “)]: the number:
“+operand+” is longer than 16 bits.”);
return null;
}
}
else {
System.out.println(“Error [“+filename+”(“+nline+ “)]: invalid
number: “+operand+”.”);
return null;
}
case DW:
if (operand.matches(“[+-]?\\d+”)) {
int val=Integer.parseInt(operand);
return val;
}
else {
System.out.println(“Error [“+filename+”(“+nline+ “)]: invalid
integer number: “+operand+”.”);
return null;
}
case LDI:
if (operand.matches(“[+-]?\\d+”)) {
int val=Integer.parseInt(operand);
if(val>=-32768 && val<32767)
return val;
else {
System.out.println(“Error [“+filename+”(“+nline+ “)]: the number:
“+operand+” is longer than 16 bits.”);
return null;
}
}
else {
System.out.println(“Error [“+filename+”(“+nline+ “)]: invalid
integer number: “+operand+”.”);
return null;
}
default:
return null;
}
}
/* parses the given line and determines if it corressponds to a
valid
SIMMAC instruction */
public static Integer parseInstruction(String line,String
filename,int nline) {
String [] parts= line.split(“\\s+”);
if(parts.length>2) {
System.out.println(“Error [“+filename+”(“+nline+ “)]: a SIMMAC
instruction can only contain one opcode and one operand.”);
return null;
}
if(parts[0].length()==0) {
System.out.println(“Error [“+filename+”(“+nline+ “)]: a SIMMAC
instruction can only contain one opcode and one operand.”);
return null;
}
int opc= getOpcode(parts[0]);
if (opc==-1) {
System.out.println(“Error [“+filename+”(“+nline+ “)]: invalid
instruction opcode: “+parts[0]+”.”);
return null;
}
if(opc==HLT) {
if(parts.length>1) {
System.out.println(“Error [“+filename+”(“+nline+ “)]: invalid
operand for HALT instruction. HALT requires no operands.”);
return null;
}
return (opc << 16);
}
else {
if(parts.length!=2) {
System.out.println(“Error [“+filename+”(“+nline+ “)]: SIMMAC
instructions other than HALT must contain an opcode and an
operand.”);
return null;
}
Integer op=parseOperand(opc,parts[1],filename,nline);
if(op==null)
return null;
return ((opc << 16)| op);
}
}
}
Main.java
package simmac;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
/* open a SIMMAC program file and parse its contents, if everything
is fine
returns an array with all the instructions */
public static int [] readProgramFile(String filename) {
try {
Scanner s = new Scanner(new File(filename));
ArrayList<Integer> instructions = new ArrayList();
int nline = 1;
while (s.hasNext()){
String line=s.nextLine().trim();
if(line.length()>0)
{
Integer inst=Instruction.parseInstruction(line,filename,nline); //
parse the instruction contained in the current line
if (inst != null) { // if the instruction was valid
instructions.add(inst);
}
else
System.exit(0);
}
nline++;
}
s.close();
int [] instr = new int[instructions.size()];
for(int i=0; i<instructions.size(); i++)
instr[i]=instructions.get(i);
return instr; // success, return the instruction list
} catch (FileNotFoundException e) {
System.out.println(“Error: program file ” + filename + ” could not
be opened.”);
System.exit(0); //failure
}
return null;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.print(“Please enter the time quantum value: “); // ask
for the filename to use
Scanner s = new Scanner(System.in);
int quantum = s.nextInt();
SIMMAC cpu = new SIMMAC();
OperatingSystem os = new OperatingSystem(cpu,quantum);
if(args.length==0) {
boolean done=false;
ArrayList<String> filenames=new ArrayList();
while(!done) {
System.out.print(“Please enter a program filename to load: “); //
ask for the filename to use
filenames.add(s.next());
System.out.print(“Do you want to load another file? (Y/N):
“);
String c=s.next();
if(c.toUpperCase().equals(“N”) ||
!c.toUpperCase().equals(“Y”))
done=true;
}
for(int i=0; i<filenames.size(); i++) {
int [] program = readProgramFile(filenames.get(i));
os.loadProgram(program);
}
}
else {
for(int i=0; i<args.length; i++)
{
int [] program = readProgramFile(args[i]);
os.loadProgram(program);
}
}
os.run(); // run the processes
}
}
OperatingSystem.java
package simmac;
import java.util.ArrayList;
public class OperatingSystem {
ArrayList<Process> readyQueue; // queue of processes ready to
run
Process
currentProcess; //
process currently being executed
SIMMAC
cpu;
// cpu used to execute the processes
int quantum; // value of the time quantum
int lastLoadAddress; // address to load a
program
int clock;
public OperatingSystem(SIMMAC cpu,int quantum) {
this.cpu=cpu;
this.quantum=quantum;
lastLoadAddress=0;
readyQueue=new ArrayList();
currentProcess=null;
clock=0;
}
/* prints the ready process queue */
public void printProcesses() {
System.out.print(“Process queue: “);
System.out.print(“[ “);
for(int i=0; i<readyQueue.size(); i++)
{
if(i>0)
System.out.print(“, “);
System.out.print(readyQueue.get(i).procid);
}
System.out.println(” ]”);
}
/* Switch the current process for another from the ready queue
*/
public void switchProcess() {
if(currentProcess!=null) {
currentProcess.ACC = cpu.ACC; // save current register
state
currentProcess.PSIAR = cpu.PSIAR;
readyQueue.add(currentProcess);
}
currentProcess=readyQueue.remove(0); // get
process from queue
cpu.ACC=currentProcess.ACC; // load register state
cpu.PSIAR=currentProcess.PSIAR;
cpu.memoryLimit = currentProcess.memoryLimit; // load
memory limits
cpu.memoryBase = currentProcess.memoryBase;
clock = 0; // restart clock count
System.out.println(“\nSwitching process.”);
System.out.println(“Next process ID:
“+currentProcess.procid);
printProcesses();
System.out.println();
}
/* Run the loaded processes in an loop until all are executed or an
error happens*/
public void run() {
boolean terminate = false;
currentProcess=null;
switchProcess(); // load first process
while(!terminate)
{
boolean exitStatus = cpu.executeInstruction();
clock++;
if(exitStatus==true) { // it the process was terminated
if(readyQueue.size()>0)
{
currentProcess=null; // invalidate current process
switchProcess(); // forced swap to a different
process
}
else
terminate=true; // no more processes, exit the program
}
if(clock>=quantum && !terminate) {
switchProcess();
}
}
}
/* load a SIMMAC program to memory */
void loadProgram(int [] program) {
int startAddress=lastLoadAddress;
if (lastLoadAddress + program.length >= cpu.MEMORY_SIZE) {
System.out.println(“Error: cannot load program, program size
exceeds memory size.”);
System.exit(0);
}
for (int i=0; i<program.length; i++)
cpu.Memory[lastLoadAddress+i] = program[i];
lastLoadAddress+=program.length;
Process process= new
Process(startAddress,program.length,readyQueue.size());
readyQueue.add(process);
}
}
Process.java
package simmac;
// Definition of a process control block
public class Process {
public int procid;
public int ACC;
public int PSIAR;
public int memoryBase;
public int memoryLimit;
public Process(int address,int size,int procid) {
this. procid=procid;
ACC = 0;
PSIAR = 0;
memoryBase = address;
memoryLimit=address+size;
}
}
SIMMAC.java
package simmac;
public class SIMMAC {
public final int MEMORY_SIZE = 512;
public int Memory[];
public int memoryBase; // starting address for current
process
public int memoryLimit; // maximum address allowed for current
process
// registers
public int ACC; // accumulator
public int PSIAR; // Primary Storage Instruction Address
Register
int SAR; // Storage Address Register
int SDR; // Storage Data Register
int TMPR; // Temporary Register
int CSIAR; // Control Storage Instruction Address Register
int IR; // Instruction Register
int MIR; // Micro-instruction Register
public SIMMAC() {
Memory=new int [MEMORY_SIZE];
CSIAR = 0;
PSIAR = 0;
ACC = 0;
memoryLimit=MEMORY_SIZE;
memoryBase=0;
}
/* read from memory using the SAR register, if an error was found
returns true */
boolean read() {
if(SAR+memoryBase>=0 && SAR+memoryBase<memoryLimit)
{
SDR=Memory[memoryBase+SAR];
return false;
}
else
return true;
}
/* write to memory using the SAR register, if an error was found
returns true */
boolean write() {
if(SAR+memoryBase>=0 && SAR+memoryBase<memoryLimit)
{
Memory[memoryBase+SAR]=SDR;
return false;
}
else
return true;
}
boolean fetch() {
SAR = PSIAR;
if(read())
return true;
IR = SDR;
SDR = IR & 0xFFFF;
CSIAR = IR >> 16;
return false;
}
boolean add() {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
TMPR = SDR;
SAR = TMPR;
if(read())
return true;
TMPR = SDR;
ACC = ACC + TMPR;
CSIAR = 0;
return false;
}
boolean sub() {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
TMPR = SDR;
SAR = TMPR;
if(read())
return true;
TMPR = SDR;
ACC = ACC – TMPR;
CSIAR = 0;
return false;
}
boolean load() {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
TMPR = SDR;
SAR = TMPR;
if(read())
return true;
ACC = SDR;
CSIAR = 0;
return false;
}
boolean store() {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
TMPR = SDR;
SAR = TMPR;
SDR = ACC;
if(write())
return true;
CSIAR = 0;
return false;
}
boolean branch() {
PSIAR = SDR;
CSIAR = 0;
return false;
}
boolean conditionalBranch() {
if (ACC==0) {
PSIAR = SDR;
CSIAR = 0;
}
else {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
CSIAR = 0;
}
return false;
}
boolean loadImmediate() {
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = SDR;
CSIAR = 0;
return false;
}
/* print the contents of all the registers and all memory */
public void dump() {
System.out.printf(“Register contents:\n”);
System.out.printf(“ACC = %08X\tPSIAR = %04X\tSAR = %04X\tSDR =
%08X\n”,ACC,PSIAR,SAR,SDR);
System.out.printf(“TMPR = %08X\tCSIAR = %04X\tIR = %04X\tMIR =
%04X\n”,TMPR,CSIAR,IR,MIR);
System.out.printf(“\nMemory contents:\n%03d: “,0);
for (int i=0; i<MEMORY_SIZE; i++) {
if(i!=0 && i%8==0)
System.out.printf(“\n%03d: “,i);
System.out.printf(“%08X “,Memory[i]);
}
System.out.println();
}
public boolean executeInstruction() {
boolean halt = false;
boolean error = false;
fetch();
switch(CSIAR) {
case Instruction.ADD:
error=add();
break;
case Instruction.SUB:
error=sub();
break;
case Instruction.LDA:
error=load();
break;
case Instruction.STR:
error=store();
break;
case Instruction.BRH:
error=branch();
break;
case Instruction.CBR:
error=conditionalBranch();
break;
case Instruction.LDI:
error=loadImmediate();
break;
case Instruction.HLT:
dump();
System.out.println(“End of job.”);
halt=true;
break;
default:
dump();
System.out.printf(“Error: invalid instruction %04X.\nProgram
terminated.\n”,IR);
halt =true;
}
if(error) {
dump();
System.out.printf(“Error: invalid memory address %04X.\nProgram
terminated.\n”,SAR);
}
return (halt || error); // return true if there was an error, false
otherwise
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
class Instruction {
public static final int DW = 0x0000;
public static final int ADD = 0x0001;
public static final int SUB = 0x0002;
public static final int LDA = 0x0003;
public static final int LDI = 0x0004;
public static final int STR = 0x0005;
public static final int BRH = 0x0006;
public static final int CBR = 0x0007;
public static final int HLT = 0x0008;
/*
* if the given word corresponds to a valid opcode
returns the numerical
* opcode, if it is not a valid opcode returns -1
*/
public static int getOpcode(String word) {
String opcode =
word.toUpperCase();
switch (opcode) {
case "DW":
return DW;
case "ADD":
return
ADD;
case "SUB":
return
SUB;
case "LDA":
return
LDA;
case "LDI":
return
LDI;
case "STR":
return
STR;
case "BRH":
return
BRH;
case "CBR":
return
CBR;
case "HALT":
return
HLT;
default:
return -1;
}
}
/*
* if the string operand corresponds to a valid operand
for the opcode it
* returns the numerical value, otherwise it returns
null
*/
private static Integer parseOperand(int opcode, String
operand, String filename, int nline) {
switch (opcode) {
case ADD:
case SUB:
case LDA:
case STR:
case BRH:
case CBR:
if
(operand.matches("\\d+")) { // if its a valid unsigned number
int val = Integer.parseInt(operand);
if (val >= 0 && val < 32767) // if
its at most 16 bits
return val;
else {
System.out.println("Error ["
+ filename + "(" + nline + ")]: the number: " + operand
+ " is longer than 16 bits.");
return null;
}
} else {
System.out.println("Error [" + filename + "(" +
nline + ")]: invalid number: " + operand + ".");
return null;
}
case DW:
if
(operand.matches("[+-]?\\d+")) {
int val = Integer.parseInt(operand);
return val;
} else {
System.out.println("Error [" + filename + "(" +
nline + ")]: invalid integer number: " + operand + ".");
return null;
}
case LDI:
if
(operand.matches("[+-]?\\d+")) {
int val = Integer.parseInt(operand);
if (val >= -32768 && val <
32767)
return val;
else {
System.out.println("Error ["
+ filename + "(" + nline + ")]: the number: " + operand
+ " is longer than 16 bits.");
return null;
}
} else {
System.out.println("Error [" + filename + "(" +
nline + ")]: invalid integer number: " + operand + ".");
return null;
}
default:
return
null;
}
}
/*
* parses the given line and determines if it
corressponds to a valid SIMMAC
* instruction
*/
public static Integer parseInstruction(String line,
String filename, int nline) {
String[] parts =
line.split("\\s+");
if (parts.length > 2) {
System.out.println("Error [" + filename + "(" + nline
+ ")]: a SIMMAC instruction
can only contain one opcode and one operand.");
return
null;
}
if (parts[0].length() == 0) {
System.out.println("Error [" + filename + "(" + nline
+ ")]: a SIMMAC instruction
can only contain one opcode and one operand.");
return
null;
}
int opc =
getOpcode(parts[0]);
if (opc == -1) {
System.out
.println("Error [" + filename
+ "(" + nline + ")]: invalid instruction opcode: " + parts[0] +
".");
return
null;
}
if (opc == HLT) {
if (parts.length
> 1) {
System.out.println("Error [" + filename + "(" +
nline
+ ")]:
invalid operand for HALT instruction. HALT requires no
operands.");
return null;
}
return (opc
<< 16);
} else {
if (parts.length
!= 2) {
System.out.println("Error [" + filename + "(" +
nline
+ ")]:
SIMMAC instructions other than HALT must contain an opcode and an
operand.");
return null;
}
Integer op =
parseOperand(opc, parts[1], filename, nline);
if (op ==
null)
return null;
return ((opc
<< 16) | op);
}
}
}
public class Main {
/*
* open a SIMMAC program file and parse its contents,
if everything is fine
* returns an array with all the instructions
*/
public static int[] readProgramFile(String filename)
{
try {
Scanner s = new
Scanner(new File(filename));
ArrayList<Integer> instructions = new ArrayList();
int nline =
1;
while
(s.hasNext()) {
String line = s.nextLine().trim();
if (line.length() > 0) {
Integer inst =
Instruction.parseInstruction(line, filename, nline); // parse
// the
// instruction
// contained
// in
// the
// current
// line
if (inst != null) { // if the
instruction was valid
instructions.add(inst);
} else
System.exit(0);
}
nline++;
}
s.close();
int[] instr =
new int[instructions.size()];
for (int i = 0;
i < instructions.size(); i++)
instr[i] = instructions.get(i);
return instr; //
success, return the instruction list
} catch (FileNotFoundException e)
{
System.out.println("Error: program file " + filename + " could not
be opened.");
System.exit(0);
// failure
}
return null;
}
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
System.out.print("Please enter the
time quantum value: "); // ask for
// the
// filename
// to use
Scanner s = new
Scanner(System.in);
int quantum = s.nextInt();
SIMMAC cpu = new SIMMAC();
OperatingSystem os = new
OperatingSystem(cpu, quantum);
if (args.length == 0) {
boolean done =
false;
ArrayList<String> filenames = new ArrayList();
while (!done)
{
System.out.print("Please enter a program
filename to load: "); // ask
// for
// the
// filename
// to
// use
filenames.add(s.next());
System.out.print("Do you want to load another
file? (Y/N): ");
String c = s.next();
if (c.toUpperCase().equals("N") ||
!c.toUpperCase().equals("Y"))
done = true;
}
for (int i = 0;
i < filenames.size(); i++) {
int[] program =
readProgramFile(filenames.get(i));
os.loadProgram(program);
}
} else {
for (int i = 0;
i < args.length; i++) {
int[] program = readProgramFile(args[i]);
os.loadProgram(program);
}
}
os.run(); // run the
processes
}
}
class OperatingSystem {
ArrayList<Process> readyQueue; // queue of
processes ready to run
Process currentProcess; // process currently being
executed
SIMMAC cpu; // cpu used to execute the processes
int quantum; // value of the time quantum
int lastLoadAddress; // address to load a
program
int clock;
public OperatingSystem(SIMMAC cpu, int quantum)
{
this.cpu = cpu;
this.quantum = quantum;
lastLoadAddress = 0;
readyQueue = new ArrayList();
currentProcess = null;
clock = 0;
}
/* prints the ready process queue */
public void printProcesses() {
System.out.print("Process queue:
");
System.out.print("[ ");
for (int i = 0; i <
readyQueue.size(); i++) {
if (i >
0)
System.out.print(", ");
System.out.print(readyQueue.get(i).procid);
}
System.out.println(" ]");
}
/* Switch the current process for another from the
ready queue */
public void switchProcess() {
if (currentProcess != null) {
currentProcess.ACC = cpu.ACC; // save current register state
currentProcess.PSIAR = cpu.PSIAR;
readyQueue.add(currentProcess);
}
currentProcess =
readyQueue.remove(0); // get process from queue
cpu.ACC = currentProcess.ACC; //
load register state
cpu.PSIAR =
currentProcess.PSIAR;
cpu.memoryLimit =
currentProcess.memoryLimit; // load memory limits
cpu.memoryBase =
currentProcess.memoryBase;
clock = 0; // restart clock
count
System.out.println("\nSwitching
process.");
System.out.println("Next process
ID: " + currentProcess.procid);
printProcesses();
System.out.println();
}
/*
* Run the loaded processes in an loop until all are
executed or an error
* happens
*/
public void run() {
boolean terminate = false;
currentProcess = null;
switchProcess(); // load first
process
while (!terminate) {
boolean
exitStatus = cpu.executeInstruction();
clock++;
if (exitStatus
== true) { // it the process was terminated
if (readyQueue.size() > 0) {
currentProcess = null; //
invalidate current process
switchProcess(); // forced
swap to a different process
} else
terminate = true; // no more
processes, exit the program
}
if (clock >=
quantum && !terminate) {
switchProcess();
}
}
}
/* load a SIMMAC program to memory */
void loadProgram(int[] program) {
int startAddress =
lastLoadAddress;
if (lastLoadAddress +
program.length >= cpu.MEMORY_SIZE) {
System.out.println("Error: cannot load program, program size
exceeds memory size.");
System.exit(0);
}
for (int i = 0; i <
program.length; i++)
cpu.Memory[lastLoadAddress + i] = program[i];
lastLoadAddress +=
program.length;
Process process = new
Process(startAddress, program.length, readyQueue.size());
readyQueue.add(process);
}
}
class Process {
public int procid;
public int ACC;
public int PSIAR;
public int memoryBase;
public int memoryLimit;
public Process(int address, int size, int procid)
{
this.procid = procid;
ACC = 0;
PSIAR = 0;
memoryBase = address;
memoryLimit = address + size;
}
}
class SIMMAC {
public final int MEMORY_SIZE = 512;
public int Memory[];
public int memoryBase; // starting address for current
process
public int memoryLimit; // maximum address allowed for
current process
// registers
public int ACC; // accumulator
public int PSIAR; // Primary Storage Instruction
Address Register
int SAR; // Storage Address Register
int SDR; // Storage Data Register
int TMPR; // Temporary Register
int CSIAR; // Control Storage Instruction Address
Register
int IR; // Instruction Register
int MIR; // Micro-instruction Register
public SIMMAC() {
Memory = new
int[MEMORY_SIZE];
CSIAR = 0;
PSIAR = 0;
ACC = 0;
memoryLimit = MEMORY_SIZE;
memoryBase = 0;
}
/*
* read from memory using the SAR register, if an error
was found returns
* true
*/
boolean read() {
if (SAR + memoryBase >= 0
&& SAR + memoryBase < memoryLimit) {
SDR =
Memory[memoryBase + SAR];
return
false;
} else
return
true;
}
/*
* write to memory using the SAR register, if an error
was found returns
* true
*/
boolean write() {
if (SAR + memoryBase >= 0
&& SAR + memoryBase < memoryLimit) {
Memory[memoryBase + SAR] = SDR;
return
false;
} else
return
true;
}
boolean fetch() {
SAR = PSIAR;
if (read())
return
true;
IR = SDR;
SDR = IR & 0xFFFF;
CSIAR = IR >> 16;
return false;
}
boolean add() {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
TMPR = SDR;
SAR = TMPR;
if (read())
return
true;
TMPR = SDR;
ACC = ACC + TMPR;
CSIAR = 0;
return false;
}
boolean sub() {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
TMPR = SDR;
SAR = TMPR;
if (read())
return
true;
TMPR = SDR;
ACC = ACC - TMPR;
CSIAR = 0;
return false;
}
boolean load() {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
TMPR = SDR;
SAR = TMPR;
if (read())
return
true;
ACC = SDR;
CSIAR = 0;
return false;
}
boolean store() {
TMPR = ACC;
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = TMPR;
TMPR = SDR;
SAR = TMPR;
SDR = ACC;
if (write())
return
true;
CSIAR = 0;
return false;
}
boolean branch() {
PSIAR = SDR;
CSIAR = 0;
return false;
}
boolean conditionalBranch() {
if (ACC == 0) {
PSIAR =
SDR;
CSIAR = 0;
} else {
TMPR =
ACC;
ACC = PSIAR +
1;
PSIAR =
ACC;
ACC =
TMPR;
CSIAR = 0;
}
return false;
}
boolean loadImmediate() {
ACC = PSIAR + 1;
PSIAR = ACC;
ACC = SDR;
CSIAR = 0;
return false;
}
/* print the contents of all the registers and all
memory */
public void dump() {
System.out.printf("Register
contents:\n");
System.out.printf("ACC =
%08X\tPSIAR = %04X\tSAR = %04X\tSDR = %08X\n", ACC, PSIAR, SAR,
SDR);
System.out.printf("TMPR =
%08X\tCSIAR = %04X\tIR = %04X\tMIR = %04X\n", TMPR, CSIAR, IR,
MIR);
System.out.printf("\nMemory
contents:\n%03d: ", 0);
for (int i = 0; i < MEMORY_SIZE;
i++) {
if (i != 0
&& i % 8 == 0)
System.out.printf("\n%03d: ", i);
System.out.printf("%08X ", Memory[i]);
}
System.out.println();
}
public boolean executeInstruction() {
boolean halt = false;
boolean error = false;
fetch();
switch (CSIAR) {
case Instruction.ADD:
error =
add();
break;
case Instruction.SUB:
error =
sub();
break;
case Instruction.LDA:
error =
load();
break;
case Instruction.STR:
error =
store();
break;
case Instruction.BRH:
error =
branch();
break;
case Instruction.CBR:
error =
conditionalBranch();
break;
case Instruction.LDI:
error =
loadImmediate();
break;
case Instruction.HLT:
dump();
System.out.println("End of job.");
halt =
true;
break;
default:
dump();
System.out.printf("Error: invalid instruction %04X.\nProgram
terminated.\n", IR);
halt =
true;
}
if (error) {
dump();
System.out.printf("Error: invalid memory address %04X.\nProgram
terminated.\n", SAR);
}
return (halt || error); // return
true if there was an error, false
// otherwise
}
}
I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public...
Explain in detail what the code below does: public class MyClass { public static void main(String args[]) { System.out.println(isUniqueChars("something")); } public static boolean isUniqueChars(String str) { int checker = 0; for (int i = 0; i < str.length(); ++i) { int val = str.charAt(i) - 'a'; if ((checker & (1 << val)) > 0) return false; checker |= (1 << val); } return true;...
Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; public class ImageLab { /* * This is the grayscale example. Use it for inspiration / help, but you dont * need to change it. * * Creates and returns a new BufferedImage of the same size as the input * image. The new image is the grayscale version of the input (red, green, and * blue components averaged together)....
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...
Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame { static String[][]maze; public static void main(String[] args) { maze=new String[30][30]; maze=fillArray("mazefile.txt"); } public static String[][]fillArray(String file) { maze = new String[30][30]; try{...
departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore { private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee; } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...
cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...
JAVA PROBLEMS 1. Update the below method to throw an IndexOutOfBoundsException: public E get(int index) { if (index < 0 || index > numItems) { System.out.println("Get error: Index " + index + " is out of bounds."); return null; } return array[index]; } Hint: Update both the method signature and the method body! 2. Update the below method to include a try-catch block rather than throwing the exception to...
Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...
import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...
Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...