Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- BF_PROGRAM = ""
- ASM_PROGRAM = ""
- ASM_INCLUDE_COMMENTS = True
- ASM_INCLUDE_IO = True
- if (len(BF_PROGRAM) == 0):
- #no program when ran...
- #wait for one from stdin (either pipe or keyboard)
- #stop accepting lines after reads a line with just a newline
- try:
- while 1:
- try:
- line = sys.stdin.readline()
- except KeyboardInterrupt:
- break
- if (line.find("\n") != -1):
- if (line.index("\n") == 0):
- break
- BF_PROGRAM += line
- except:
- print "NO BF PROGRAM GIVEN!"
- exit(0)
- ASM_PROGRAM += "global _main\n"
- ASM_PROGRAM += "extern _putch\n"
- ASM_PROGRAM += "extern _getch\n\n"
- ASM_PROGRAM += "section .text\n_main:\n\n"
- ASM_PROGRAM += "mov eax, 0\n"
- ASM_PROGRAM += "mov ebx, 0\n"
- ASM_PROGRAM += "mov ecx, 0\n"
- ASM_PROGRAM += "mov edx, 0\n\n"
- if (ASM_INCLUDE_COMMENTS):
- ASM_PROGRAM += "; MEMORY STARTS AT EAX\n"
- ASM_PROGRAM += "mov eax, [MEMORYCELLS]\n\n" #memory
- #BF_PUTCHAR function (get char from EDX)
- ASM_PROGRAM += "BF_PUTCHAR:\n"
- if (ASM_INCLUDE_IO):
- ASM_PROGRAM += "push eax\n"
- ASM_PROGRAM += "push ebx\n"
- ASM_PROGRAM += "push ecx\n"
- ASM_PROGRAM += "push edx\n"
- ASM_PROGRAM += "pushf\n"
- ASM_PROGRAM += "push "
- ASM_PROGRAM += "call _putch\n"
- ASM_PROGRAM += "add esp, 4\n"
- ASM_PROGRAM += "popf\n"
- ASM_PROGRAM += "pop edx\n"
- ASM_PROGRAM += "pop ecx\n"
- ASM_PROGRAM += "pop ebx\n"
- ASM_PROGRAM += "pop eax\n"
- ASM_PROGRAM += "ret\n\n"
- #BF_GETCHAR function (return char to EDX)
- ASM_PROGRAM += "BF_GETCHAR:\n"
- if (ASM_INCLUDE_IO):
- ASM_PROGRAM += "push eax\n"
- ASM_PROGRAM += "push ebx\n"
- ASM_PROGRAM += "push ecx\n"
- ASM_PROGRAM += "pushf\n"
- ASM_PROGRAM += "call _getch\n"
- ASM_PROGRAM += "mov edx, eax\n"
- ASM_PROGRAM += "add esp, 4\n"
- ASM_PROGRAM += "popf\n"
- ASM_PROGRAM += "pop ecx\n"
- ASM_PROGRAM += "pop ebx\n"
- ASM_PROGRAM += "pop eax\n"
- else:
- ASM_PROGRAM += "mov edx, 255\n"
- ASM_PROGRAM += "ret\n\n"
- lookup = {
- '>' : "inc eax\n\n",
- '<' : "dec eax\n\n",
- '+' : "mov ebx, [eax]\ninc ebx\nmov [eax], ebx\n\n",
- '-' : "mov ebx, [eax]\ndec ebx\nmov [eax], ebx\n\n",
- '.' : "mov edx, [eax]\ncall BF_PUTCHAR\n\n",
- ',' : "call BF_GETCHAR\nmov [eax], edx\n\n",
- '[' : "BF_WHILE_$(RAND):\nmov ebx, [eax]\ncmp ebx, 0\nje BF_WHILE_END_$(RAND)\n\n",
- ']' : "jmp BF_WHILE_$(LRAND)\nBF_WHILE_END_$(LRAND):\n\n",
- }
- namestack = []
- namerng = 0
- if (ASM_INCLUDE_COMMENTS):
- ASM_PROGRAM += "; ACTUAL BF PROGRAM STARTS HERE\n\n"
- for c in BF_PROGRAM:
- if (lookup.has_key(c)):
- result = lookup[c]
- if (result.find("$(RAND)") != -1):
- namestack += [namerng]
- result = result.replace("$(RAND)", str(namerng))
- namerng += 1
- if (result.find("$(LRAND)") != -1):
- result = result.replace("$(LRAND)", str(namestack.pop()))
- if (ASM_INCLUDE_COMMENTS):
- ASM_PROGRAM += "; COMMAND '%c'\n" % c
- ASM_PROGRAM += result
- ASM_PROGRAM += "\nMEMORYCELLS:\ndb 0\n"
- print "; ========================================\n"
- print "; CREATE WITH BF2ASM.PY\n"
- print "; ========================================\n"
- print ASM_PROGRAM
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement