public ASMDecoderResult DecodeASM(IEnumerable <byte> bytes, uint pc, bool littleEndian = true, bool useRegAliases = false) { uint[] uintArray = ASMValueHelper.GetUintArrayFromBytes(bytes, littleEndian); return(DecodeASM(uintArray, pc, useRegAliases)); }
public byte[] UpdateBlockReferences(byte[] bytes, uint pc, bool littleEndian, IEnumerable <BlockMove> blockMoves) { int byteCount = bytes.Length; if (byteCount < 4) { return(bytes); } byte[] resultBytes = new byte[byteCount]; int startIndex = 0; byte[] asmBytes = bytes; if (byteCount > 4) { uint offsetBytes = pc % 4; if (offsetBytes != 0) { uint skipBytes = 4 - offsetBytes; pc = pc + skipBytes; startIndex += (int)skipBytes; int length = (int)(bytes.Length - skipBytes); byte[] newBytes = new byte[length]; Array.Copy(bytes, skipBytes, newBytes, 0, length); Array.Copy(bytes, 0, resultBytes, 0, startIndex); asmBytes = newBytes; } } uint[] instructions = ASMValueHelper.GetUintArrayFromBytes(asmBytes, littleEndian); int numInstructions = instructions.Length; uint[] newInstructions = new uint[numInstructions]; uint[] regLuiValues = new uint[32]; int[] regLuiIndexes = new int[32]; for (int regNum = 0; regNum < 32; regNum++) { regLuiIndexes[regNum] = -1; } for (int index = 0; index < numInstructions; index++) { uint uBinaryLine = instructions[index]; uint opcode = (uBinaryLine >> 26); uint newInstruction = uBinaryLine; // Is unconditional jump literal command J or JAL if ((opcode & 0x3E) == 0x02) // ((opcode & 0b111110) == 0b000010) { uint jumpAddress = (((uBinaryLine & 0x03FFFFFFU) << 2) | (pc & 0xF0000000U)); foreach (BlockMove blockMove in blockMoves) { if ((jumpAddress >= blockMove.Location) && (jumpAddress < blockMove.EndLocation)) { uint newJumpAddress = (uint)(jumpAddress + blockMove.Offset); newInstruction = (opcode << 26) | ((newJumpAddress >> 2) & 0x03FFFFFFU); } } } // Is Load Upper Immediate (LUI) EncodingFormat encFormat = Decoder.FormatHelper.FindFormatByBinary(uBinaryLine); if (encFormat.Command == "lui") { int regNum = (int)(uBinaryLine >> encFormat.RegisterPositions[0] & encFormat.RegisterIncludeMasks[0]); uint immediate = (uint)(uBinaryLine >> encFormat.ImmediatePositions[0] & encFormat.ImmediateIncludeMasks[0]); regLuiValues[regNum] = immediate; regLuiIndexes[regNum] = index; } // Is Load or Store command, or ADDI, ADDIU, or ORI if (IsLoadCommand(encFormat.Command) || IsStoreCommand(encFormat.Command) || ((encFormat.Command == "addi") || (encFormat.Command == "addiu") || (encFormat.Command == "ori"))) { int regNum = (int)(uBinaryLine >> encFormat.RegisterPositions[1] & encFormat.RegisterIncludeMasks[1]); if (regLuiIndexes[regNum] >= 0) { short offset = ASMValueHelper.UnsignedShortToSignedShort((ushort)(uBinaryLine & 0xffff)); uint targetAddress = (uint)((regLuiValues[regNum] << 16) + offset) | (0x80000000U); foreach (BlockMove blockMove in blockMoves) { if ((targetAddress >= blockMove.Location) && (targetAddress < blockMove.EndLocation)) { uint newTargetAddress = (uint)(targetAddress + blockMove.Offset); uint newLuiValue = (ushort)(newTargetAddress >> 16); ushort newOffset = (ushort)(newTargetAddress & 0xffff); if (encFormat.Command != "ori") { newLuiValue += (uint)((newOffset >= 0x8000) ? 1 : 0); } newInstruction = ((uBinaryLine & 0xFFFF0000U) | newOffset); // Modify the LUI if necessary if (newLuiValue != regLuiValues[regNum]) { uint newLuiInstruction = (newInstructions[regLuiIndexes[regNum]] & 0xFFFF0000U) | newLuiValue; newInstructions[regLuiIndexes[regNum]] = newLuiInstruction; byte[] newLuiInstructionBytes = ASMValueHelper.ConvertUIntToBytes(newLuiInstruction, littleEndian); Array.Copy(newLuiInstructionBytes, 0, resultBytes, (regLuiIndexes[regNum] * 4) + startIndex, 4); } } } } } newInstructions[index] = newInstruction; byte[] newBytes = ASMValueHelper.ConvertUIntToBytes(newInstruction, littleEndian); Array.Copy(newBytes, 0, resultBytes, (index * 4) + startIndex, 4); } for (int index = (numInstructions * 4) + startIndex; index < byteCount; index++) { resultBytes[index] = bytes[index]; } return(resultBytes); }