Ejemplo n.º 1
0
        private void ExecuteInstruction(Instruction inst)
        {
            BigInteger operand1;
            BigInteger operand2;

            switch (inst.Opcode)
            {
            case Opcode.Add:
                operand1 = ReadValue(inst.A);
                operand2 = ReadValue(inst.B);
                WriteValue(operand1 + operand2, inst.C);
                break;

            case Opcode.Multiply:
                operand1 = ReadValue(inst.A);
                operand2 = ReadValue(inst.B);
                WriteValue(operand1 * operand2, inst.C);
                break;

            case Opcode.Input:
                operand1 = InputProvider.GetInput();
                WriteValue(operand1, inst.A);
                break;

            case Opcode.Output:
                operand1 = ReadValue(inst.A);
                foreach (var sink in OutputSinks)
                {
                    sink.SendOutput(operand1);
                }
                ResultSink.SendOutput(operand1);
                break;

            case Opcode.JumpIfTrue:
                operand1 = ReadValue(inst.A);
                if (operand1 != 0)
                {
                    IP = (int)ReadValue(inst.B) - inst.Length;
                }
                break;

            case Opcode.JumpIfFalse:
                operand1 = ReadValue(inst.A);
                if (operand1 == 0)
                {
                    IP = (int)ReadValue(inst.B) - inst.Length;
                }
                break;

            case Opcode.LessThan:
                operand1 = ReadValue(inst.A);
                operand2 = ReadValue(inst.B);

                if (operand1 < operand2)
                {
                    WriteValue(1, inst.C);
                }
                else
                {
                    WriteValue(0, inst.C);
                }
                break;

            case Opcode.Equals:
                operand1 = ReadValue(inst.A);
                operand2 = ReadValue(inst.B);

                if (operand1 == operand2)
                {
                    WriteValue(1, inst.C);
                }
                else
                {
                    WriteValue(0, inst.C);
                }
                break;

            case Opcode.AddRelativeOffset:
                operand1      = ReadValue(inst.A);
                RelativeBase += (int)operand1;
                break;

            case Opcode.Halt:
                IsHalted = true;
                break;
            }

            IP += inst.Length;
        }
Ejemplo n.º 2
0
        private void ExecuteInstruction(Instruction inst)
        {
            int operand1;
            int operand2;

            switch (inst.Opcode)
            {
            case Opcode.Add:
                operand1 = ReadValue(inst.A);
                operand2 = ReadValue(inst.B);
                Program[inst.C.Value] = operand1 + operand2;
                IP += inst.Length;
                break;

            case Opcode.Multiply:
                operand1 = ReadValue(inst.A);
                operand2 = ReadValue(inst.B);
                Program[inst.C.Value] = operand1 * operand2;
                IP += inst.Length;
                break;

            case Opcode.Input:
                operand1 = InputProvider.GetInput();
                Program[inst.A.Value] = operand1;
                IP += inst.Length;
                break;

            case Opcode.Output:
                operand1 = ReadValue(inst.A);
                foreach (var sink in OutputSinks)
                {
                    sink.SendOutput(operand1);
                }
                ResultSink.SendOutput(operand1);
                IP += inst.Length;
                break;

            case Opcode.JumpIfTrue:
                operand1 = ReadValue(inst.A);
                if (operand1 != 0)
                {
                    IP = ReadValue(inst.B);
                }
                else
                {
                    IP += inst.Length;
                }
                break;

            case Opcode.JumpIfFalse:
                operand1 = ReadValue(inst.A);
                if (operand1 == 0)
                {
                    IP = ReadValue(inst.B);
                }
                else
                {
                    IP += inst.Length;
                }
                break;

            case Opcode.LessThan:
                operand1 = ReadValue(inst.A);
                operand2 = ReadValue(inst.B);

                if (operand1 < operand2)
                {
                    Program[inst.C.Value] = 1;
                }
                else
                {
                    Program[inst.C.Value] = 0;
                }
                IP += inst.Length;
                break;

            case Opcode.Equals:
                operand1 = ReadValue(inst.A);
                operand2 = ReadValue(inst.B);

                if (operand1 == operand2)
                {
                    Program[inst.C.Value] = 1;
                }
                else
                {
                    Program[inst.C.Value] = 0;
                }
                IP += inst.Length;
                break;
            }
        }