instructions.html
来自「MIPS Simulator assembly languge.」· HTML 代码 · 共 873 行 · 第 1/2 页
HTML
873 行
token = this.tokenList.shift();
if(!isHexNumber(token))
throw new Error("Offset Expected!!");
instructionComponents[3] = token.toUpperCase();
token = this.tokenList.shift();
if (token != "(")
throw new Error("( Expected!!");
token = this.tokenList.shift();
if (!isRegister(token))
throw new Error("Register Expected!!");
instructionComponents[1] = getRegisterNumber(token);
token = this.tokenList.shift();
if (token != ")")
throw new Error(") Expected!!");
if (this.tokenList.length != 0)
throw new Error("Extra text follows instruction!!");
this.instructions.push(fourFieldInstruction(instructionComponents));
}
catch (ex) {
throw ex;
}
}
function rType() {
try {
var instructionComponents = new Array();
var operation = this.tokenList.shift();
switch (operation) {
case "add" : instructionComponents[5] = "20";
break;
case "and" : instructionComponents[5] = "24";
break;
case "or" : instructionComponents[5] = "25";
break;
case "sub" : instructionComponents[5] = "22";
break;
case "slt" : instructionComponents[5] = "2A";
break;
case "nor" : instructionComponents[5] = "27";
break;
case "xor" : instructionComponents[5] = "26";
break;
}
instructionComponents[0] = "00";
instructionComponents[4] = "00";
token = this.tokenList.shift();
if (!isRegister(token))
throw new Error("Register Expected!!");
if (isConstantRegister(token))
throw new Error("$zero Register Cannot Be Changed!!");
instructionComponents[3] = getRegisterNumber(token);
token = this.tokenList.shift();
if (token != ",")
throw new Error("Comma Expected!!");
token = this.tokenList.shift();
if (!isRegister(token))
throw new Error("Register Expected!");
instructionComponents[1] = getRegisterNumber(token);
token = this.tokenList.shift();
if (token != ",")
throw new Error("Comma Expected!!");
token = this.tokenList.shift();
if (!isRegister(token))
throw new Error("Register Expected!");
instructionComponents[2] = getRegisterNumber(token);
if (this.tokenList.length != 0)
throw new Error("Extra text follows instruction!!");
this.instructions.push(sixFieldInstruction(instructionComponents));
}
catch (ex) {
throw ex;
}
}
function branch() {
var instructionComponents = new Array();
var operation = this.tokenList.shift();
switch (operation) {
case "beq" : instructionComponents[0] = "04";
break;
case "bne" : instructionComponents[0] = "05";
break;
}
var token = this.tokenList.shift();
if (!isRegister(token))
throw new Error("Register Expected!!");
instructionComponents[1] = getRegisterNumber(token);
token = this.tokenList.shift();
if (token != ",")
throw new Error("Comma Expected!!");
token = this.tokenList.shift();
if (!isRegister(token))
throw new Error("Register Expected!!");
instructionComponents[2] = getRegisterNumber(token);
token = this.tokenList.shift();
if (token != ",")
throw new Error("Comma Expected!!");
token = this.tokenList.shift();
if(!isHexNumber(token, 4))
throw new Error("Address Offset of at Most 4 Hex Digits Expected!!");
instructionComponents[3] = token.toUpperCase();
if (this.tokenList.length != 0)
throw new Error("Extra text follows instruction!!");
this.instructions.push(fourFieldInstruction(instructionComponents));
}
function jump() {
var instructionComponents = new Array();
instructionComponents[0] = "02";
this.tokenList.shift();
token = this.tokenList.shift();
if (!isHexNumber(token, 7))
throw new Error("Address of at Most 7 Hex Digits Expected!!");
instructionComponents[1] = token.toUpperCase();
this.instructions.push(twoFieldInstruction(instructionComponents));
}
function parse() {
try {
switch (this.tokenList[0]) {
case "add" :
case "and" :
case "or" :
case "sub" :
case "slt" :
case "nor" :
case "xor" : this.rType();
break;
case "sllv" :
case "srav" :
case "srlv" : this.variableShift();
break;
case "addi" :
case "andi" :
case "ori" :
case "xori" : this.immediateType();
break;
case "sll" :
case "sra" :
case "srl" : this.immediateShift();
break;
case "lw" :
case "sw" : this.dataTransfer();
break;
case "beq" :
case "bne" : this.branch();
break;
case "j" : this.jump();
break;
default :
throw new Error("A Valid Assembly Operation Expected!");
}
}
catch (ex) {
throw ex;
}
}
function extractTokens(assemblyCode) {
try {
var instruction = new Array();
var token;
var index;
while(this.tokenList.length > 0)
this.tokenList.pop();
for (var i=0; i<assemblyCode.length; i++)
instruction.push(assemblyCode.charAt(i));
while (instruction.length > 0) {
if (isLetter(instruction[0]) ||
isDecimalDigit(instruction[0])) {
token = "";
while ( (instruction.length > 0) &&
(isLetter(instruction[0]) ||
isDecimalDigit(instruction[0])) ) {
token += instruction.shift();
}
this.tokenList.push(token);
}
else if (instruction[0] == "$") {
if (isLetter(instruction[1])) {
token = "";
token += instruction.shift();
while ( (isLetter(instruction[0]) || isDecimalDigit(instruction[0])) &&
(instruction.length > 0) ) {
token += instruction.shift();
}
this.tokenList.push(token);
}
else
throw new Error("A Letter is Expected!");
}
else {
token = "";
switch (instruction[0]) {
case "(" : token += instruction.shift();
this.tokenList.push(token);
break;
case ")" : token += instruction.shift();
this.tokenList.push(token);
break;
case "," : token += instruction.shift();
this.tokenList.push(token);
break;
default :
throw new Error("Illegal character " + instruction[0]);
}
}
while ( (instruction.length > 0) &&
((instruction[0].charCodeAt(0) < 33) || (instruction[0].charCodeAt(0) > 176)) ) {
instruction.shift();
}
}
}
catch (ex) {
throw ex;
}
}
function assembleLoad() {
try {
for (var i=0; i < this.temp2.length; i++) {
var assemblyCode = this.temp2[i];
var index = assemblyCode.indexOf("#");
if (index >= 1)
assemblyCode = assemblyCode.substr(0, index);
assemblyCode = assemblyCode.toLowerCase();
this.extractTokens(assemblyCode);
this.parse();
}
for (var i=0; i < this.instructions.length; i++) {
var address = unsignedToHexString(i*4);
top.DataPath.setInstruction(this.instructions[i], address);
this.lastLoaded[i] = this.instructions[i];
}
top.DataPath.resetDataPath();
this.showInstructions("");
alert("Instructions Successfully Assembled and Loaded into Memory!");
}
catch (ex) {
alert("ERROR\n" +
ex.message + "\nin\n" +
assemblyCode +
"\nAssembly Terminated!");
}
}
function resetMachine() {
top.DataPath.initInstructions();
top.DataPath.resetDataPath();
top.DataInput.reLoad();
top.RegisterInput.reLoad();
while (this.instructions.length > 0)
this.instructions.pop();
for (var i=0; i<this.lastLoaded.length; i++) {
var address = unsignedToHexString(i*4, 3);
top.DataPath.setInstruction(this.lastLoaded[i], address);
this.instructions[i] = this.lastLoaded[i];
}
this.showInstructions("");
}
function load(code) {
var temp = new Array();
var index = 0;
if (code.length == 0) return;
if (top.getBrowserName() == "Microsoft Internet Explorer")
temp = code.split(/\n/); // For IE: split on newline
else { // For Netscape: replace \n with space\n and split on \n
var codeString = "";
for(i=0; i<code.length; i++)
if (code.charAt(i) == "\n")
codeString = codeString + " \n";
else
codeString = codeString + code.charAt(i)
temp = codeString.split(/\n/);
}
for (var i=0; i<temp.length; i++)
temp[i] = temp[i].substr(0, temp[i].length-1);
while (this.temp2.length > 0)
this.temp2.pop();
for (var i=0; i<temp.length; i++) {
index = 0;
while ((index < temp[i].length) && (temp[i].charAt(index) == " "))
index++;
temp[i] = temp[i].substr(index);
if ((temp[i].charAt(0) != '#') && (temp[i].length > 0))
this.temp2.push(temp[i].toUpperCase());
}
while(this.instructions.length > 0)
this.instructions.pop();
while(this.lastLoaded.length > 0)
this.lastLoaded.pop();
top.DataPath.initInstructions;
this.assembleLoad();
}
function returnedInstructionAccess(address) {
var addressString = new String(address);
if (addressString.length > 0)
codeList.showInstructions(addressString);
}
function showInstructions(address) {
var memoryIndex;
top.InstructionMemory.document.open();
top.InstructionMemory.document.write("<head>");
top.InstructionMemory.document.write("<LINK HREF='pathsim.css' REL='stylesheet' type='text/css'>");
top.InstructionMemory.document.write("</head><body>");
top.InstructionMemory.document.write("<div class='boxHead'>Instruction Memory</div>");
top.InstructionMemory.document.write("<div class ='code'>");
if (address.length == 0) {
for (var i=0; i<this.instructions.length; i++) {
if (this.instructions[i] != "00000000")
top.InstructionMemory.document.write(unsignedToHexString(i*4, 3) + ":" + this.instructions[i] + "<br>");
}
}
else {
memoryIndex = hexStringToUnsigned(address)/4;
for (var i=0; i<memoryIndex; i++) {
if (this.instructions[i] != "00000000")
top.InstructionMemory.document.write(unsignedToHexString(i*4, 3) + ":" + this.instructions[i] + "<br>");
}
top.InstructionMemory.document.write("</div>");
top.InstructionMemory.document.write("<div class ='readSelected'>");
top.InstructionMemory.document.write(unsignedToHexString(memoryIndex*4, 3) + ":" + this.instructions[memoryIndex] + "<br>");
top.InstructionMemory.document.write("</div>");
top.InstructionMemory.document.write("<div class ='code'>");
for (var i=memoryIndex+1; i<this.instructions.length; i++) {
if (this.instructions[i] != "00000000")
top.InstructionMemory.document.write(unsignedToHexString(i*4, 3) + ":" + this.instructions[i] + "<br>");
}
}
top.InstructionMemory.document.write("</div></body>");
top.InstructionMemory.document.close();
}
function defineHandlers() {
var codeArea = document.instructions.elements[0];
var loadButton = document.instructions.elements[1];
var resetButton = document.instructions.elements[2];
loadButton.onclick = function() {codeList.load(codeArea.value)};
resetButton.onclick = function() {codeList.resetMachine()};
}
function CodeList() {
this.instructions = new Array();
this.lastLoaded = new Array();
this.temp2 = new Array();
this.tokenList = new Array();
this.resetMachine = resetMachine;
this.load = load;
this.assembleLoad = assembleLoad;
this.extractTokens = extractTokens;
this.parse = parse;
this.immediateType = immediateType;
this.immediateShift = immediateShift;
this.variableShift = variableShift;
this.dataTransfer = dataTransfer;
this.rType = rType;
this.branch = branch;
this.jump = jump;
this.showInstructions = showInstructions;
}
</script>
</head>
<body>
<form name="instructions">
<table>
<tr>
<td><div class="boxHead">Instruction Input</div></td>
</tr>
<tr>
<td><textarea name="InstructionCode" align="left" rows="12" columns="12"></textarea></td>
</tr>
<tr>
<td>
<input type="button" name="assemble" value="Assemble">
<input type="button" name="reset" value="Reset Machine">
</td>
</table>
</form>
<script>
var codeList = new CodeList();
defineHandlers();
</script>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?