简体   繁体   中英

Java program that acts as an assembler (for a made up language): will not quit while loop

Basically, it is a two pass assembler and I am working on implementing entry points into a given assembly file. The format of the command is as follows:

Prog     .ORIG
         .ENT    some,entry,point
some     LD      R0,entry
entry    LD      R1,point
point    .FILL   #42
         .END    some

The relevant part is the .ENT line. That is the line the assembler is getting hung up on.

The first pass takes care of handling .ENTs but it will not work for anything more than two arguments (that is, more than one comma). It does work for two operands and less, though. The code for the specific .ENT part is as follows:

String comma = ",";
String entry = "";
String[] tempEntryArray = new String[2];
int indexOfComma = read.indexOf(comma);
int startingIndex = 17;
int numOperands = 1;
while (indexOfComma != -1) {
    if ((indexOfComma-startingIndex) == 0) {
        return "An operand must precede the comma.";
    }
    if (numOperands > 4) {
        return "The .ENT psuedo-op on line " + lineCounter
               + " has more than 4 operands.";
    }
    entry = overSubstring(read, startingIndex, indexOfComma);
    if (entry.contains(" ")) {
        return "The operand \"" + entry + "\" on line " 
               + lineCounter + " has a space in it.";
    }
    if (entry.length() > 6) {
        return "The operand \"" + entry + "\" on line "
               + lineCounter + " is longer than 6 characters.";
    }
    machineTables.externalSymbolTable.put(entry, tempEntryArray);
    entry = read.substring(indexOfComma + 1);
    startingIndex = indexOfComma + 1;
    indexOfComma = entry.indexOf(comma);
    if (indexOfComma != -1) {
        indexOfComma += (startingIndex - 1);
    }
    numOperands++;
}
entry = overSubstring(read, startingIndex, read.length());
if (entry.contains(" ")) {
    return "The operand \"" + entry + "\" on line " 
           + lineCounter + " has a space in it.";
}
if (entry.length() > 6) {
    return "The operand \"" + entry + "\" on line "
           + lineCounter + " is longer than 6 characters.";
}
machineTables.externalSymbolTable.put(entry, tempEntryArray);

read is a String containing one line of the input file. overSubstring is a method that will perform similarly to substring but it will return a whitespace character if it reads a null string.

I am sorry for the huge block of code, and I know the error messages can be done a lot better, but for now I am concerned with this particular code hanging the assembler whenever there are more than two operands (more than one comma).

I would appreciate it very much if someone could help me with this problem.

Thanks.

I think that you're reading the same indexOfComma value infinitely. Instead of all that startingIndex and substring() business, just use String#indexOf(String, int) instead of String#indexOf(String) to properly skip the preceding indices you've already found.

Get indexOfComma consistently. Something like this:

int indexOfComma = -1;
int numOperands = 1;
while ((indexOfComma = read.indexOf(comma, indexOfComma+1)) != -1) {
    // snip...
    machineTables.externalSymbolTable.put(entry, tempEntryArray);
    numOperands++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM