Skip to content
Advertisement

NumberFormatException java assembler

I have been working on an assembler in java for my VM and i dont know why this is happening because the string that is provided is a 1. I have tried trimming it and it is still throwing this error. Everything i found wont help with this issue the error is on line 117 where i set tmpa please help!
asm.java

package vm;

import java.util.Scanner;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
class asm {
    public static int pc = 0;
    public static void main(String[] args){
        String a = "";
        String filename = "prog.asm";
        String tmp = "";
        try
        {
            BufferedReader reader = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = reader.readLine()) != null)
            {
                tmp += line;
                tmp += " ";
            }
            reader.close();
        }
        catch (Exception e)
        {
            System.err.format("Exception occurred trying to read '%s'.", filename);
            e.printStackTrace();
            System.exit(1);
        }
        tmp = tmp.replace(", ", " ");
        tmp = tmp.replace(",", " ");
        tmp = tmp.replace(" ,", " ");
        String[] prog = tmp.split(" ");
        for(int i=0;i<prog.length;i++){
            System.out.println(
                "CURRENT POS: " + i + 
                ", " + prog[i]
            );
        }
        while(pc < prog.length){
            String d = prog[pc];
            if(!((prog[pc].substring(0, 1)).equals("$")) && !((prog[pc].substring(0, 1)).equals("r"))){
                System.out.println("FOUND INSTR " + prog[pc].substring(0, 1));
                a += " 0x";
            }
            if(prog[pc].equals("hlt")) {
                a += "00000000";
            }else if(prog[pc].equals("mov")){
                if(prog[pc].substring(0, 1).equals("r")){
                    a += "02";
                }else{
                    a += "01";
                }
            }else if(prog[pc].equals("add")){
                if(prog[pc].substring(0, 1).equals("r")){
                    a += "04";
                }else{
                    a += "03";
                }
            }
            else if(prog[pc].equals("sub")){
                if(prog[pc].substring(0, 1).equals("r")){
                    a += "06";
                }else{
                    a += "05";
                }
            }
            else if(prog[pc].equals("mul")){
                if(prog[pc].substring(0, 1).equals("r")){
                    a += "08";
                }else{
                    a += "07";
                }
            }
            else if(prog[pc].equals("div")){
                if(prog[pc].substring(0, 1).equals("r")){
                    a += "0A";
                }else{
                    a += "09";
                }
            }else if(prog[pc].equals("psh")){
                if(prog[pc].substring(0, 1).equals("r")){
                    a += "0C";
                }else{
                    a += "0B";
                }
            }else if(prog[pc].equals("psh")){
                if(prog[pc].substring(0, 1).equals("r")){
                    a += "0E";
                }else{
                    a += "0D";
                }
            }else if(prog[pc].substring(0, 1).equals("$")){
                int tmpa = Integer.parseInt(prog[pc + 1].substring(1, 2)) - 1;
                String tmpc="";
                char[] tmpd = prog[pc].toCharArray();
                for(int i=0;i<tmpd.length;i++) {
                    if(tmpd[i] == '$') {}
                    else if(tmpd[i] == '#') break;
                    else tmpc += tmpd[i];
                }
                a += tmpa;
                a += "0";
                if(tmpc.length() > 3) {
                    a += tmpc;
                }else if(tmpc.length() > 2) {
                    a += "0" + tmpc;
                }else if(tmpc.length() > 1) {
                    a += "00" + tmpc;
                }else if(tmpc.length() > 0) {
                    a += "000" + tmpc;
                }
                pc++;
            }
            else if(prog[pc].substring(0, 1).equals("r")) {
                System.out.println(prog[pc + 1].substring(1, 2));
                int tmpa = Integer.parseInt(prog[pc + 1].substring(1, 2).trim()); << Error
                int tmpb = Integer.parseInt(prog[pc].substring(1, 2).trim());
                a += tmpb;
                a += tmpa;
                a += "0000";
                pc++;
            }
            pc++;
        }
        System.out.println(a);
    }
}

prog.asm

mov $1#, r1
mov $1#, r2
add r1, r2
psh r1
hlt

Advertisement

Answer

When your code is ran on “hlt” line and examined substring contains an “l” letter which is not a digit, so the Integer.ParseInt cannot convert an “l” letter to an integer.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement