简体   繁体   English

充当汇编程序的Java程序(用于一种组合语言):while循环不会退出

[英]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. 相关的部分是.ENT行。 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). 第一遍处理.ENTs,但是它不适用于两个以上的参数(即,一个以上的逗号)。 It does work for two operands and less, though. 但是,它确实适用于两个或更少的操作数。 The code for the specific .ENT part is as follows: 特定的.ENT部分的代码如下:

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. read是一个字符串,其中包含输入文件的一行。 overSubstring is a method that will perform similarly to substring but it will return a whitespace character if it reads a null string. overSubstring是一种与子字符串类似的方法,但是如果读取空字符串,它将返回一个空格字符。

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. 我认为您正在无限读取相同的indexOfComma值。 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. 代替所有的startingIndexsubstring()业务,只需使用String#indexOf(String, int)而不是String#indexOf(String)来正确跳过您已经找到的前面的索引。

Get indexOfComma consistently. 始终获取indexOfComma Something like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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