-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathX86Assembler.hpp
More file actions
66 lines (56 loc) · 1.94 KB
/
Copy pathX86Assembler.hpp
File metadata and controls
66 lines (56 loc) · 1.94 KB
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
#pragma once
enum x86regs {
XREG_EAX,
XREG_ECX,
XREG_ESI
};
class X86Assembler
{
public:
X86Assembler( unsigned char *buffer, unsigned int length )
: _buffer(buffer), _cursor(buffer), _length(length)
{
}
void * getCursor( ) { return _cursor; }
private:
void _writeBytes( unsigned char val1 ) {
*_cursor++ = val1;
}
void _writeBytes( unsigned char val1, unsigned char val2 ) {
*_cursor++ = val1;
*_cursor++ = val2;
}
void _writeBytes( unsigned char val1, unsigned char val2, unsigned char val3 ) {
*_cursor++ = val1;
*_cursor++ = val2;
*_cursor++ = val3;
}
void _writeUint( unsigned int val1 ) {
*((unsigned int*&)_cursor)++ = val1;
}
void _writeInt( int val1 ) {
*((int*&)_cursor)++ = val1;
}
protected:
unsigned char *_buffer;
unsigned char *_cursor;
unsigned int _length;
// INSTRUCTIONS //
public:
void aNOP( ) { _writeBytes(0x90); };
void aINT3( ) { _writeBytes(0xCC); };
void aRETN( ) { _writeBytes(0xC3); };
void aJMP_IMM( unsigned int imm ) { _writeBytes(0xE9); _writeInt((int)imm-(int)_cursor-4); }
void aCALL_IMM( unsigned int imm ) { _writeBytes(0xE8); _writeInt((int)imm-(int)_cursor-4); }
void aPUSH_EAX( ) { _writeBytes(0x50); };
void aMOV_EAX_ECX( ) { _writeBytes(0x89,0xc1); }
void aMOV_EAX_uIMM( unsigned int imm ) { _writeBytes(0xB8); _writeUint(imm); }
void aMOV_EAX_pESIoIMM( int off ) { _writeBytes(0x8b,0x86); _writeInt(off); }
void aMOV_ECX_pESIoIMM( int off ) { _writeBytes(0x8b,0x8e); _writeInt(off); }
void aMOV_pEAX_ECX( ) { _writeBytes(0x89,0x08); }
void aMOV_pESIoIMM_EAX( int off ) { _writeBytes(0x89,0x86); _writeInt(off); }
void aMOV_pESIoIMM_uIMM( int off, unsigned int imm ) { _writeBytes(0xC7,0x86); _writeInt(off); _writeUint(imm); }
void aADD_EAX_iIMM( int imm ) { _writeBytes(0x05); _writeInt(imm); }
void aADD_EAX_pESIoIMM( int off ) { _writeBytes(0x03,0x86); _writeInt(off); }
void aADD_pESIoIMM_iIMM( int off, int imm ) { _writeBytes(0x81,0x86); _writeInt(off); _writeInt(imm); }
};