-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf2asm64.py
executable file
·85 lines (81 loc) · 1.42 KB
/
bf2asm64.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
#!/usr/bin/env python3
import sys
HEADER = '''\
.bss
mem:
.space 0x10000
.text
.globl main
main:
pushq %rbx
subq $32, %rsp
leaq mem(%rip), %rbx
'''
FOOTER = '''\
xorl %eax, %eax
addq $32, %rsp
popq %rbx
ret
'''
GT = '''\
addq $1, %rbx
'''
LT = '''\
subq $1, %rbx
'''
PLUS = '''\
addb $1, (%rbx)
'''
MINUS = '''\
subb $1, (%rbx)
'''
DOT = '''\
movzbl (%rbx), %ecx
call putchar
'''
COMMA = '''\
call getchar
movl $0, %ecx
cmpl $-1, %eax
cmove %ecx, %eax
movb %al, (%rbx)
'''
LBRACKET = '''\
movb (%rbx), %al
testb %al, %al
jz .END{lbl}
.BEGIN{lbl}:
'''
RBRACKET = '''\
movb (%rbx), %al
testb %al, %al
jnz .BEGIN{lbl}
.END{lbl}:
'''
COMMANDS = {'>': GT, '<': LT, '+': PLUS, '-': MINUS, '.': DOT, ',': COMMA,
'[': LBRACKET, ']': RBRACKET}
def bf2asm(code):
asm = [HEADER]
brackets = []
bracketi = 0
for c in code:
comm = COMMANDS.get(c)
if comm is None:
continue
if c == '[':
comm = comm.format(lbl=bracketi)
brackets.append(bracketi)
bracketi += 1
elif c == ']':
bracketj = brackets.pop()
comm = comm.format(lbl=bracketj)
asm.append(comm)
asm.append(FOOTER)
return ''.join(asm)
if __name__ == '__main__':
file = sys.stdin
if len(sys.argv) >= 2 and sys.argv[1] != '-':
file = open(sys.argv[1])
code = file.read()
asm = bf2asm(code)
sys.stdout.write(asm)