Javascript流量控制故障

时间:2015-05-24 18:06:02

标签: javascript

更新:已解决!第116行(ERROR3)必须从' parseInst(rom [i])更改; ' ' rom [i]; '

我在Javascript中使用装配模拟器。由于某种原因,JUMP指令会混淆寄存器内容。以下程序(可以复制粘贴)增加寄存器' A' (0 - > 1)然后跳转到指令0.而不是' 1',寄存器的内容变为超过5000的值。我做错了什么?更新:添加了调试器捕获的三个错误("未捕获的RangeError:超出最大调用堆栈大小")。

var regA = 0;
var regB = 0;
var accu = 0;
var rom = [];
var instCount = 0;
var flag1 = 0;
var stopState = 0;

function eval() {
    var inst = document.getElementById("text_f").value;
    parseInst(inst);
};

function parseInst(instString) {

    if (instString.includes("LDA")) {         //ERROR1
        var strSplitA = instString.split(":");
        regA = parseInt(strSplitA[1]);
        document.getElementById("regA").innerHTML = regA;
        instCount++;
        document.getElementById("demo").innerHTML = "load register A: " + strSplitA[1]+"type of: "+typeof regA;

    } else if (instString.includes("LDB")) {
        var strSplitB = instString.split(":");
        document.getElementById("demo").innerHTML = "load register B: " + strSplitB[1];
        regB = parseInt(strSplitB[1]);
        document.getElementById("regB").innerHTML = regB;
        instCount++;

    } else if (instString == "ADD") {
        accu = regA + regB;
        document.getElementById("demo").innerHTML = "add " + regA + "+" + regB + "=" + accu;
        document.getElementById("accu").innerHTML = accu;
        instCount++;

    } else if (instString.includes("JMP")) {
        var jumpTo = instString.split(":");
        instCount = parseInt(jumpTo[1]);
        document.getElementById("demo").innerHTML = "jump to: " + instCount+" typeof: "+typeof instCount;
        document.getElementById("count").innerHTML = instCount;

        runStop(stopState,parseInt(jumpTo[1]));    //ERROR2


    } else if (instString == "CMP") {
        if (regA === regB) {
            flag1 = 1;
            instCount++;
            document.getElementById("flag1").innerHTML = 1;
            document.getElementById("demo").innerHTML = "flag1 set to 1";
         } else {
            flag1 = 0;
            instCount++;
            document.getElementById("flag1").innerHTML = 0;
            document.getElementById("demo").innerHTML = "flag1 set to 0";
        };


    } else if (instString.includes("INC")) {
        var incRegister = instString.split(":");
        switch (incRegister[1]) {
        case "A":
            regA++;
            document.getElementById("demo").innerHTML = "case A";
            document.getElementById("regA").innerHTML = regA;
            instCount++;
            break;
        case "B":
            regB++;
            document.getElementById("demo").innerHTML = "case B";
            document.getElementById("regB").innerHTML = regB;
            instCount++;
            break;
        default:
            document.getElementById("demo").innerHTML = "error: register name";
            break;
        }

    } else {
        document.getElementById("demo").innerHTML = "error: no instruction";

    };
};

function saveToRom() {
      var romString = document.getElementById("text_f").value;
    rom = romString.split(",");
    document.getElementById("rom").innerHTML = rom;
    document.getElementById("demo").innerHTML = "#debug:save to rom";
    reset();
};

function step() {
    parseInst(rom[instCount]);
    document.getElementById("count").innerHTML = instCount-1;
};

function run() {
    stopState = 0;
    document.getElementById("demo").innerHTML = "run";
    runStop(stopState,instCount);
    };

function stop(){
    stopState = 1;
    document.getElementById("demo").innerHTML = "stop";
    runStop(stopState,instCount);
};

function runStop(stopSt,instructionCount){

   if(stopSt == 0){
        for(var i=instructionCount;i<rom.length;i++){
        parseInst(rom[i]);                                     //ERROR3
        document.getElementById("demo").innerHTML = "#runStop(): stopState: "+stopState+" for loop length: " + rom.length;
       }
   } else {
        document.getElementById("demo").innerHTML = "#runStop(): stopState: "+stopState;  
   };
};


function reset() {
    document.getElementById("demo").innerHTML = "debug: reset";
    regA = 0;
    regB = 0;
    accu = 0;
    flag1 = 0;
    instCount = 0;
    document.getElementById("regA").innerHTML = regA;
    document.getElementById("regB").innerHTML = regB;
    document.getElementById("accu").innerHTML = accu;
    document.getElementById("count").innerHTML = instCount;
    document.getElementById("flag1").innerHTML = flag1;
};

The full source code with HTML on Github.

我提前感谢您的帮助!编辑:HTML代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>COMPU</title>
    <script type='text/javascript' src='comp3.1.js'></script>
    <link rel="stylesheet" type="text/css" href="stylesheet_comp.css"> 
</head>

<body>
    <input type="text" id="text_f" value=" " autofocus>
    <br><br>
    <div class="nav">
        <button onclick="eval()">EXEC</button>
        <button onclick="saveToRom()">SAVE</button>
        <button onclick="reset()">RST</button>
        <button onclick="step()">STEP</button>
        <button onclick="run()">RUN</button>
        <button id="stop" value=0 onclick="stop()">STOP</button>
    </div>
    <br>

    <div class="displays">
        DEBUG:
        <p id="demo">*debugging messages*</p>
        REG A:
        <p id="regA">0</p>
        REG B:
        <p id="regB">0</p>
        ACCU:
        <p id="accu">0</p>
        <br> ROM:
        <p id="rom"></p>
        INS COUNT:
        <p id="count">0</p>
        FLAG1:
        <p id="flag1">0</p>
<!--
        DEBUG2:
        <p id="dbg2"></p>
-->
    </div>
    INSTRUCTIONS:
    <ol>
        <li>ADD</li>
    </ol>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

更新:已解决!第116行(ERROR3)必须从'parseInst(rom [i])更改; '到'rom [i]; ”。