Jump to content

bfc - Brainf*ck compiler in C++ and assembly [WIP]

Nineshadow

https://github.com/Nineshadow/bfc

WHAT IS BFC?bfc is a brainfuck compiler which uses assembly and c++.________________________________________________________________________________________Version : 0.9________________________________________________________________________________________HOW TO USE :bfc.exe generates an assembly source file which can then be compiled into an executable.To generate this assembly soure file :(while in the same directory as bfc.exe)bfc.exe <input_file> <output_file>Then, you can transform the assembly source file(.s) into an executable using g++ :g++ -o <name_of_exec> <asm_source>You can also use help to get these instructions :bfc.exe helpOr, to skip these 2 steps, you can use compile.exe.The syntax is the same :compile.exe <input_file> <output_file>Still a little buggy._________________________________________________________________________________________PREREQ : g++ installed_________________________________________________________________________________________FEATURES :This compiler currently doesn't support all of the instructions that the brainfuck language has, as it's still very early in development.Lists of currently supported instructions:'>' - increment pointer'<' - decrement pointer'+' - increment value at pointer'-' - decrement value at pointer'.' - output value at pointer',' - input value at pointerAlso, the array in which you are free to move supports only integers for the moment. I realized that it should have been characters way too late, so I'm going to make a proper version later(that will hopefully also have instructions for the loops)__________________________________________________________________________________________WHAT IS BRAINFUCK :Brainfuck is an esoteric programming language, the ungodly creation of Urban Müller, whose goal was apparently to create a Turing-complete language for which he could write the smallest compiler ever, for the Amiga OS 2.0. His compiler was 240 bytes in size. (Though he improved upon this later -- he , at one point, said that he had managed to bring it under 200 bytes.) A Brainfuck program has an implicit byte pointer, called "the pointer", which is free to move around within an array of 30000 bytes, initially all set to zero. The pointer itself is initialized to point to the beginning of this array.The Brainfuck programming language consists of eight commands, each of which is represented as a single character.> 	Increment the pointer.< 	Decrement the pointer.+ 	Increment the byte at the pointer.- 	Decrement the byte at the pointer.. 	Output the byte at the pointer., 	Input a byte and store it in the byte at the pointer.[ 	Jump forward past the matching ] if the byte at the pointer is zero.] 	Jump backward to the matching [ unless the byte at the pointer is zero.The semantics of the Brainfuck commands can also be succinctly expressed in terms of C, as follows (assuming that p has been previously defined as a char*):> 	becomes 	++p;< 	becomes 	--p;+ 	becomes 	++*p;- 	becomes 	--*p;. 	becomes 	putchar(*p);, 	becomes 	*p = getchar();[ 	becomes 	while (*p) {] 	becomes 	}___________________________________________________________________________________________MISC :In ./asm_instructions you can find text files with the assembly instructions corresponding to some C/C++ statements/commands, both in AT&T(not yet finished) and Intel syntax../asm_test : used for finding out the corresponding assembly instructions.You can compile a .cpp/.c file dirrectly into assembly source code with gcc/g++ :g++ -S <input_file>In ./src you can find the source code for the compiler, for both a char(WIP) and int version.In ./tests you can find sample brainfuck sources you could compile. Not a whole lot of them so far.____________________________________________________________________________________________REASON OF PROJECT :For fun and to exercise with assembly.Also to test how github works  .____________________________________________________________________________________________UPDATES :0.9 - second release, added loops, still only using integers. Version 1.0 will use characters. Also, please don't critique the jump from 0.1 to 0.9 directly. I feel like it's has an almos full feature set, hence the 0.9. 0.1 - first release, didn't have support for loops and only worked with integers

It's 90% complete! If you find any issues, please report them. I'm also open to ideas

 

 

 

 

UPDATE 0.9 :

The long awaited moment ( by no person ever) has arrived! This brainfuck
compiler now has support for loops. You can now use any number of
brackets that you'd like.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Also, do you guys have any suggestions on how to do the loops in assembly?

I know asm doesn't have any while loops, but it has jumps. How should I implement those? I summon you, @Ciccioo !

Sex with unicorns.

 

And then there's me, learning how to use PhotoShop and Premiere, and giving zero fucks that he can only make Visual Basic programs with tutorials. :P

Main rig on profile

VAULT - File Server

Spoiler

Intel Core i5 11400 w/ Shadow Rock LP, 2x16GB SP GAMING 3200MHz CL16, ASUS PRIME Z590-A, 2x LSI 9211-8i, Fractal Define 7, 256GB Team MP33, 3x 6TB WD Red Pro (general storage), 3x 1TB Seagate Barracuda (dumping ground), 3x 8TB WD White-Label (Plex) (all 3 arrays in their respective Windows Parity storage spaces), Corsair RM750x, Windows 11 Education

Sleeper HP Pavilion A6137C

Spoiler

Intel Core i7 6700K @ 4.4GHz, 4x8GB G.SKILL Ares 1800MHz CL10, ASUS Z170M-E D3, 128GB Team MP33, 1TB Seagate Barracuda, 320GB Samsung Spinpoint (for video capture), MSI GTX 970 100ME, EVGA 650G1, Windows 10 Pro

Mac Mini (Late 2020)

Spoiler

Apple M1, 8GB RAM, 256GB, macOS Sonoma

Consoles: Softmodded 1.4 Xbox w/ 500GB HDD, Xbox 360 Elite 120GB Falcon, XB1X w/2TB MX500, Xbox Series X, PS1 1001, PS2 Slim 70000 w/ FreeMcBoot, PS4 Pro 7015B 1TB (retired), PS5 Digital, Nintendo Switch OLED, Nintendo Wii RVL-001 (black)

Link to comment
Share on other sites

Link to post
Share on other sites

To do loops, you compare and jump to the corresponding label (in IA32 at least) 

 

More specifically, compare 2 registers.

 

Make the jump, change the value, and do the comparison once again.

Link to comment
Share on other sites

Link to post
Share on other sites

To do loops, you compare and jump to the corresponding label (in IA32 at least) 

Yeah, just thinking about the implementation D: .

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, just thinking about the implementation D: .

Consider this simple c program:

 

int simple_jump(int x, int y, int z) {

   if (x == 0)

      return y-z;

   return z-y;

}

 

IA32 might compile to:

 

simple_jump:
        pushl   %ebp
        movl    %esp, %ebp
 
        cmpl    $0, 8(%ebp)     // compare x to 0
        jne     .L2             // jump if x != 0
                                // get here if x == 0
        movl    12(%ebp), %eax  // y into %eax
        subl    16(%ebp), %eax  // y - z into %eax
        jmp     .L3             // done
.L2:                            // this is the case x != 0
        movl    16(%ebp), %eax  // z in %eax
        subl    12(%ebp), %eax  // z - y in %eax
 
.L3:                            // common return
        popl    %ebp
        ret
Link to comment
Share on other sites

Link to post
Share on other sites

Sex with unicorns.

 

And then there's me, learning how to use PhotoShop and Premiere, and giving zero fucks that he can only make Visual Basic programs with tutorials. :P

Ah, come on, it's not that hard. I gotta say this was pretty damn easy.

I did it just for fun and exercise(and to see how github works).

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Ah, come on, it's not that hard. I gotta say this was pretty damn easy.

I did it just for fun and exercise(and to see how github works).

It's nowhere near as easy as PhotoShop. And I get bored just typing and typing and typing... And typing... Video editing and photo edits are kinda my favorite thing to do, I guess.

Main rig on profile

VAULT - File Server

Spoiler

Intel Core i5 11400 w/ Shadow Rock LP, 2x16GB SP GAMING 3200MHz CL16, ASUS PRIME Z590-A, 2x LSI 9211-8i, Fractal Define 7, 256GB Team MP33, 3x 6TB WD Red Pro (general storage), 3x 1TB Seagate Barracuda (dumping ground), 3x 8TB WD White-Label (Plex) (all 3 arrays in their respective Windows Parity storage spaces), Corsair RM750x, Windows 11 Education

Sleeper HP Pavilion A6137C

Spoiler

Intel Core i7 6700K @ 4.4GHz, 4x8GB G.SKILL Ares 1800MHz CL10, ASUS Z170M-E D3, 128GB Team MP33, 1TB Seagate Barracuda, 320GB Samsung Spinpoint (for video capture), MSI GTX 970 100ME, EVGA 650G1, Windows 10 Pro

Mac Mini (Late 2020)

Spoiler

Apple M1, 8GB RAM, 256GB, macOS Sonoma

Consoles: Softmodded 1.4 Xbox w/ 500GB HDD, Xbox 360 Elite 120GB Falcon, XB1X w/2TB MX500, Xbox Series X, PS1 1001, PS2 Slim 70000 w/ FreeMcBoot, PS4 Pro 7015B 1TB (retired), PS5 Digital, Nintendo Switch OLED, Nintendo Wii RVL-001 (black)

Link to comment
Share on other sites

Link to post
Share on other sites

I summon you, @Ciccioo !

it_s_n10.png

with that said, this should translate quite naturally to a conditional jump instruction (not that i would know how it works)

 

@Stuff_ 's example looks exaustive enough

Link to comment
Share on other sites

Link to post
Share on other sites

  • 1 month later...

UPDATE 0.9 :

The long awaited moment ( by no person ever) has arrived! This brainfuck
compiler now has support for loops. You can now use any number of
brackets that you'd like.

 

@Ciccioo

 

I gotta say my work is 90% complete right now. Only need to change the array in which you can move the pointer to the type that brainfuck uses. Currently it's working with integers.

 

And the jumps weren't even all that hard. You just needed to have some knowledge of assembly, which I kinda lacked. After I had worked for like half an hour on the instructions for a jump, it just crashed.Then for the next 3 hours I kept debugging.Heck , I even made a version in C. Only to go back to the C++ vesion to discover that I had to change 1 letter and 1 instruction.I feel like an idiot.

This is what I'm talking about, from this :

cmp    %eax , 0je endLoop+countID 

to this:

testl   %eax , %eaxjz endLoop+countID 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×