2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-03-28 02:57:34 -06:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/x64Analyzer.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2013-09-02 14:37:04 -06:00
|
|
|
bool DisassembleMov(const unsigned char *codePtr, InstructionInfo *info)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
|
|
|
unsigned const char *startCodePtr = codePtr;
|
|
|
|
u8 rex = 0;
|
2014-04-23 07:05:40 -06:00
|
|
|
u32 opcode;
|
|
|
|
int opcode_length;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2008-12-07 22:30:24 -07:00
|
|
|
//Check for regular prefix
|
2013-09-02 14:37:04 -06:00
|
|
|
info->operandSize = 4;
|
|
|
|
info->zeroExtend = false;
|
|
|
|
info->signExtend = false;
|
|
|
|
info->hasImmediate = false;
|
|
|
|
info->isMemoryWrite = false;
|
2014-04-23 07:05:40 -06:00
|
|
|
info->byteSwap = false;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
u8 modRMbyte = 0;
|
|
|
|
u8 sibByte = 0;
|
2013-04-15 14:28:55 -06:00
|
|
|
bool hasModRM = false;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
|
|
|
int displacementSize = 0;
|
|
|
|
|
|
|
|
if (*codePtr == 0x66)
|
|
|
|
{
|
2013-09-02 14:37:04 -06:00
|
|
|
info->operandSize = 2;
|
2008-12-07 22:30:24 -07:00
|
|
|
codePtr++;
|
2013-10-28 23:23:17 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
else if (*codePtr == 0x67)
|
|
|
|
{
|
|
|
|
codePtr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check for REX prefix
|
|
|
|
if ((*codePtr & 0xF0) == 0x40)
|
|
|
|
{
|
|
|
|
rex = *codePtr;
|
|
|
|
if (rex & 8) //REX.W
|
|
|
|
{
|
2013-09-02 14:37:04 -06:00
|
|
|
info->operandSize = 8;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
codePtr++;
|
|
|
|
}
|
|
|
|
|
2014-04-23 07:05:40 -06:00
|
|
|
opcode = *codePtr++;
|
|
|
|
opcode_length = 1;
|
|
|
|
if (opcode == 0x0F)
|
2013-04-15 14:28:55 -06:00
|
|
|
{
|
2014-04-23 07:05:40 -06:00
|
|
|
opcode = (opcode << 8) | *codePtr++;
|
|
|
|
opcode_length = 2;
|
|
|
|
if ((opcode & 0xFB) == 0x38)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-04-23 07:05:40 -06:00
|
|
|
opcode = (opcode << 8) | *codePtr++;
|
|
|
|
opcode_length = 3;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-23 07:05:40 -06:00
|
|
|
|
|
|
|
switch (opcode_length)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-04-23 07:05:40 -06:00
|
|
|
case 1:
|
|
|
|
if ((opcode & 0xF0) == 0x80 ||
|
|
|
|
((opcode & 0xF8) == 0xC0 && (opcode & 0x0E) != 0x02))
|
|
|
|
{
|
|
|
|
modRMbyte = *codePtr++;
|
|
|
|
hasModRM = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (((opcode & 0xF0) == 0x00 && (opcode & 0x0F) >= 0x04 && (opcode & 0x0D) != 0x0D) ||
|
|
|
|
((opcode & 0xF0) == 0xA0 && (opcode & 0x07) <= 0x02) ||
|
|
|
|
(opcode & 0xF0) == 0x30 ||
|
|
|
|
(opcode & 0xFF) == 0x77 ||
|
|
|
|
(opcode & 0xF0) == 0x80 ||
|
|
|
|
(opcode & 0xF8) == 0xC8)
|
|
|
|
{
|
|
|
|
// No mod R/M byte
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
modRMbyte = *codePtr++;
|
|
|
|
hasModRM = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// TODO: support more 3-byte opcode instructions
|
|
|
|
if ((opcode & 0xFE) == 0xF0)
|
|
|
|
{
|
|
|
|
modRMbyte = *codePtr++;
|
|
|
|
hasModRM = true;
|
|
|
|
}
|
|
|
|
break;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasModRM)
|
|
|
|
{
|
|
|
|
ModRM mrm(modRMbyte, rex);
|
2013-09-02 14:37:04 -06:00
|
|
|
info->regOperandReg = mrm.reg;
|
2008-12-07 22:30:24 -07:00
|
|
|
if (mrm.mod < 3)
|
|
|
|
{
|
|
|
|
if (mrm.rm == 4)
|
|
|
|
{
|
|
|
|
//SIB byte
|
|
|
|
sibByte = *codePtr++;
|
2013-09-02 14:37:04 -06:00
|
|
|
info->scaledReg = (sibByte >> 3) & 7;
|
|
|
|
info->otherReg = (sibByte & 7);
|
|
|
|
if (rex & 2) info->scaledReg += 8;
|
|
|
|
if (rex & 1) info->otherReg += 8;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-28 23:23:17 -06:00
|
|
|
//info->scaledReg =
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mrm.mod == 1 || mrm.mod == 2)
|
|
|
|
{
|
|
|
|
if (mrm.mod == 1)
|
|
|
|
displacementSize = 1;
|
|
|
|
else
|
|
|
|
displacementSize = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (displacementSize == 1)
|
2013-09-02 14:37:04 -06:00
|
|
|
info->displacement = (s32)(s8)*codePtr;
|
2008-12-07 22:30:24 -07:00
|
|
|
else
|
2014-04-23 07:05:40 -06:00
|
|
|
info->displacement = *((s32*)codePtr);
|
2008-12-07 22:30:24 -07:00
|
|
|
codePtr += displacementSize;
|
|
|
|
|
2014-04-23 07:05:40 -06:00
|
|
|
switch (opcode)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2013-09-02 14:37:04 -06:00
|
|
|
case 0xC6: // mem <- imm8
|
2014-04-23 07:05:40 -06:00
|
|
|
info->isMemoryWrite = true;
|
|
|
|
info->hasImmediate = true;
|
|
|
|
info->immediate = *codePtr;
|
|
|
|
codePtr++;
|
2013-09-02 14:37:04 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xC7: // mem <- imm16/32
|
2014-04-23 07:05:40 -06:00
|
|
|
info->isMemoryWrite = true;
|
|
|
|
switch (info->operandSize)
|
2013-09-02 14:37:04 -06:00
|
|
|
{
|
2014-04-23 07:05:40 -06:00
|
|
|
case 2:
|
|
|
|
info->hasImmediate = true;
|
|
|
|
info->immediate = *(u16*)codePtr;
|
|
|
|
codePtr += 2;
|
2013-09-24 20:47:01 -06:00
|
|
|
break;
|
|
|
|
|
2014-04-23 07:05:40 -06:00
|
|
|
case 4:
|
|
|
|
info->hasImmediate = true;
|
|
|
|
info->immediate = *(u32*)codePtr;
|
|
|
|
codePtr += 4;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
info->zeroExtend = true;
|
|
|
|
info->immediate = *(u32*)codePtr;
|
|
|
|
codePtr += 4;
|
2013-09-02 14:37:04 -06:00
|
|
|
break;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2014-04-23 07:05:40 -06:00
|
|
|
break;
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-04-23 07:05:40 -06:00
|
|
|
case 0x88: // mem <- r8
|
|
|
|
info->isMemoryWrite = true;
|
|
|
|
if (info->operandSize != 4)
|
2008-12-07 22:30:24 -07:00
|
|
|
{
|
2014-04-23 07:05:40 -06:00
|
|
|
return false;
|
2013-09-02 14:37:04 -06:00
|
|
|
}
|
2014-04-23 07:05:40 -06:00
|
|
|
info->operandSize = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x89: // mem <- r16/32/64
|
|
|
|
info->isMemoryWrite = true;
|
|
|
|
break;
|
2013-09-02 14:37:04 -06:00
|
|
|
|
|
|
|
case 0x8A: // r8 <- mem
|
2014-04-23 07:05:40 -06:00
|
|
|
if (info->operandSize != 4)
|
2013-09-02 14:37:04 -06:00
|
|
|
{
|
2014-04-23 07:05:40 -06:00
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2014-04-23 07:05:40 -06:00
|
|
|
info->operandSize = 1;
|
|
|
|
break;
|
2013-09-02 14:37:04 -06:00
|
|
|
|
|
|
|
case 0x8B: // r16/32/64 <- mem
|
2014-04-23 07:05:40 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0FB6: // movzx on byte
|
|
|
|
info->zeroExtend = true;
|
|
|
|
info->operandSize = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0FB7: // movzx on short
|
|
|
|
info->zeroExtend = true;
|
|
|
|
info->operandSize = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0FBE: // movsx on byte
|
|
|
|
info->signExtend = true;
|
|
|
|
info->operandSize = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0FBF: // movsx on short
|
|
|
|
info->signExtend = true;
|
|
|
|
info->operandSize = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0F38F0: // movbe read
|
|
|
|
info->byteSwap = true;
|
|
|
|
break;
|
2013-09-02 14:37:04 -06:00
|
|
|
|
2014-04-23 07:05:40 -06:00
|
|
|
case 0x0F38F1: // movbe write
|
|
|
|
info->byteSwap = true;
|
|
|
|
info->isMemoryWrite = true;
|
2013-09-02 14:37:04 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
2008-12-07 22:30:24 -07:00
|
|
|
}
|
2013-09-02 14:37:04 -06:00
|
|
|
info->instructionSize = (int)(codePtr - startCodePtr);
|
2008-12-07 22:30:24 -07:00
|
|
|
return true;
|
|
|
|
}
|