Exemplo n.º 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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        private void Jmp(string[] operands)
        {
            bool invalidSyntax = false;

            if (operands.Length != 1)
            {
                invalidSyntax = true;
            }
            else if (AddressSyntax.TryParse(operands[0], _symbolTable, out var address))
            {
                _bytes.Add(((byte)Opcode.JumpEqual, (byte)0x0).Combine(), _currentLineNumber);
                _bytes.Add(address, _currentLineNumber);
            }
            else
            {
                invalidSyntax = true;
            }

            if (invalidSyntax)
            {
                throw new AssemblyException("Expected a single address.", _currentLineNumber);
            }
        }
Exemplo n.º 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);
        }