This abstract class represents a register for the x86 architecture
Exemplo n.º 1
0
		/// <summary>
		/// <see cref="DataInstruction"/> represents a data value encoded into the output image.
		/// </summary>
		/// <param name="indent">if set to <c>true</c>, the data instruction is indented in the ASM dump</param>
		/// <param name="label">The label.</param>
		/// <param name="reference">The reference.</param>
		/// <param name="name">The name.</param>
		/// <param name="parameters">The parameters.</param>
		/// <param name="rmMemory">The rm memory.</param>
		/// <param name="rmRegister">The rm register.</param>
		/// <param name="register">The register.</param>
		/// <param name="value">The value.</param>
		/// <param name="encoding">The encoding.</param>
		public DataInstruction (bool indent, string label, string reference, string name, string parameters, Memory rmMemory, Register rmRegister, Register register, object value, string [] encoding)
			: base (indent, label, reference, name, parameters, rmMemory, rmRegister, register, value, encoding)
		{
		}
Exemplo n.º 2
0
		/// <summary>
		/// Encodes the specified bits32.
		/// </summary>
		/// <param name="bits32">if set to <c>true</c> [bits32].</param>
		/// <param name="spareRegister">The spare register.</param>
		/// <param name="binaryWriter">The binary writer.</param>
		/// <returns></returns>
		public bool Encode (bool bits32, byte spareRegister, BinaryWriter binaryWriter)
		{
			byte value = (byte) (spareRegister * 8);

			if (bits32 != this.bits32Address && (this._base != null || this.index != null))
				throw new EngineException ("Wrong kind of address. (16bit/32bit mix not allowed)");

			if (bits32) {
				R32Type _base = (R32Type) this._base, index = (R32Type) this.index;
				Int32 displacement = this.displacement;
				bool displacementSet = this.displacementSet;
				byte scale = this.scale;

				if ((this._base == null && this.index != null && this.scale == 0)
						|| this.index == R32.ESP) {
					_base = (R32Type) this.index;
					index = (R32Type) this._base;
				}

				if (_base == null && index != null && scale == 1) {
					_base = index;
					scale = 0;
				}

				bool fixEBP = false;

				if (!displacementSet && _base == R32.EBP) // && index == null) || (_base == null && index == R32.EBP)))
				{
					fixEBP = true;
					displacementSet = true;
					displacement = 0;
				}

				bool shortDisplacement = false;

				if (displacementSet != false) {
					if (_base == null && index == null) {
						value += 5;

						binaryWriter.Write (value);

						binaryWriter.Write ((UInt32) displacement);

						return true;

					} else if (_base != null) {
						if (fixEBP || (displacement >= -0x80 && displacement <= 0x7f)) {
							shortDisplacement = true;
							value += 1 * 64; // 8bit

						} else {
							value += 2 * 64; // 32bit
						}
					}
				}

				if (_base != R32.ESP && index == null) {
					value += _base.Index;

				} else {
					value += 0x04;
				}

				binaryWriter.Write (value);

				if (_base == R32.ESP || index != null) {
					value = (byte) (scale * 64);

					if (index != null) {
						value += (byte) (index.Index * 8);

					} else {
						value += 0x20;
					}

					if (_base != null) {
						value += _base.Index;

					} else {
						value += 0x05;
					}

					binaryWriter.Write (value);

					if (!this.displacementSet && _base == null)
						binaryWriter.Write ((UInt32) 0);
				}

				if (displacementSet) {
					if (fixEBP || shortDisplacement) //(displacement >= -0x80 && displacement <= 0x7f))
						binaryWriter.Write ((byte) displacement);

					else
						binaryWriter.Write ((UInt32) displacement);
				}

			} else {
				byte rm;
				Int16 displacement = (Int16) this.displacement;

				if (this._base == R16.BX && this.index == R16.SI) {
					rm = 0;

				} else if (this._base == R16.BX && this.index == R16.DI) {
					rm = 1;

				} else if (this._base == R16.BP && this.index == R16.SI) {
					rm = 2;

				} else if (this._base == R16.BP && this.index == R16.DI) {
					rm = 3;

				} else if (this._base == R16.SI && this.index == null) {
					rm = 4;

				} else if (this._base == R16.DI && this.index == null) {
					rm = 5;

				} else if (this._base == R16.BP && this.index == null) {
					rm = 6;

					if (!displacementSet) {
						value += 0x46;
						binaryWriter.Write (value);
						binaryWriter.Write ((byte) 0);
						return true;
					}

				} else if (this._base == R16.BX && this.index == null) {
					rm = 7;

				} else {
					rm = 6;
				}

				value += rm;

				if (this._base != null || this.index != null) {
					if (displacementSet != false) {
						if (displacement >= -0x80 && displacement <= 0x7f) {
							value += 0x40; // 8Bit

						} else {
							value += 0x80; // 16Bit
						}
					}
				}

				binaryWriter.Write (value);

				if (displacementSet != false) {
					if (displacement >= -0x80 && displacement <= 0x7f) {
						binaryWriter.Write ((byte) displacement);

					} else {
						binaryWriter.Write ((UInt16) displacement);
					}
				}
			}

			return true;
		}