OISC_Compiler.OISCAsm.Assemble C# (CSharp) Method

Assemble() public method

public Assemble ( ) : byte[]
return byte[]
        public byte[] Assemble()
        {
            ICollection<AddressableInstruction> sourceTree = ParseSource();

            AddressableInstruction lastInstruction = sourceTree.Last();
            long binarySize = lastInstruction.Address.BinaryAddress + lastInstruction.BinaryLength;

            // Create an array of the required size to hold the binary data.
            // Size is taken from the final binary address used.
            byte[] binary = new byte[binarySize];

            // Assemble the binary for each instruction and store it in the array.
            foreach (AddressableInstruction instruction in sourceTree)
            {
                byte[] instructionBinary = instruction.AssembleBinary();
                Array.Copy(instructionBinary, 0, binary, instruction.Address.BinaryAddress, instruction.BinaryLength);
            }

            return binary;
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                DisplayUsage();
                return;
            }

            String filePath = args[0];
            if (!File.Exists(filePath))
            {
                Console.WriteLine("{0} not found", filePath);
                return;
            }

            // Load the source.
            Console.WriteLine(Strings.AppName);
            Console.WriteLine();
            Console.WriteLine("Loading source ({0})", filePath);
            String[] sourceCodeLines = File.ReadAllLines(filePath);

            // Compile source.
            OISCAsm compiler = new OISCAsm(sourceCodeLines);
            byte[] compiledBytes = compiler.Assemble();

            // Output compiled binary to file.
            String outputFilePath = GenerateOutputFilename(filePath);
            File.WriteAllBytes(outputFilePath, compiledBytes);
        }
All Usage Examples Of OISC_Compiler.OISCAsm::Assemble