-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm_cmds.py
141 lines (103 loc) · 3.68 KB
/
asm_cmds.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""This module defines and implements classes representing assembly commands. The ASMCommand object is the base class
for most ASM commands. Some commands inherit from ASMCommandMultiSize or JumpCommand instead.
"""
class ASMCommand:
"""Base class for a standard ASMCommand, like `add` or `imul`. This class is used for ASM commands which take
arguments of the same size.
"""
name = None
def __init__(self, dest=None, source=None, size=None):
self.dest = dest.asm_str(size) if dest else None
self.source = source.asm_str(size) if source else None
self.size = size
def __str__(self):
s = "\t" + self.name
if self.dest: s += " " + self.dest
if self.source: s += ", " + self.source
return s
class ASMCommandMultiSize:
""" Base class for an ASMCommand which takes arguments of different sizes. For example, `movsx` and `movzx` """
name = None
def __init__(self, dest, source, source_size, dest_size):
self.dest = dest.asm_str(source_size)
self.source = source.asm_str(dest_size)
self.source_size = source_size
self.dest_size = dest_size
def __str__(self):
s = "\t" + self.name
if self.dest: s += " " + self.dest
if self.source: s += ", " + self.source
return s
class JumpCommand:
""" Base class for jump commands """
name = None
def __init__(self, target):
self.target = target
def __str__(self):
s = "\t" + self.name + " " + self.target
return s
class Comment:
""" Class for comments """
def __init__(self, msg):
self.msg = msg
def __str__(self):
return "\t;; " + self.msg
class Label:
""" Class for label """
def __init__(self, label):
self.label = label
def __str__(self):
return self.label + ":"
class LabelFunc:
""" Class for start of function definition label """
def __init__(self, label):
self.label = label
def __str__(self):
return self.label + " PROC"
class LabelEndFunc:
""" Class for end of function label """
def __init__(self, label):
self.label = label
def __str__(self):
return self.label + " ENDP\n"
class Lea:
""" Class for lea command """
name = "lea"
def __init__(self, dest, source):
self.dest = dest
self.source = source
def __str__(self):
return ("\t" + self.name + " " + self.dest.asm_str(4) + ", "
"" + self.source.asm_str(0))
class Je(JumpCommand): name = "je"
class Jne(JumpCommand): name = "jne"
class Jg(JumpCommand): name = "jg"
class Jge(JumpCommand): name = "jge"
class Jl(JumpCommand): name = "jl"
class Jle(JumpCommand): name = "jle"
class Ja(JumpCommand): name = "ja"
class Jae(JumpCommand): name = "jae"
class Jb(JumpCommand): name = "jb"
class Jbe(JumpCommand): name = "jbe"
class Jmp(JumpCommand): name = "jmp"
class Movsx(ASMCommandMultiSize): name = "movsx"
class Movzx(ASMCommandMultiSize): name = "movzx"
class Mov(ASMCommand): name = "mov"
class BitwiseAnd(ASMCommand): name = "and"
class Add(ASMCommand): name = "add"
class Sub(ASMCommand): name = "sub"
class Neg(ASMCommand): name = "neg"
class Not(ASMCommand): name = "not"
class Div(ASMCommand): name = "div"
class Imul(ASMCommand): name = "imul"
class Idiv(ASMCommand): name = "idiv"
class Cdq(ASMCommand): name = "cdq"
class Cqo(ASMCommand): name = "cqo"
class Xor(ASMCommand): name = "xor"
class Cmp(ASMCommand): name = "cmp"
class Pop(ASMCommand): name = "pop"
class Push(ASMCommand): name = "push"
class Call(ASMCommand): name = "call"
class Ret(ASMCommand): name = "ret"
class Sar(ASMCommandMultiSize): name = "sar"
class Sal(ASMCommandMultiSize): name = "sal"