GithubHelp home page GithubHelp logo

ladnik / brainasm Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 47 KB

An interpreter for the esoteric programming language brainfuck, written in assembly. Included is a translation to the most excellent programming language MemeAssembly.

Assembly 100.00%

brainasm's Introduction

[WIP] brainASM

Warning

This repository is currently being worked on. Stuff may break and not work as advertised.

This project is an interpreter for the esoteric programming language brainfuck, written in assembly. Included is a translation to the most excellent programming language MemeAssembly.

If you have any questions regarding this project or ideas on improving it, feel free to contact me.

Background

Brainfuck is an esoteric programming language created in 1993 by Urban Müller. Notable for its extreme minimalism, the language consists of only eight simple commands, a data pointer and an instruction pointer. While it is fully Turing complete, it is not intended for practical use, but to challenge and amuse programmers. [Wikipedia]

Implementation

In this project, two memory ranges are reserved: input and cells. The input brainfuck string is provided via stdin with a maximum buffer size of 10k characters. For each cell one byte of memory is reserved, in total there is space for 10k cells. These constants are defined as INPUT_SIZE and CELL_COUNT, respectively. As Brainfuck has a very limited number of commands (8 in total: < > + - , . [ ]), a switch-like comparision is used to perform the desired actions. Characters not defined as commands will be ignored. The instruction pointer is stored in EAX, the cell pointer in EBX.

Further details are explained in the code's comments.

Execution

Provide the brainfuck program to stdin, output will be sent do stdout.

I just wanna run this without much of a hassle

Compile it online via [myCompiler]

Compiling locally

Build your own executable locally with NASM and GNU Linker:

  • brainasm.asm:
nasm -f elf64 -o asm/object_a.o asm/brainasm.asm && ld -o asm/out_a asm/object_a.o
  • brainasm_stack.asm
nasm -f elf64 -o asm/object_b.o asm/brainasm_stack.asm && ld -o asm/out_b asm/object_b.o
  • brainasm_stack.memeasm:
memeasm -o memeasm/out_c memeasm/brainasm_stack.memeasm

Examples

Examples can be found in /examples/. These can be passed to the interpreter like so:

cat examples/helloworld.b | ./asm/out_a
  • Hello World
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

Output: Hello World!

  • The mathematical constant e
>>>>++>+>++>+>>++<+[[>[>>[>>>>]<<<<[[>>>>+<<<<-]<<<<]
>>>>>>]+<]>->>--[+[+++<<<<--]++>>>>--]+[>>>>]<<<<[<<+
<+<]<<[>>>>>>[[<<<<+>>>>-]>>>>]<<<<<<<<[<<<<]>>-[<<+>
>-]+<<[->>>>[-[+>>>>-]-<<-[>>>>-]++>>+[-<<<<+]+>>>>]<
<<<[<<<<]]>[-[<+>-]]+<[->>>>[-[+>>>>-]-<<<-[>>>>-]++>
>>+[-<<<<+]+>>>>]<<<<[<<<<]]<<]>>>+[>>>>]-[+<<<<--]++
[<<<<]>>>+[>-[>>[--[++>>+>>--]-<[-[-[+++<<<<-]+>>>>-]
]++>+[-<<<<+]++>>+>>]<<[>[<-<<<]+<]>->>>]+>[>>>>]-[+<
<<<--]++<[[>>>>]<<<<[-[+>[<->-]++<[[>-<-]++[<<<<]+>>+
>>-]++<<<<-]>-[+[<+[<<<<]>]<+>]+<[->->>>[-]]+<<<<]]>[
<<<<]>[-[-[+++++[>++++++++<-]>-.>>>-[<<<----.<]<[<<]>
>[-]>->>+[[>>>>]+[-[->>>>+>>>>>>>>-[-[+++<<<<[-]]+>>>
>-]++[<<<<]]+<<<<]>>>]+<+<<]>[-[->[--[++>>>>--]->[-[-
[+++<<<<-]+>>>>-]]++<+[-<<<<+]++>>>>]<<<<[>[<<<<]+<]>
->>]<]>>>>[--[++>>>>--]-<--[+++>>>>--]+>+[-<<<<+]++>>
>>]<<<<<[<<<<]<]>[>+<<++<]<]>[+>[--[++>>>>--]->--[+++
>>>>--]+<+[-<<<<+]++>>>>]<<<[<<<<]]>>]>]

Note: Has to be manually terminated. [brainfuck.org]

Output: 2.71828182845904523536028747135266249775...

  • Sierpiński triangle
++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+
[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>
+[>>]>+]

Output:

                               *
                              * *
                             *   *
                            * * * *
                           *       *
                          * *     * *
                         *   *   *   *
                        * * * * * * * *
...

brainasm's People

Contributors

ladnik avatar

Stargazers

Jasper Stritzke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.