Closed Thread
Results 1 to 5 of 5

Thread: implemning assembly interpreter in Java

  1. #1
    halona is offline Newbie
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    implemning assembly interpreter in Java

    I have this code in assembly

    1024=15; //memory location
    1025=24; //memory location
    1023=34; //memory location
    SUB R1,R1,0
    LD R1,R1,1024
    SUB R2,R2,0
    LD R2,R2,1025
    SUB R3,R3,0
    LD R3,R3,1023
    MUL R1,R2,0
    DIV R1,R3,0


    and I want to write an interpreter for it in Java..

    I started in the code .. determine the syntax structure.. but couldn't write the code in Jave .. where and what..

    so .. I need some one to guide me to what should I do .. to finish this interpreter.

    this is what I wrote so far

    Code:
    grammar Expr;
    
    @header {
    import java.util.HashMap;
    }
    
    @members {
    /** Map variable name to Integer object holding value */
    int[] regi=new int[32];
    int[] mem=new int[65536];
    int counter=0;
    
    
    
    
    HashMap memory = new HashMap();
    int firstreg;
    int regvalue;
    int intvalue;
    int	regValue(String x)	{
    	int reg;
    
    	if(x.compareTo(new String("R0")) == 0)
    		reg = 0;
    	else if (x.compareTo(new String("R1")) == 0)
    		reg = 1;
    	else if (x.compareTo(new String("R2")) == 0)
    		reg = 2;
    	else if (x.compareTo(new String("R3")) == 0)
    		reg = 3;
    	else if (x.compareTo(new String("R4")) == 0)
    		reg = 4;
    	else if (x.compareTo(new String("R5")) == 0)
    		reg = 5;
    	else if (x.compareTo(new String("R6")) == 0)
    		reg = 6;
    	else if (x.compareTo(new String("R7")) == 0)
    		reg = 7;
    	else if (x.compareTo(new String("R8")) == 0)
    		reg = 8;
    	else if (x.compareTo(new String("R9")) == 0)
    		reg = 9;
    	else if (x.compareTo(new String("R10")) == 0)
    		reg = 10;
    	else if (x.compareTo(new String("R11")) == 0)
    		reg = 11;
    	else if (x.compareTo(new String("R12")) == 0)
    		reg = 12;
    	else if (x.compareTo(new String("R13")) == 0)
    		reg = 13;
    	else if (x.compareTo(new String("R14")) == 0)
    		reg = 14;
    	else if (x.compareTo(new String("R15")) == 0)
    		reg = 15;
    	else if (x.compareTo(new String("R16")) == 0)
    		reg = 16;
    	else if (x.compareTo(new String("R17")) == 0)
    		reg = 17;
    	else if (x.compareTo(new String("R18")) == 0)
    		reg = 18;
    	else if (x.compareTo(new String("R19")) == 0)
    		reg = 19;
    	else if (x.compareTo(new String("R20")) == 0)
    		reg = 20;
    	else if (x.compareTo(new String("R21")) == 0)
    		reg = 21;
    	else if (x.compareTo(new String("R22")) == 0)
    		reg = 22;
    	else if (x.compareTo(new String("R23")) == 0)
    		reg = 23;
    	else if (x.compareTo(new String("R24")) == 0)
    		reg = 24;
    	else if (x.compareTo(new String("R25")) == 0)
    		reg = 25;
    	else if (x.compareTo(new String("R26")) == 0)
    		reg = 26;
    	else if (x.compareTo(new String("R27")) == 0)
    		reg = 27;
    	else if (x.compareTo(new String("R28")) == 0)
    		reg = 28;
    	else if (x.compareTo(new String("R29")) == 0)
    		reg = 29;
    	else if (x.compareTo(new String("R30")) == 0)
    		reg = 30;
    	else if (x.compareTo(new String("R31")) == 0)
    		reg = 31;
    	else reg = -1;
    	return reg;
    }
    }
    
    prog:   stat+ ;
                    
    stat:   expr NEWLINE {System.out.println($expr.value);}
        |   ID '=' expr NEWLINE
            {memory.put($ID.text, new Integer($expr.value));}
        |   NEWLINE
        | LD REG COMMA regInExpr {System.out.println($LD.text + " " + regValue(new String($REG.text))+ " " + regvalue + " " + intvalue); }
        | DIV REG COMMA regInExpr {System.out.println($DIV.text + " " + regValue(new String($REG.text))+ " " + regvalue + " " + intvalue); } 
       | SUB REG COMMA regInExpr {System.out.println($SUB.text + " " + regValue(new String($REG.text))+ " " + regvalue + " " + intvalue); }
       | MUL REG COMMA regInExpr {System.out.println($MUL.text + " " + regValue(new String($REG.text))+ " " + regvalue + " " + intvalue); }
       | ST REG COMMA regInExpr {System.out.println($ST.text + " " + regValue(new String($REG.text))+ " " + regvalue + " " + intvalue); }
        ;
    expr returns [int value]
        :   e=multExpr {$value = $e.value;}
            (   MUL e=multExpr {$value = $e.value;}
            |   SUB e=multExpr {$value -= $e.value;}
            |   DIV  e=multExpr {$value = $e.value;}
            |   LD  e=multExpr {$value += $e.value;}
            |   ST  e=multExpr {$value = $e.value;}  
            )*
        ;
    
    regInExpr : REG COMMA INT { 
    		if (counter==0)
    		{mem [1024]=15;
    		mem[1025]=24;
    		mem[1023]=34;
    		regi[0]=0;
    		regi[1]=1;
    		regi[2]=2;
    		regi[3]=3;
    		regi[4]=4;
    		counter +=counter;
    		regvalue = regValue(new String($REG.text));intvalue =   Integer.parseInt($INT.text);
    		//System.out.println(mem[1024],mem[1025],mem[1023]);
    		 }
    		else
    
    regvalue = regValue(new String($REG.text));intvalue =   Integer.parseInt($INT.text); };
    
    multExpr returns [int value]
        :   e=atom {$value = $e.value;} ('*' e=atom {$value *= $e.value;})*
        ; 
    
    atom returns [int value]
        :   INT {$value = Integer.parseInt($INT.text);}
        |   ID
            {
            Integer v = (Integer)memory.get($ID.text);
            if ( v!=null ) $value = v.intValue();
            else System.err.println("undefined variable "+$ID.text);
            }
        |   '(' expr ')' {$value = $expr.value;}
        ;
    
    COMMA : ',';
    LD    : 'L''D';
    MUL   :'M''U''L'; 
    DIV   :'D''I''V';
    SUB   : 'S''U''B';
    ST    :  'S''T';
    REG   : 'R'('0'|'1'|'2') ('0'..'9') | 'R''3' ('0'|'1'|'2') | 'R'('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9');
    ID    :   ('a'..'z'|'A'..'Z')+ ;
    INT   :   '0'..'9'+ ;
    NEWLINE:'\r'? '\n' ;
    WS  :   (' '|'\t')+ {skip();} ;

    I am using ANTLR with this..
    any one know please help me

    thank u
    Last edited by Jaan; 10-26-2009 at 04:53 AM. Reason: Please use code tags when you are posting your codes !

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: implemning assembly interpreter in Java

    You are going about this entirely the wrong way. And from the looks of it you don't have the experience or knowledge required to write an assembly interpreter.

  4. #3
    halona is offline Newbie
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Re: implemning assembly interpreter in Java

    Quote Originally Posted by meth0dz_ View Post
    You are going about this entirely the wrong way. And from the looks of it you don't have the experience or knowledge required to write an assembly interpreter.

    yes I am..

    I don't know what should I do.. I need larde help and guide as well

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: implemning assembly interpreter in Java

    Well, you'll need a memory model, variables to store registers, a parser, and logic to implement each possible statement correctly. There's probably a half dozen other things that I'm missing. That's just the high-level view. You can't just start coding, for something this big.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    halona is offline Newbie
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Re: implemning assembly interpreter in Java

    Quote Originally Posted by WingedPanther View Post
    Well, you'll need a memory model, variables to store registers, a parser, and logic to implement each possible statement correctly. There's probably a half dozen other things that I'm missing. That's just the high-level view. You can't just start coding, for something this big.
    well..
    for the memorey and the registers I have created an array to hold the value
    with their sizes.


    Code:
    int[] regi=new int[32];
    int[] mem=new int[65536];

    iam not going to create the full compiler for the language..
    it is just for the following code
    Code:
    1024=15; //memory location
    1025=24; //memory location
    1023=34; //memory location
    SUB R1,R1,0
    LD R1,R1,1024
    SUB R2,R2,0
    LD R2,R2,1025
    SUB R3,R3,0
    LD R3,R3,1023
    MUL R1,R2,0
    DIV R1,R3,0

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. brainf*ck interpreter in php
    By __ak in forum PHP Development
    Replies: 1
    Last Post: 03-17-2011, 12:02 AM
  2. Brainfuck interpreter!
    By mebob in forum C and C++
    Replies: 8
    Last Post: 11-07-2010, 12:32 PM
  3. Whitespace Interpreter
    By MeTh0Dz in forum Classes and Code Snippets
    Replies: 2
    Last Post: 02-02-2010, 08:42 AM
  4. Python Interpreter
    By Sionofdarkness in forum Python
    Replies: 2
    Last Post: 01-06-2007, 03:29 AM
  5. T Interpreter 1.1
    By Kernel in forum Software Development Tools
    Replies: 0
    Last Post: 10-03-2006, 09:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

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