Ejemplo n.º 1
0
        public ProgramSource GenerateProgramSource()
        {
            var ps = new ProgramSource();

            foreach (KeyValuePair <string, byte[]> data in _assembly.Data)
            {
                GenerateDataEntry(ps, data.Key, data.Value);
            }
            foreach (KeyValuePair <string, int> constant in _assembly.Constants)
            {
                ps.AddCodeLine("var ", constant.Key, " = 0x", constant.Value.ToString("X"));
            }

            ps.AddLine("goto _start;");

            foreach (X86AssemblySection section in _assembly.Sections)
            {
                ps.AddLine(_assembly.InternalNameOf(section.Name) + ":");

                if (section.InstructionCount == 0)
                {
                    ps.AddLine(";");
                }
                else
                {
                    foreach (X86Instruction instruction in section.Instructions)
                    {
                        InstructionCodeGenerator.Generate(ps, _assembly, instruction);
                    }
                }
            }

            return(ps);
        }
 private string TranslateLabel(LabelArgument arg)
 {
     if (X86Assembler.IsFunction(arg.Label))
     {
         return(arg.Label);
     }
     if (!_assembly.NameExists(arg.Label))
     {
         throw new LabelMissingException(arg.Line, arg.Label);
     }
     return(_assembly.InternalNameOf(arg.Label));
 }
Ejemplo n.º 3
0
        public static void JumpInstruction(ProgramSource ps, X86Assembly assembly, X86Instruction x86Instruction)
        {
            if (x86Instruction.OperandCount != 1)
            {
                throw new WrongParameterException(x86Instruction);
            }
            if (x86Instruction.Dst.Type != OperandTypes.Label || x86Instruction.Dst as LabelArgument == null)
            {
                throw new WrongParameterException(x86Instruction);
            }
            var    dst             = x86Instruction.Dst as LabelArgument;
            string instructionName = x86Instruction.Name;
            string realLabel       = dst.Label;

            if (!assembly.LabelExists(realLabel))
            {
                throw new LabelMissingException(x86Instruction.Line, realLabel);
            }
            string label = assembly.InternalNameOf(realLabel);

            switch (instructionName)
            {
            case "jmp":
                ps.AddCodeLine("goto ", label);
                return;

            case "je":
                ps.AddJump("ZF == 1", label);
                return;

            case "jne":
                ps.AddJump("ZF == 0", label);
                return;

            case "jg":
                ps.AddJump("ZF == 0 && SF == OF", label);
                return;

            case "jge":
                ps.AddJump("SF == OF", label);
                return;

            case "jl":
                ps.AddJump("SF != OF", label);
                return;

            case "jle":
                ps.AddJump("ZF == 1 || SF != OF", label);
                return;

            case "jz":
                ps.AddJump("ZF == 1", label);
                return;

            case "jnz":
                ps.AddJump("ZF == 0", label);
                return;

            case "loop":
                ps.AddCodeLine("vm.Ecx.Value--");
                ps.AddJump("vm.Ecx.Value != 0", label);
                return;
            }
            Debug.Assert(false, "Unknown jump instruction");
        }