instructions.html

来自「MIPS Simulator assembly languge.」· HTML 代码 · 共 873 行 · 第 1/2 页

HTML
873
字号
<!--				File: Instructions.html	
----
----   Author:   Dr. D. Hunkins
----   Date:     October 2004
----   Modified: 6 June 2005
---->

<html>
<head>
<LINK HREF="pathsim.css" REL="stylesheet" type="text/css">
<title>Instructions</title>
<script language="JavaScript">

// An address as communicated between this and the Java applet is
// a logical address (i.e., a multiple of 4) and it is a
// hex string

   function valueOf(c) {
     if (("0" <= c) && (c <= "9")) return (c-"0");
     switch (c) {
       case "A" : 
	 case "a" : return 10;
       case "B" : 
       case "b" : return 11;
       case "C" : 
	 case "c" : return 12;
       case "D" : 
       case "d" : return 13;
       case "E" : 
       case "e" : return 14;
       case "F" : 
       case "f" : return 15;
     }
   }

   function hexStringToUnsigned(address) {
     var value = 0;
     for (var i=0; i<address.length; i++) 
       value = 16*value + valueOf(address.charAt(i));
     return value;
   }

   function unsignedToHexString(number, numberOfDigits) {
      var result  = "";
	var intermediateValue = number;
	var digitValue;
	var digitCount = 0;
      var temp;
	while (true){
	  digitValue = (intermediateValue % 16);
	  switch (digitValue){
	    case  0: result = "0" + result;
	 		 break;
	    case  1: result = "1" + result;
		       break;
	    case  2: result = "2" + result;
		       break;               
	    case  3: result = "3" + result;
		       break;
	    case  4: result = "4" + result;
			 break;
	    case  5: result = "5" + result;
			 break;
	    case  6: result = "6" + result;
			 break;
	    case  7: result = "7" + result;
			 break;
	    case  8: result = "8" + result;
			 break;
	    case  9: result = "9" + result;
			 break;
	    case 10: result = "A" + result;
			 break;
	    case 11: result = "B" + result;
			 break;
	    case 12: result = "C" + result;
			 break;
	    case 13: result = "D" + result;
			 break;
	    case 14: result = "E" + result;
			 break;
	    case 15: result = "F" + result;
			 break;
	  }

	  digitCount++;
	  temp = "" + (intermediateValue /= 16); 
        if (temp.indexOf(".") == 0)
           intermediateValue = 0;
        else
        if (temp.indexOf(".") == -1) {
           temp = temp.substr(0);
           intermediateValue = Number(temp);
        }
        else {
           temp = temp.substr(0, temp.indexOf("."));
           intermediateValue = Number(temp);
        }
	  if ((intermediateValue == 0) || (digitCount == numberOfDigits)) break;
	}

	while (result.length < numberOfDigits)
	   result = "0" + result;
	return result;
   }

   function isLetter(c) {
     return ( ("a" <= c) && (c <= "z") );
   }

   function isDecimalDigit(c) {
     return ( ("0" <= c) && (c <= "9") );
   }

   function isHexDigit(c) {
     return ( (("0" <= c) && (c <= "9")) ||
              (("A" <= c) && (c <= "F")) );
   }

   function isHexNumber(token, numberOfDigits) {
     token = token.toUpperCase();
     if (token.length == 0) return false;
     for (var i=0; i<token.length; i++)
       if (!isHexDigit(token.charAt(i))) return false;
     if (token.length > numberOfDigits) return false;
     return true;
   }

   function isRegister(token) {
     return ( (token == "$at") || (token == "$v0") || (token == "$v1") || (token == "$a0") || (token == "$a1") ||
              (token == "$a2") || (token == "$a3") || (token == "$t0") || (token == "$t1") || (token == "$t2") ||
              (token == "$t3") || (token == "$t4") || (token == "$t5") || (token == "$t6") || (token == "$t7") ||
              (token == "$s0") || (token == "$s1") || (token == "$s2") || (token == "$s3") || (token == "$s4") ||
              (token == "$s5") || (token == "$s6") || (token == "$s7") || (token == "$t8") || (token == "$t9") ||
              (token == "$k0") || (token == "$s1") || (token == "$gp") || (token == "$sp") || (token == "$fp") ||
              (token == "$ra") || (token == "$zero") );
   }

   function isConstantRegister(token) {
     return (token == "$zero");
   }

   function getRegisterNumber(token) {
     switch (token) {
        case "$at" : return "01";
                     break;
        case "$v0" : return "02";
                     break;
        case "$v1" : return "03";
                     break;
        case "$a0" : return "04";
                     break;
        case "$a1" : return "05";
                     break;
        case "$a2" : return "06";
                     break;
        case "$a3" : return "07";
                     break;
        case "$t0" : return "08";
                     break;
        case "$t1" : return "09";
                     break;
        case "$t2" : return "0A";
                     break;
        case "$t3" : return "0B";
                     break;
        case "$t4" : return "0C";
                     break;
        case "$t5" : return "0D";
                     break;
        case "$t6" : return "0E";
                     break;
        case "$t7" : return "0F";
                     break;
        case "$s0" : return "10";
                     break;
        case "$s1" : return "11";
                     break;
        case "$s2" : return "12";
                     break;
        case "$s3" : return "13";
                     break;
        case "$s4" : return "14";
                     break;
        case "$s5" : return "15";
                     break;
        case "$s6" : return "16";
                     break;
        case "$s7" : return "17";
                     break;
        case "$t8" : return "18";
                     break;
        case "$t9" : return "19";
                     break;
        case "$k0" : return "1A";
                     break;
        case "$k1" : return "1B";
                     break;
        case "$gp" : return "1C";
                     break;
        case "$sp" : return "1D";
                     break;
        case "$fp" : return "1E";
                     break;
        case "$ra" : return "1F";
                     break;
        case "$zero" : return "00";
                       break;
     }
  }

  function fourFieldInstruction(instructionComponents) {
    var temp = hexStringToUnsigned(instructionComponents[0]) & 60;
    var word = unsignedToHexString(Math.floor(temp/4), 1);
    temp =  (hexStringToUnsigned(instructionComponents[0]) &  3)*4;
    temp += Math.floor((hexStringToUnsigned(instructionComponents[1]) & 24)/8);
    word += unsignedToHexString(temp, 1);
    temp =  (hexStringToUnsigned(instructionComponents[1]) & 7)*2;
    temp += Math.floor((hexStringToUnsigned(instructionComponents[2]) & 16)/16);
    word += unsignedToHexString(temp, 1);
    temp =  hexStringToUnsigned(instructionComponents[2]) & 15;
    word += unsignedToHexString(temp, 1);
    temp =  hexStringToUnsigned(instructionComponents[3]);
    word += unsignedToHexString(temp, 4);
    return word;
  }

  function twoFieldInstruction(instructionComponents) {
    var temp = hexStringToUnsigned(instructionComponents[0]) & 60;
    var word = unsignedToHexString(Math.floor(temp/4), 1);
    temp =  (hexStringToUnsigned(instructionComponents[0]) &  3)*4;
    word += unsignedToHexString(temp, 1);
    temp =  hexStringToUnsigned(instructionComponents[1]);
    word += unsignedToHexString(temp, 6);
    return word;
  }

  function sixFieldInstruction(instructionComponents) {
     var temp = hexStringToUnsigned(instructionComponents[0]) & 60;
     var word = unsignedToHexString(Math.floor(temp/4), 1);

     temp =  (hexStringToUnsigned(instructionComponents[0]) &  3)*4;
     temp += Math.floor((hexStringToUnsigned(instructionComponents[1]) & 24)/8);
     word += unsignedToHexString(temp, 1);

     temp =  (hexStringToUnsigned(instructionComponents[1]) & 7)*2;
     temp += Math.floor((hexStringToUnsigned(instructionComponents[2]) & 16)/16);
     word += unsignedToHexString(temp, 1);

     temp =  hexStringToUnsigned(instructionComponents[2]) & 15;
     word += unsignedToHexString(temp, 1);

     temp  = Math.floor((hexStringToUnsigned(instructionComponents[3]) & 30)/2);
     word += unsignedToHexString(temp, 1);

     temp  = (hexStringToUnsigned(instructionComponents[3]) & 1)*8;
     temp += Math.floor((hexStringToUnsigned(instructionComponents[4]) & 28)/4);
     word += unsignedToHexString(temp, 1);

     temp  = (hexStringToUnsigned(instructionComponents[4]) & 3)*4;
     temp += Math.floor((hexStringToUnsigned(instructionComponents[5]) & 48)/16);
     word += unsignedToHexString(temp, 1);

     temp  = hexStringToUnsigned(instructionComponents[5]) & 15;
     word += unsignedToHexString(temp, 1);
     return word;
  }

   function immediateType() {
    try {
       var instructionComponents = new Array();
       var operation = this.tokenList.shift();
       switch (operation) {
          case "addi"  : instructionComponents[0] = "08";
                         break;
          case "andi"  : instructionComponents[0] = "0C";
                         break;
          case "ori"   : instructionComponents[0] = "0D";
                         break;
          case "xori"  : instructionComponents[0] = "0E";
                         break;
        }
        var token = this.tokenList.shift();
        if (!isRegister(token))
           throw new Error("Register Expected in first!!");
        if (isConstantRegister(token))
           throw new Error("$zero Register Cannot Be Changed!!");
        instructionComponents[2] = 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 in second!!");
        instructionComponents[1] = getRegisterNumber(token);
 
        token = this.tokenList.shift();
        if (token != ",")
           throw new Error("Comma Expected!!");

        token = this.tokenList.shift();
        if(!isHexNumber(token, 4))
          throw new Error("Immediate Value 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));
   }
    catch(ex) {
      throw ex;
    }
  }

   function immediateShift() {
    try {
       var instructionComponents = new Array();
       var operation = this.tokenList.shift();
       switch (operation) {
	    case "sll"  : instructionComponents[5] = "00";
                        break;
	    case "sra"  : instructionComponents[5] = "03";
                        break;
	    case "srl"  : instructionComponents[5] = "02";
                        break;
        }
        instructionComponents[0] = "00";
        instructionComponents[1] = "00";
        var 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[2] = getRegisterNumber(token);
 
        token = this.tokenList.shift();
        if (token != ",")
           throw new Error("Comma Expected!!");

        token = this.tokenList.shift();
        if(!isHexNumber(token, 2))
          throw new Error("Immediate Value of at most 2 Hex Digits Expected!!");
        if (hexStringToUnsigned(token) > 31)
          throw new Error("Shift amount must be in the range 0 to 31!!");
        instructionComponents[4] = token.toUpperCase();

        if (this.tokenList.length != 0)
           throw new Error("Extra text follows instruction!!");

        this.instructions.push(sixFieldInstruction(instructionComponents));
   }
    catch(ex) {
      throw ex;
    }
  }

  function variableShift() {
    try {
       var instructionComponents = new Array();
       var operation = this.tokenList.shift();

       switch (operation) {
	    case "sllv" : instructionComponents[5] = "04";
                        break;
	    case "srav" : instructionComponents[5] = "07";
                        break;
	    case "srlv" : instructionComponents[5] = "06";
                        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[2] = 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);
  
       if (this.tokenList.length != 0)
           throw new Error("Extra text follows instruction!!");

        this.instructions.push(sixFieldInstruction(instructionComponents));
     }
     catch (ex) {
        throw ex;
     }
  }

 function dataTransfer() {
    try {
      var instructionComponents = new Array();

      var operation = this.tokenList.shift();
      if (operation == "lw")
         instructionComponents[0] = "23";
      else
         instructionComponents[0] = "2B";
      token = this.tokenList.shift();
      if (!isRegister(token))
         throw new Error("Register Expected!!");
      if ((operation == "lw") && (isConstantRegister(token)))
         throw new Error("$zero Register Cannot Be Changed!!");
      instructionComponents[2] = getRegisterNumber(token);

      token = this.tokenList.shift();
      if (token != ",") 
         throw new Error("Comma Expected!!");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?