-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasmshell.py
73 lines (58 loc) · 1.75 KB
/
asmshell.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
#! /usr/bin/python3
import os
from subprocess import run
INPUT_FILE = 'input.asm'
# cuts off last extension
OUTPUT_FILE = ''.join(INPUT_FILE.split('.')[:-1])
MODES = ('16','32','64')
CURRENT_MODE = '[BITS 32]\n'
def changemode(userInput):
global CURRENT_MODE
try:
mode = userInput.split('=')[1].strip()
if mode in MODES:
CURRENT_MODE = "[BITS {}]\n".format(mode)
except IndexError:
pass # makes mode= to do nothing ( empty argument )
def disas(mnemo):
# Writes user input to file to comC:\Users\hlz\Desktop\asmshellpile it with nasm
f = open(INPUT_FILE, 'w')
f.write(CURRENT_MODE)
f.write(mnemo)
f.close()
# Compilation
# If successfull nasm will return binary in
# file called the same but without .asm
try:
ret = run(['nasm', INPUT_FILE])
os.remove(INPUT_FILE)
except FileNotFoundError:
print('Error: You need to have nasm installed to make asmshell work.')
return
if ret.returncode != 0:
return
# Compilation was successfull
# Reading nasm output and printing to user
f = open(OUTPUT_FILE, 'rb')
out = []
for byte in f.read():
# Collect binary as hex values without 0x prefix
# in 2 nibble format
out.append('{:02x}'.format(byte))
f.close()
os.remove(OUTPUT_FILE)
if len(out) == 0:
return
print('Code: ', ' '.join(out))
def main():
while True:
print('asmshell > ', end='')
userin = input().lower().replace(';','\n')
if userin == 'exit' or userin == 'quit':
return
if userin.startswith('mode'):
changemode(userin)
continue
disas(mnemo=userin)
if __name__ == '__main__':
main()