-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathAssemblerTest.cs
114 lines (91 loc) · 2.58 KB
/
AssemblerTest.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using Dotx64Dbg;
using static Dotx64Dbg.Operands;
public class AssemblerTest
{
public AssemblerTest()
{
}
[Command("TestAssembler")]
public void BasicAssembly(string[] args)
{
nuint ip = Thread.Active != null ? Thread.Active.Nip : 0;
using (var asm = new Assembler(ip))
{
#if _X64_
// Create some assembly.
asm
.Mov(R9, R10)
.Shl(R9, Imm(1))
.Push(Rax)
.Pop(Rdx)
.Lea(Rsp, QwordPtr(Rsp, -8))
.Xchg(Rax, Rdx)
.Ret()
;
// Insert at the beginning.
asm.Cursor = null;
asm
.Push(R9)
.Pop(R10)
;
#else
asm
.Mov(Eax, Edx)
.Shl(Edx, Imm(1))
.Push(Eax)
.Pop(Edx)
.Lea(Esp, Ptr(Esp, -4))
.Xchg(Eax, Edx)
.Ret()
;
#endif
// Serialize the nodes into x86.
asm.Finalize();
// Write into process.
var bytes = asm.GetData();
var bytesWritten = Memory.Write(ip, bytes);
Console.WriteLine($"Wrote {bytesWritten} bytes");
UI.Disassembly.Update();
}
}
[Command("AssembleFromIP")]
public void EncodeIntoAssembler(string[] args)
{
var decoder = Decoder.Create();
nuint ip = Thread.Active.Nip;
var asm = new Assembler(ip);
var instr = decoder.Decode(ip);
asm.Emit(instr);
// Serialize the nodes into x86.
asm.Finalize();
// Write into process.
var bytes = asm.GetData();
var bytesWritten = Memory.Write(ip, bytes);
Console.WriteLine($"Wrote {bytesWritten} bytes");
UI.Disassembly.Update();
}
[Command("AssembleWithLabel")]
public void AssemblerWithLabels(string[] args)
{
nuint ip = Thread.Active.Nip;
var asm = new Assembler(ip);
var myLabel = asm.CreateLabel();
asm.Mov(Eax, Imm(12))
.Xor(Edx, Edx)
.Cmp(Eax, Edx)
.Jmp(myLabel)
.Nop()
.Nop()
.BindLabel(myLabel)
.Ret()
;
// Serialize the nodes into x86.
asm.Finalize();
// Write into process.
var bytes = asm.GetData();
var bytesWritten = Memory.Write(ip, bytes);
Console.WriteLine($"Wrote {bytesWritten} bytes");
UI.Disassembly.Update();
}
}