I am new to java.I am learning from a book called "Java how to program" from Dietel. First, Is it a good book?
Second, I want help about an exersice the exersice is to make a simulation to a mchine langauge hat supports the following instructions:
final int READ = 10; Read a word from the keyboard into a specific location in memory.
final int WRITE = 11; Write a word from a specific location in memory to the screen.
final int LOAD = 20; Load a word from a specific location in memory into the accumulator.
final int STORE = 21; Store a word from the accumulator into a specific location in memory.
final int ADD = 30; Add a word from a specific location in memory to the word in the accumulator (leave the result in the accumulator).
final int SUBTRACT = 31; Subtract a word from a specific location in memory from the word in the accumulator (leave the result in the accumulator).
final int DIVIDE = 32; Divide a word from a specific location in memory into the word in the accumulator (leave result in the accumulator).
final int MULTIPLY = 33; Multiply a word from a specific location in memory by the word in the accumulator (leave the result in the accumulator).
final int BRANCH = 40; Branch to a specific location in memory.
final int BRANCHNEG = 41; Branch to a specific location in memory if the accumulator is negative.
final int BRANCHZERO = 42; Branch to a specific location in memory if the accumulator is zero.
final int HALT = 43; Halt. The program has completed its task
and here is my code i want to know if there r any logical errors. and if there any optimization for the code
import java.util.Scanner;
public class SML
{
private static int acc, instcounter, opcode, operand, instregister;
private static int[] memory;
private static Scanner input = new Scanner ( System.in );
public static void load()
{
int index =0;
System.out.println("*** Welcome to Simpletron! ***\n" +
"*** Please enter your program one instruction ***\n" +
"*** (or data word) at a time into the input ***\n" +
"*** text field. I will display the location ***\n" +
"*** number and a question mark (?). You then ***\n" +
"*** type the word for that location. Press the ***\n" +
"*** Done button to stop entering your program. ***" );
while( true )
{
System.out.printf("%02d ", index);
memory[index] = input.nextInt();
if( memory[index] == -99999 )
break;
else
index++;
System.out.print("\n");
}
System.out.print("*** Program loading completed ***\n" +
"*** Program execution begins ***\n" );
}
public static void fetch()
{
instregister = memory[instcounter];
opcode = instregister / 100;
operand = instregister % 100;
}
public static void execute()
{
switch( opcode )
{
case 10: // Read from Keyboard
System.out.print("Input an integer: ");
memory[operand] = input.nextInt();
//System.out.printf("\n%d\n", memory[operand]);
instcounter ++;
break;
case 11: // print value of a memory location
System.out.printf("%d\n", memory[operand]);
instcounter ++;
break;
case 20: //Load accumulator
acc = memory[operand];
//System.out.printf("\n%d\n", acc);
instcounter ++;
break;
case 21: //Store accumulator
memory[operand] = acc;
//System.out.printf("\n%d\n", memory[operand]);
instcounter ++;
break;
case 30: //Add
acc += memory[operand];
//System.out.printf("\n%d\n", acc);
instcounter ++;
break;
case 31: //Sub
acc -= memory[operand];
instcounter ++;
break;
case 32: //Div
acc /= memory[operand];
instcounter ++;
break;
case 33: //Multiply
acc *= memory[operand];
instcounter ++;
break;
case 40: //unconditional branch
instcounter = operand;
break;
case 41: //Branch if negative
if( acc < 0)
instcounter = operand;
else
instcounter ++;
break;
case 42: //Branch if Zero
if( acc == 0)
instcounter = operand;
else
instcounter ++;
break;
case 43: //Halt
System.out.print("*** Simpletron execution terminated ***");
instcounter ++;
break;
}
}
public static void main(String[] args)
{
memory = new int[100];
load();
while ( memory[instcounter] != -99999)
{
fetch();
execute();
}
}
}
Machine Langauge simu
Started by AmrAwad, Jan 12 2008 09:51 PM
5 replies to this topic
#1
Posted 12 January 2008 - 09:51 PM
|
|
|
#2
Posted 13 January 2008 - 05:41 AM
The 'How to program' series are reasonable if you are learning your first language but I wouldn't bother with them if you have already learned at least one language. Spends far too much time on things that should take 5 minutes once you understand the concepts.
If you want to know if there are logical errors in your code then test it. Run up some programs and find the expected output by hand. Then run it through your machine.
If you want to know if there are logical errors in your code then test it. Run up some programs and find the expected output by hand. Then run it through your machine.
#3
Posted 13 January 2008 - 08:52 AM
I have done this project before. As I can see from the code, you should organized it a little bit better. For example, instead of using the code for the instruction (eg. 10 for READ), declare some constants with the instruction value and use them. I mean, something like this:
[HIGHLIGHT="Java"]
private final int READ = 10;
private final int WRITE = 11;
private final int LOAD = 20;
...
[/HIGHLIGHT]
Your missing a couple of things there, like error handling, which is required in the exercise as well. I don't know if that's the whole code but I don't see the a printDump() function, which prints the contents of the memory. I suggest you work on these details 'cause they are important for the completeness of the exercise.
In respect to the book, I agree with G_Morgan. It's a good book for beginners but a little too repetitive for someone who already know the basics. Anyway it's a good book since it have many advance topics as well, like networking, multithreading, GUIs, and more. The exercises are challenging and fun too, so I suggest you take them seriously.
[HIGHLIGHT="Java"]
private final int READ = 10;
private final int WRITE = 11;
private final int LOAD = 20;
...
[/HIGHLIGHT]
Your missing a couple of things there, like error handling, which is required in the exercise as well. I don't know if that's the whole code but I don't see the a printDump() function, which prints the contents of the memory. I suggest you work on these details 'cause they are important for the completeness of the exercise.
In respect to the book, I agree with G_Morgan. It's a good book for beginners but a little too repetitive for someone who already know the basics. Anyway it's a good book since it have many advance topics as well, like networking, multithreading, GUIs, and more. The exercises are challenging and fun too, so I suggest you take them seriously.
#4
Posted 13 January 2008 - 02:13 PM
Thank u for ur comments.About Java books Is there any good books u know? and about "Java How to program" should i solve all the exersices to grapple the langauge?
Thank u again for ur valuable help.
Thank u again for ur valuable help.
#5
Posted 13 January 2008 - 02:38 PM
Use your judgement. If you have previous experience with an OOP language like Java I would skim across some of the most basic material and then pick some project I felt would expose me to the parts that I would need.
#6
Posted 13 January 2008 - 05:53 PM
From the "Java How to Program", the exercises I mostly do are those who have names on parentheses (eg. (Computer Simulator)). Those are the most interesting ones. I don't do them all of course, only those who I like the most. I tried to do all the exercises on the special sections, those are the most challenging ones.
As for other books in Java, I really don't know. I consider this one to be very complete, so I don't need anything else. Try to finish this one until the end, then pick other book more advance.
As for other books in Java, I really don't know. I consider this one to be very complete, so I don't need anything else. Try to finish this one until the end, then pick other book more advance.


Sign In
Create Account

Back to top









