Пример #1
0
        private void Load(string[] operands)
        {
            if (operands.Length == 2)
            {
                if (RegisterSyntax.TryParse(operands[0], out var register) && AddressSyntax.TryParse(operands[1], _symbolTable, out var address))
                {
                    _bytes.Add(((byte)Opcode.ImmediateLoad, register.Index).Combine(), _currentLineNumber);
                    _bytes.Add(address, _currentLineNumber);
                    return;
                }

                if (RegisterSyntax.TryParse(operands[0], out register) && AddressSyntax.TryParse(operands[1], _symbolTable, out address, BracketExpectation.Present))
                {
                    _bytes.Add(((byte)Opcode.DirectLoad, register.Index).Combine(), _currentLineNumber);
                    _bytes.Add(address, _currentLineNumber);
                    return;
                }

                if (RegisterSyntax.TryParse(operands[0], out var register1) && RegisterSyntax.TryParse(operands[1], out var register2, BracketExpectation.Present))
                {
                    _bytes.Add(((byte)Opcode.IndirectLoad, (byte)0x0).Combine(), _currentLineNumber);
                    _bytes.Add((register1.Index, register2.Index).Combine(), _currentLineNumber);
                    return;
                }
            }

            throw new AssemblyException("Invalid operands for load instruction.", _currentLineNumber);
        }
Пример #2
0
        public static bool TryParse(string input, out RegisterSyntax registerSyntax, BracketExpectation bracketExpectation = BracketExpectation.NotPresent)
        {
            bool isSuccess;

            bool isRegister = Regex.IsMatch(input, @"^\[?R[0-9A-F]\]?$");

            bool isSurroundedByBrackets = input.StartsWith("[") && input.EndsWith("]");

            if (bracketExpectation == BracketExpectation.Present)
            {
                isSuccess = isRegister && isSurroundedByBrackets;
            }
            else
            {
                isSuccess = isRegister && !isSurroundedByBrackets;
            }

            if (isSuccess)
            {
                registerSyntax = new RegisterSyntax(input.Trim('[', ']'));
            }
            else
            {
                registerSyntax = null;
            }

            return(isSuccess);
        }
Пример #3
0
        private void Xor(string[] operands)
        {
            if (operands.Length == 3)
            {
                if (RegisterSyntax.TryParse(operands[0], out var register1) && RegisterSyntax.TryParse(operands[1], out var register2) && RegisterSyntax.TryParse(operands[2], out var register3))
                {
                    _bytes.Add(((byte)Opcode.Xor, register1.Index).Combine(), _currentLineNumber);
                    _bytes.Add((register2.Index, register3.Index).Combine(), _currentLineNumber);
                    return;
                }
            }

            throw new AssemblyException("Invalid operands for xor instruction.", _currentLineNumber);
        }
Пример #4
0
        private void JmpLE(string[] operands)
        {
            if (operands.Length == 2)
            {
                if (RegisterSyntax.TryParse(operands[0].Split('<', '=')[0], out var register) && AddressSyntax.TryParse(operands[1], _symbolTable, out var address))
                {
                    _bytes.Add(((byte)Opcode.JumpLessEqual, register.Index).Combine(), _currentLineNumber);
                    _bytes.Add(address, _currentLineNumber);
                    return;
                }
            }

            throw new AssemblyException("Invalid operands for jmple instruction.", _currentLineNumber);
        }
Пример #5
0
        private void Ror(string[] operands)
        {
            if (operands.Length == 2)
            {
                if (RegisterSyntax.TryParse(operands[0], out var register) && NumberSyntax.TryParse(operands[1], out byte number))
                {
                    if (number < 16)
                    {
                        _bytes.Add(((byte)Opcode.Ror, register.Index).Combine(), _currentLineNumber);
                        _bytes.Add(((byte)0x0, number).Combine(), _currentLineNumber);
                        return;
                    }
                    else
                    {
                        throw new AssemblyException("Number cannot be larger than 15.", _currentLineNumber);
                    }
                }
            }

            throw new AssemblyException("Invalid operands for ror instruction.", _currentLineNumber);
        }
Пример #6
0
        private void JmpEQ(string[] operands)
        {
            if (operands.Length == 2)
            {
                string[] registers = operands[0].Split('=');

                if (!RegisterSyntax.TryParse(registers[1], out var rightRegister) || rightRegister.Index != 0)
                {
                    throw new AssemblyException("Expected a comparison with R0.", _currentLineNumber);
                }

                if (RegisterSyntax.TryParse(registers[0], out var leftRegister) && AddressSyntax.TryParse(operands[1], _symbolTable, out var address))
                {
                    _bytes.Add(((byte)Opcode.JumpEqual, leftRegister.Index).Combine(), _currentLineNumber);
                    _bytes.Add(address, _currentLineNumber);
                    return;
                }
            }

            throw new AssemblyException("Invalid operands for jmpeq instruction.", _currentLineNumber);
        }