Exemple #1
0
        private string GetRType(Command command, bool suppressRdWarning = false)
        {
            var instruction = OperationCodes.Instructions[command.Tokens[0]];

            if (instruction.Instruction == "jr")
            {
                ValidateTokens(command.Tokens, 2);
                var jumpReg = ParseRegisterIndex(command.Tokens[1]);
                return String.Format("{0}{1}{2}",
                    instruction.OpCode,
                    DecToBinary(0, 3),
                    DecToBinary(jumpReg, 3),
                    DecToBinary(0, 6));
            }

            ValidateTokens(command.Tokens, 4);

            var rd = ParseRegisterIndex(command.Tokens[1]);
            WarnRd(rd, suppressRdWarning);
            var rs = ParseRegisterIndex(command.Tokens[2]);
            var rt = ParseRegisterIndex(command.Tokens[3]);

            return String.Format("{0}{1}{2}{3}{4}",
                 instruction.OpCode,
                 DecToBinary(rd, 3),
                 DecToBinary(rs, 3),
                 DecToBinary(rt, 3),
                 instruction.ALUCode);
        }
Exemple #2
0
        private string GetJType(Command command, List<Label> labels)
        {
            ValidateTokens(command.Tokens, 2);
            var instruction = OperationCodes.Instructions[command.Tokens[0]];

            var immediate = 0;

            var isLabel = true;
            try
            {
                immediate = ParseImmediate(command.Tokens[1]);
                isLabel = false;
            }
            catch (Exception e) { } // Do nothing; we have the flag

            if (isLabel)
            {
                var label = labels.FirstOrDefault(l => l.Name == command.Tokens[1]);
                if (label == null)
                {
                    throw new Exception(String.Format("Cannot find label {0}", command.Tokens[1]));
                }
                immediate = label.Index;
            }

            return String.Format("{0}{1}{2}",
                instruction.OpCode,
                DecToBinary(0, 6),
                DecToBinary(immediate, 6));
        }
Exemple #3
0
 private string GetNoOp(Command command)
 {
     // Use: Add $0 to $0 and store to $0 as our noop
     var noopCommand = new Command
     {
         Index = command.Index,
         Tokens = new string[] { "add", "$0", "$0", "$0" }
     };
     return GetRType(noopCommand, true);
 }
Exemple #4
0
        private string GetIType(Command command, List<Label> labels)
        {
            var instruction = OperationCodes.Instructions[command.Tokens[0]];
            var code = instruction.Instruction;

            var rd = ParseRegisterIndex(command.Tokens[1]);
            WarnRd(rd);
            var rs = 0;
            var immediate = 0;

            if (code == "beq" || code == "bne")
            {
                // Format: beq $rd, $rs, label
                ValidateTokens(command.Tokens, 4);
                rs = ParseRegisterIndex(command.Tokens[2]);

                var label = labels.FirstOrDefault(l => l.Name == command.Tokens[3]);
                if (label == null)
                {
                    throw new Exception("Cannot find label");
                }
                immediate = label.Index;
            }
            else if (code == "lw" || code == "sw")
            {
                // Format: lw $rd, offset($rs)
                ValidateTokens(command.Tokens, 3);

                var thirdVal = command.Tokens[2];
                ValidateParenthesis(thirdVal);

                var offsetAndRegister = thirdVal.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                immediate = ParseOffset(offsetAndRegister[0]);
                rs = ParseRegisterIndex(offsetAndRegister[1]);
            }
            else
            {
                // Format: XXXX $rd, $rs, imm
                ValidateTokens(command.Tokens, 4);
                rs = ParseRegisterIndex(command.Tokens[2]);
                immediate = ParseImmediate(command.Tokens[3]);
            }

            return String.Format("{0}{1}{2}{3}",
                 instruction.OpCode,
                 DecToBinary(rd, 3),
                 DecToBinary(rs, 3),
                 DecToBinary(immediate, 6));
        }