Results 1 to 2 of 2

Thread: Machine langauge simulation

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    5

    Post Machine langauge simulation

    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();
    }

    }

    }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Machine langauge simulation

    Well, you'll have to test it for logical errors. Try every possible scenario to see if it works. And about the code optimization, I think you've done pretty well.

    Oh, and the book is great
    cheers
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width