-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
148 lines (134 loc) · 3.16 KB
/
generate.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
142
143
144
145
146
147
148
import random
import os
import string
NUM_LINES = 30
# these are not the complete
# set of x86_64 registers
registers = [
'rax',
'rbx',
'rcx',
'rdx',
'rsi',
'rdi',
'rbp',
'rsp'
]
# instruction name
# number of operands
# register position
# register is optional
instructions = [
{
'name': 'mov',
'num_operands': 2,
'register_pos_opt': 1,
'register_pos_req': 0
},
{
'name': 'and',
'num_operands': 2,
'register_pos_opt': 1,
'register_pos_req': 0
},
{
'name': 'or',
'num_operands': 2,
'register_pos_opt': 1,
'register_pos_req': 0
},
{
'name': 'xor',
'num_operands': 2,
'register_pos_opt': 1,
'register_pos_req': 0
},
{
'name': 'add',
'num_operands': 2,
'register_pos_opt': 1,
'register_pos_req': 0
},
{
'name': 'sub',
'num_operands': 2,
'register_pos_opt': 1,
'register_pos_req': 0
},
{
'name': 'inc',
'num_operands': 1,
'register_pos_opt': None,
'register_pos_req': 0
},
{
'name': 'dec',
'num_operands': 1,
'register_pos_opt': None,
'register_pos_req': 0
},
{
'name': 'syscall',
'num_operands': 0,
'register_pos_opt': None,
'register_pos_req': None
}
]
# if anyone has any ideas for
# extra system calls, let me know
syscalls = [
{
'address': '1',
'name': 'write',
'num_arguments': 4,
},
{
'address': '60',
'name': 'exit',
'num_arguments': 0,
}
]
# choose a number between 1 (write) and 60 (exit), and add the address of our "hello world"
# choices = [1, 60, 'message', 'messagelen']
choices = list(range(1, 61))
choices.append('message')
choices.append('messagelen')
def choose_register_positon(instruction):
choice = [None, None]
req = instruction['register_pos_req']
if req is not None:
choice[req] = True
opt = instruction['register_pos_opt']
if opt is not None:
choice[opt] = bool(random.randint(0,1))
return choice
def call_with_operands(instruction):
register_position = choose_register_positon(instruction)
parts = [instruction['name']]
if register_position[0]:
parts.append(random.choice(registers))
if register_position[1]:
parts[1] += ','
parts.append(random.choice(registers))
elif register_position[1] is False:
parts[1] += ','
parts.append(str(random.choice(choices)))
return parts
def generate_line():
return ' '.join(call_with_operands(random.choice(instructions)))
def generate_asm():
return '''
global _start
section .text
_start:
{}
section .data
message: db "Hello World", 10
messagelen: equ $ - message
'''.format('\n'.join([generate_line() for i in range(0, NUM_LINES)]))
'''
filename = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(FILENAME_LEN)) + '.asm'
with open(filename, 'w') as f:
print('writing to {}'.format(filename))
f.write(asm)
'''