""" This program compiles BrainF**k programs to assembly. Copyright (C) 2004 John Bauman This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ import fileinput previouslabels = [] currentlabelnumber = 0 #putchar can be changed to _putch, and getchar can be changed to _getche, #getchar works with files, but requires an enter to be pressed putchar = "_putch" getchar = "_getche" print """ EXTERN ExitProcess IMPORT ExitProcess kernel32.dll EXTERN %s EXTERN %s SEGMENT .data USE32 array times 30000 dw 0 SEGMENT .code USE32 ..start""" % (putchar, getchar) print "mov EBX, array" for line in fileinput.input(): for char in line: if char == "+": print "inc DWORD [EBX]" if char == "-": print "dec DWORD [EBX]" if char == ">": print "add EBX, 4" if char == "<": print "sub EBX, 4" if char == "[": previouslabels.append(currentlabelnumber) print "while_%i:" % (currentlabelnumber) print "CMP DWORD [EBX], 0" print "JE NEAR wend_%i" % (currentlabelnumber) currentlabelnumber += 1 if char == "]": this_level = previouslabels.pop() print "JMP while_%i" % (this_level) print "wend_%i:" % (this_level) if char == ",": print "call %s" % (getchar) print "mov [EBX], EAX" print "cmp EAX, 3" print "JE NEAR endprogram" if char ==".": print "push DWORD [EBX]" print "call %s" % (putchar) print "endprogram:" print "push DWORD 0" print "call [ExitProcess]"