Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
127 views

Simpletron Java

This Java program simulates a simple computer called Simpletron. It initializes an array to store instructions and data. It then loops through the instructions, decoding the operation code and operand to perform actions like reading input, writing output, arithmetic, and transferring control. These actions manipulate registers like the accumulator and update the instruction counter. When finished, it prints the final register and memory values.

Uploaded by

Huzaifa Rasheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Simpletron Java

This Java program simulates a simple computer called Simpletron. It initializes an array to store instructions and data. It then loops through the instructions, decoding the operation code and operand to perform actions like reading input, writing output, arithmetic, and transferring control. These actions manipulate registers like the accumulator and update the instruction counter. When finished, it prints the final register and memory values.

Uploaded by

Huzaifa Rasheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;
public class Simpletron
{
public static void main(String[] args)
{
System.out.print("\nWelcome to Simpletron\n\n");

//Input/Output operations

final int READ=10;


final int WRITE=11;

//Load/Store operations
final int LOAD=20;
final int STORE=21;

//Aritrhematic operations
final int ADD=30;
final int SUBTRACT=31;
final int DIVIDE=32;
final int MULTIPLY=33;

//Transfer-of-control operations
final int HALT=43;

int opCode;
int operand;
int instructionCounter=0;
int instructionRegister=0;
int accumulator=0;

int array[]= new int[100];


Scanner input= new Scanner(System.in);

//initialization of array
int initial=0;
while(initial<args.length)
{
array[initial]=Integer.parseInt(args[initial]);
initial++;
}
int counter=1;
do
{
instructionRegister=Integer.parseInt(args[instructionCounter]);
opCode=instructionRegister/100;
operand=instructionRegister%100;

switch(opCode)
{
case READ:
{
int n;
System.out.print("Enter a number:");
n=input.nextInt();
array[operand]=n;
break;
}

case WRITE:
{
System.out.printf("\nValue entered is %d\n",array[operand]);
break;
}

This study source was downloaded by 100000850211804 from CourseHero.com on 03-04-2023 02:02:11 GMT -06:00

https://www.coursehero.com/file/64862129/Simpletronjava/
case LOAD:
{
accumulator=array[operand];
break;
}

case STORE:
{
array[operand]=accumulator;
break;
}

case ADD:
{
accumulator=accumulator+array[operand];
break;
}

case SUBTRACT:
{
accumulator=accumulator-array[operand];
break;
}
case MULTIPLY:
{
accumulator=accumulator*array[operand];
break;
}

case DIVIDE:
{
accumulator=accumulator/array[operand];
break;
}

case HALT:
{
break;
}

default:
{
System.out.printf("\nInvalid input\n");
break;
}

}
++instructionCounter;
counter++;
}while(counter<args.length);

System.out.printf("\nREGISTERS:\naccumulator %d\ninstructionCounter
%d\ninstructionRegister %d\noperationCode %d\noperand %d\n\nMEMORY:"
,accumulator,instructionCounter,instructionRegister,opCode,operand);

for(int i=0;i<args.length;i++)
{
array[i]=Integer.parseInt(args[i]);
}

System.out.println();

for(int j=0;j<10;j++)

This study source was downloaded by 100000850211804 from CourseHero.com on 03-04-2023 02:02:11 GMT -06:00

https://www.coursehero.com/file/64862129/Simpletronjava/
{
System.out.printf("\t%d",j);
}

for(int k=0;k<array.length;k++)
{
if(k%10==0)
{
System.out.printf("\n%d",k);
}
if(array[k]==0)
System.out.printf("\t0000");
else
System.out.printf("\t%d",array[k]);
}
System.out.printf("\n\nSimpletron execution is completed\n");
}
}

This study source was downloaded by 100000850211804 from CourseHero.com on 03-04-2023 02:02:11 GMT -06:00

https://www.coursehero.com/file/64862129/Simpletronjava/
Powered by TCPDF (www.tcpdf.org)

You might also like