-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIB.cs
executable file
·68 lines (56 loc) · 1.98 KB
/
SIB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// This file is part of bugreport.
// Copyright (c) 2006-2009 The bugreport Developers.
// See AUTHORS.txt for details.
// Licensed under the GNU General Public License, Version 3 (GPLv3).
// See LICENSE.txt for details.
using System;
namespace bugreport
{
public static class SIB
{
private static readonly Opcode opcode = new X86Opcode();
public static RegisterName GetScaledRegister(Byte[] code)
{
if (!ModRM.HasSIB(code))
{
throw new InvalidOperationException(
"For ModRM that does not specify a SIB, usage of GetBaseRegister is invalid.");
}
var register = (RegisterName)GetIndexFor(code);
if (register == RegisterName.ESP)
{
register = RegisterName.None;
}
return register;
}
public static UInt32 GetScaler(Byte[] code)
{
if (!ModRM.HasSIB(code))
{
throw new InvalidOperationException(
"For ModRM that does not specify a SIB, usage of GetBaseRegister is invalid.");
}
var s = (Byte)((GetSIBFor(code) >> 6) & 3);
return (UInt32)Math.Pow(2, s);
}
public static RegisterName GetBaseRegister(Byte[] code)
{
if (!ModRM.HasSIB(code))
{
throw new InvalidOperationException(
"For ModRM that does not specify a SIB, usage of GetBaseRegister is invalid.");
}
var sib = GetSIBFor(code);
var register = (RegisterName)(sib & 7);
return register;
}
private static Byte GetIndexFor(Byte[] code)
{
return (Byte)((GetSIBFor(code) >> 3) & 7);
}
private static Byte GetSIBFor(Byte[] code)
{
return code[opcode.GetOpcodeLengthFor(code) + 1];
}
}
}