1 Replies - 947 Views - Last Post: 13 March 2010 - 10:06 AM

#1 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

PlasticineGuy's Brainf*ck Compiler

Posted 12 March 2010 - 10:46 PM

I wrote a tool in Visual C++ that produces assembly code and a Windows executable from a given file of Brainf*ck code which it then assembles using Flat Assembler. I do not claim it to be perfect as I only have rudimentary knowledge of ASM.
The generated code uses WinAPI calls so it will only work on a Windows PC.

Here's what it goes through:
Brainf*ck Original Code
+++++ +++++             initialize counter (cell #0) to 10
[                       use loop to set the next four cells to 70/100/30/10
    > +++++ ++              add  7 to cell #1
    > +++++ +++++           add 10 to cell #2 
    > +++                   add  3 to cell #3
    > +                     add  1 to cell #4
    <<<< -                  decrement counter (cell #0)
]                   
> ++ .                  print 'H'
> + .                   print 'e'
+++++ ++ .              print 'l'
.                       print 'l'
+++ .                   print 'o'
>++ .                   print ' '
<< +++++ +++++ +++++ .  print 'W'
> .                     print 'o'
+++ .                   print 'r'
----- - .               print 'l'
----- --- .             print 'd'
> + .                   print '!'
> .                     print '\n'


Flat Assembler source code
;generated by BFCompiler v0.1 on Sat Mar 13 16:42:14 2010

include 'C:\bf\BFCompiler\include\win32ax.inc'
.data
inchar DB ?
numwritten DD ?
numread DD ?
outhandle DD ?
inhandle DD ?

cell0 DB 0
cell1 DB 0
cell2 DB 0
cell3 DB 0
cell4 DB 0
.code
start:
.init:
invoke AllocConsole
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov [outhandle],eax
invoke GetStdHandle,STD_INPUT_HANDLE
mov [inhandle],eax
.program:
add [cell0],10
.lp_GXajRojnxB:
add [cell1],7
add [cell2],10
add [cell3],3
add [cell4],1
dec [cell0]
cmp [cell0],0
jne .lp_GXajRojnxB
add [cell1],2
invoke WriteConsole,[outhandle],cell1,1,numwritten,0
add [cell2],1
invoke WriteConsole,[outhandle],cell2,1,numwritten,0
add [cell2],7
invoke WriteConsole,[outhandle],cell2,1,numwritten,0
invoke WriteConsole,[outhandle],cell2,1,numwritten,0
add [cell2],3
invoke WriteConsole,[outhandle],cell2,1,numwritten,0
add [cell3],2
invoke WriteConsole,[outhandle],cell3,1,numwritten,0
add [cell1],15
invoke WriteConsole,[outhandle],cell1,1,numwritten,0
invoke WriteConsole,[outhandle],cell2,1,numwritten,0
add [cell2],3
invoke WriteConsole,[outhandle],cell2,1,numwritten,0
sub [cell2],6
invoke WriteConsole,[outhandle],cell2,1,numwritten,0
sub [cell2],8
invoke WriteConsole,[outhandle],cell2,1,numwritten,0
add [cell3],1
invoke WriteConsole,[outhandle],cell3,1,numwritten,0
invoke WriteConsole,[outhandle],cell4,1,numwritten,0
.exit:
invoke ReadConsole,[inhandle],inchar,1,numread,0
invoke ExitProcess,0
.end start

And the output:
Hello World!




Download Link:
Download bfcompiler.zip

I'm trying to figure out how to optimise it so ASM programs bigger than about a kilobyte load and run in a reasonable amount of time.
It performs only rudimentary optimisations; folding multiple inc/dec instructions into a single add/sub instruction.

Warning: Currently expressions similar to [<] do not work correctly.

This post has been edited by PlasticineGuy: 13 March 2010 - 02:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PlasticineGuy's Brainf*ck Compiler

#2 programble   User is offline

  • (cons :dic :head)

Reputation: 50
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: PlasticineGuy's Brainf*ck Compiler

Posted 13 March 2010 - 10:06 AM

Cool. Similar to mine, which translates to C, written for Linux.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1