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

New Topic/Question
Reply



MultiQuote



|