0

我有一个问题,我已经尝试解决好几天了。我想知道是否可以让文本变成整数。所以每次我在我的 textarea("ALC") Load 中写入,然后在 textarea("MLC") 001 上写入。最后还包括 1-15 到二进制例如 Load #1 将显示 001 0 00001

<html>
<head>
<center><font size ="24"> Simple Assembler </font></center>
<script type="text/javascript">
    var Load = "001";
    var Store = "010";
    var Add = "011";
    var Sub = "100";
    var Equal = "101";
    var Jump = "110";
    var Halt = "111";
    var # = "1";


</script>
</head>
<body>

<form name="AssemblyLanguagecode" action="" method="">
<textarea Id="ALC" style="resize:none;width:35%;height:35%;margin-left:15%" value="">Insert Assembly Language Code</textarea>
<textarea Id="MLC" style="resize:none;width:35%;height:35%;" ReadOnly="True">Machine Language Code will be displayed here</textarea><br  />
<p align="center"><input type="button" value="Assemble" onclick="ALCtoMLC()";" /></p>
</form>


<script type= "text/javascript">

function ALCtoMLC() {   
    var x = document.getElementById("ALC").value;
    x = parseInt(x);
    var bin = x.toString(2);



document.getElementById("MLC").innerHTML = bin;
}

</script>

</body>
</html>
4

4 回答 4

2

我想我明白你想做什么。您想使用您在“ALC”中键入的内容作为值的键。在这种情况下,您希望使用 javascript 对象并将指令分配为键,并将二进制文件分配给值。如

var instruction_set = {
    "Load" : "001",
    "Store" : "010",
    "Add" : "011",
    "Sub" : "100",
    "Equal" : "101",
    "Jump" : "110",
    "Halt" : "111"
}

function ALCtoMLC() {
    var x = document.getElementById("ALC").value;
    x = instruction_set[x];
}
于 2013-03-29T04:12:42.450 回答
0

更新:

试试这个:

<html>
<head>
<center><font size ="24"> Simple Assembler </font></center>
<script type="text/javascript">
    var Load = "001";
    var Store = "010";
    var Add = "011";
    var Sub = "100";
    var Equal = "101";
    var Jump = "110";
    var Halt = "111";
    var # = "1";



</script>
</head>
<body>

<form name="AssemblyLanguagecode" action="" method="">
<textarea Id="ALC" style="resize:none;width:35%;height:35%;margin-left:15%" value="">Insert Assembly Language Code</textarea>
<textarea Id="MLC" style="resize:none;width:35%;height:35%;" ReadOnly="True">Machine Language Code will be displayed here</textarea><br  />
<p align="center"><input type="button" value="Assemble" onclick="ALCtoMLC();" /></p>
</form>


<script type= "text/javascript">
var Dict = { 'Load':"001",'Store':"010"}; //example Instruction set

function ALCtoMLC() {   
    var x = document.getElementById("ALC").value;
    var instrType = '';
    for (var instr in Dict){
        var ind = x.indexOf(instr);
        if( ind > -1){
            instrType = instrType + Dict[instr];
            x = x.replace(instr,'');
        }
    }
    console.log(instrType, "::", x);
    x = parseInt(x);
    var bin = x.toString(2);
    bin = instrType + bin; 
    document.getElementById("MLC").innerHTML = bin;
}

</script>

</body>
</html>
于 2013-03-29T04:40:06.090 回答
0

我会这样做:

var opcodes = {
    Load:  1,
    Store: 2,
    Add:   3,
    Sub:   4,
    Equal: 5,
    Jump:  6,
    Halt:  7
};

var assemblyTextarea = document.querySelector("#assembly");
var machineTextarea = document.querySelector("#machine");

document.querySelector("#assemble").addEventListener("click", function () {
    var instruction = assemblyTextarea.value.split(" ");
    var operand =+ instruction[1].slice(1);
    var opcode = instruction[0];

    var code = opcodes[opcode] * 16 + operand;
    var bits = ("0000000" + code.toString(2)).slice(-8);

    machineTextarea.value = bits;
}, false);

在此处查看演示:http: //jsfiddle.net/fs5mb/1/

输入的格式应如下:Load #15

于 2013-03-29T04:45:16.723 回答
0

假设您有办法获得令牌。那么你的函数应该是这样的

var tokens = getTokens( document.getElementById("ALC").value ) ;
var vocabulary = { "Load" : "001" , " ... " } ;
var output = []
var i = 0;
var tokensLength = tokens.length;
for ( ; i < tokensLength; i++){
     var token = tokens[i];
     if ( isNaN(token) && typeof(vocabulary[token]) != "undefined" ){
           output.push( vocabulary[token] );

     }else if ( !isNaN(token) ){
          output.push( Number(token).toString(2) );
     }else{
          console.log(["error : unknown token ", token]);
     }
}

document.getElementById("MLC").value = output.join(" "); 

我在问题中看到 Load 转换为 0010 而不是 001,所以我只需修改词汇表。

解释 :

  • 我假设您有办法将输入拆分为令牌。(我仍然不清楚 ALC 语法)。
  • 令牌数组将包含例如["Load","#","15", "Load","#","16"]等。
  • 然后我循环标记。
  • 如果令牌是数字 - 我将其转换为二进制字符串。
  • 如果令牌可按词汇表翻译 - 我将其切换为二进制表示。
  • 否则我打印一个错误。

注意:如果输出应该用“0”填充 - 即使问题中没有指定,我也会使用"0000".substring(n.length) + n

于 2013-03-29T04:45:28.080 回答