Advertisement
captmicro

BrainFuck -> x86 Assembly

Oct 12th, 2012
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. import sys
  2.  
  3. BF_PROGRAM = ""
  4. ASM_PROGRAM = ""
  5. ASM_INCLUDE_COMMENTS = True
  6. ASM_INCLUDE_IO = True
  7.  
  8. if (len(BF_PROGRAM) == 0):
  9.     #no program when ran...
  10.     #wait for one from stdin (either pipe or keyboard)
  11.     #stop accepting lines after reads a line with just a newline
  12.     try:
  13.         while 1:
  14.             try:
  15.                 line = sys.stdin.readline()
  16.             except KeyboardInterrupt:
  17.                 break
  18.             if (line.find("\n") != -1):
  19.                 if (line.index("\n") == 0):
  20.                     break
  21.             BF_PROGRAM += line
  22.     except:
  23.         print "NO BF PROGRAM GIVEN!"
  24.         exit(0)
  25.  
  26. ASM_PROGRAM += "global _main\n"
  27. ASM_PROGRAM += "extern  _putch\n"
  28. ASM_PROGRAM += "extern  _getch\n\n"
  29. ASM_PROGRAM += "section .text\n_main:\n\n"
  30.  
  31. ASM_PROGRAM += "mov eax, 0\n"
  32. ASM_PROGRAM += "mov ebx, 0\n"
  33. ASM_PROGRAM += "mov ecx, 0\n"
  34. ASM_PROGRAM += "mov edx, 0\n\n"
  35.  
  36. if (ASM_INCLUDE_COMMENTS):
  37.     ASM_PROGRAM += "; MEMORY STARTS AT EAX\n"
  38. ASM_PROGRAM += "mov eax, [MEMORYCELLS]\n\n" #memory
  39.  
  40. #BF_PUTCHAR function (get char from EDX)
  41. ASM_PROGRAM += "BF_PUTCHAR:\n"
  42. if (ASM_INCLUDE_IO):
  43.     ASM_PROGRAM += "push eax\n"
  44.     ASM_PROGRAM += "push ebx\n"
  45.     ASM_PROGRAM += "push ecx\n"
  46.     ASM_PROGRAM += "push edx\n"
  47.     ASM_PROGRAM += "pushf\n"
  48.     ASM_PROGRAM += "push "
  49.     ASM_PROGRAM += "call _putch\n"
  50.     ASM_PROGRAM += "add esp, 4\n"
  51.     ASM_PROGRAM += "popf\n"
  52.     ASM_PROGRAM += "pop edx\n"
  53.     ASM_PROGRAM += "pop ecx\n"
  54.     ASM_PROGRAM += "pop ebx\n"
  55.     ASM_PROGRAM += "pop eax\n"
  56. ASM_PROGRAM += "ret\n\n"
  57.  
  58. #BF_GETCHAR function (return char to EDX)
  59. ASM_PROGRAM += "BF_GETCHAR:\n"
  60. if (ASM_INCLUDE_IO):
  61.     ASM_PROGRAM += "push eax\n"
  62.     ASM_PROGRAM += "push ebx\n"
  63.     ASM_PROGRAM += "push ecx\n"
  64.     ASM_PROGRAM += "pushf\n"
  65.     ASM_PROGRAM += "call _getch\n"
  66.     ASM_PROGRAM += "mov edx, eax\n"
  67.     ASM_PROGRAM += "add esp, 4\n"
  68.     ASM_PROGRAM += "popf\n"
  69.     ASM_PROGRAM += "pop ecx\n"
  70.     ASM_PROGRAM += "pop ebx\n"
  71.     ASM_PROGRAM += "pop eax\n"
  72. else:
  73.     ASM_PROGRAM += "mov edx, 255\n"
  74. ASM_PROGRAM += "ret\n\n"
  75.  
  76. lookup = {
  77.     '>' : "inc eax\n\n",
  78.     '<' : "dec eax\n\n",
  79.     '+' : "mov ebx, [eax]\ninc ebx\nmov [eax], ebx\n\n",
  80.     '-' : "mov ebx, [eax]\ndec ebx\nmov [eax], ebx\n\n",
  81.     '.' : "mov edx, [eax]\ncall BF_PUTCHAR\n\n",
  82.     ',' : "call BF_GETCHAR\nmov [eax], edx\n\n",
  83.     '[' : "BF_WHILE_$(RAND):\nmov ebx, [eax]\ncmp ebx, 0\nje BF_WHILE_END_$(RAND)\n\n",
  84.     ']' : "jmp BF_WHILE_$(LRAND)\nBF_WHILE_END_$(LRAND):\n\n",
  85. }
  86.  
  87. namestack = []
  88. namerng = 0
  89.  
  90. if (ASM_INCLUDE_COMMENTS):
  91.     ASM_PROGRAM += "; ACTUAL BF PROGRAM STARTS HERE\n\n"
  92.  
  93. for c in BF_PROGRAM:
  94.     if (lookup.has_key(c)):
  95.         result = lookup[c]
  96.         if (result.find("$(RAND)") != -1):
  97.             namestack += [namerng]
  98.             result = result.replace("$(RAND)", str(namerng))
  99.             namerng += 1
  100.         if (result.find("$(LRAND)") != -1):
  101.             result = result.replace("$(LRAND)", str(namestack.pop()))
  102.         if (ASM_INCLUDE_COMMENTS):
  103.             ASM_PROGRAM += "; COMMAND '%c'\n" % c
  104.         ASM_PROGRAM += result
  105.  
  106. ASM_PROGRAM += "\nMEMORYCELLS:\ndb 0\n"
  107.    
  108. print "; ========================================\n"
  109. print "; CREATE WITH BF2ASM.PY\n"
  110. print "; ========================================\n"
  111. print ASM_PROGRAM
  112.  
  113. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement