Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FasmException" /> class.
 /// </summary>
 public FasmException(FasmErrors errorCode, FasmResult condition, int lineNumber, string[] mnemonics) : base($"Failed to assemble FASM Mnemonics: Error name: {errorCode.ToString()}, Line Number: {lineNumber}, Result: {condition.ToString()}")
 {
     Result    = condition;
     ErrorCode = errorCode;
     Line      = lineNumber;
     Mnemonics = mnemonics;
 }
        /// <summary>
        /// Assembles the given mnemonics.
        /// </summary>
        /// <param name="mnemonics">The mnemonics to assemble; delimited by new line \n for each new instruction.</param>
        /// <param name="passLimit">The maximum number of passes to perform when assembling data.</param>
        /// <exception cref="FasmWrapperException">Your text to be assembled is too large to fit in the preallocated region for program text.</exception>
        /// <exception cref="FasmException">An error thrown by the native FASM compiler.</exception>
        public byte[] Assemble(string mnemonics, ushort passLimit = 100)
        {
            // Convert Text & Append
            byte[] mnemonicBytes = Encoding.ASCII.GetBytes(mnemonics + "\0");

            if (mnemonicBytes.Length > _textSize)
            {
                throw new FasmWrapperException($"Your supplied array of mnemonics to be assembled is too large ({mnemonicBytes.Length} > {_textSize} bytes)." +
                                               "Consider simplifying your code or creating a new Assembler with greater textSize.");
            }

            _processMemory.WriteRaw(_textAddress, mnemonicBytes);

            // Assemble and check result.
            FasmResult result = _assembleFunction(_textAddress, _resultAddress, (IntPtr)_resultSize, passLimit, IntPtr.Zero);

            //    As stated in FASMDLL.TXT, at the beginning of the block, the FASM_STATE structure will reside.
            //    It is defined in FASM.ASH. We read it here.

            _processMemory.Read(_resultAddress, out FasmState state);

            if (result == FasmResult.Ok)
            {
                byte[] assembledBytes = new byte[state.OutputLength];
                Marshal.Copy((IntPtr)state.OutputData, assembledBytes, 0, assembledBytes.Length);
                return(assembledBytes);
            }
            else
            {
                // TODO: Make this exception more detailed in time with FASMX64's development.

                /* For now, I still do not know if FASMX64 will ever plan to change the pointer size,
                 * so for now, I will opt to not get the line details and/or other more "complex" info. */

                string[] originalMnemonics = mnemonics.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                var      lineHeader        = state.GetLineHeader();

                throw new FasmException(state.ErrorCode, state.Condition, lineHeader.LineNumber, originalMnemonics);
            }
        }