Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial How to make a Brainfuck: Part 2, implementing the data instructions in C filter_list
Author
Message
How to make a Brainfuck: Part 2, implementing the data instructions in C #1
How to make a Brainfuck: Implementing the data instructions


In this tutorial, we'll be implementing the following operations:
  • +
  • -
  • >
  • <

You should have a basic knowledge of C syntax, and of how arrays and pointers work.

How is this going to work?

We need two arrays, one to act as a stack, and another for the cells.  Both arrays will be 256 items long, I may go over dynamic data allocation later, but not now.

Lets begin:

Open your favorite text editor, and write a blank main function, along with a 256-item long char array named tape (remember that arrays index from 0), and a pointer to the it, named cell:
Code:
char tape[255];
char *cell = tape;

int main(int argc, char *argv[])
{

}

Then use bzero to fill tape with zeros:
Code:
int main(int argc, char *argv[])
{
   bzero(tape, 255);
}

Now, lets see how the operations look in C code:
Code:
+ → ++*cell
- → --*cell
> → ++cell
< → --cell

Next, make a function called interpret, which takes a char array/pointer named instructionList as it's only argument, and returns int.
Code:
int interpret(char *instructionList)
{

}
If you're new to C you might be wondering why I didn't do interpret(char instructionList[]).  That actually works the same, the reason for this is that arrays decay into pointers when passed to functions.
Not only that, but when you write arrayname[index], this is converted to *(arrayname + index) during compilation... So... arrayname[index] will give the same index[arrayname], which converts to *(index + arrayname).

We need an int x to keep track of where we are, set it to 0.  We also need a char named instruction.
Spoiler:
Code:
int interpret(char *instructionList)
{
   char instruction;
   int x;

   x = 0;
}

Ok, now let's get to the real code!
Lets make a for loop, which initializes instruction as instructionList[x], checks if instruction is not equal to zero, and increments x at the end.
In the for loop, lets set instruction to instructionList[x], and lets add a switch statement.  This switch statement shall execute the code for the instruction that's in instruction, and break.

If you don't know about switch statements, you can read up on them here: https://www.tutorialspoint.com/cprogramm...t_in_c.htm

When you're done with that, you can check your code against this:
Spoiler: code
Code:
int interpret(char *instructionList)
{
   char instruction;
   int x;

   x = 0;
   for (instruction = instructionList[x]; instruction != 0; x++)
   {
       instruction = instructionList[x];
       switch(instruction)
       {
           case '+':
               ++*cell;
               break;
            case '-':
               --*cell;
               break;
           case '>':
               ++cell;
               break;
           case '<':
               --cell;
               break;
       }
   }
}

Conclusion

There we go, you have your data instructions!
Next, we'll implement I/O, '[' and ']', and file reading. In the third tutorial, we'll add error checking, arguments, and extensions.

After that, we'll dive into assembly.  You don't have to stay along for that, as it's for more advanced crowds.  
At some point, I'd like to make a compiler tutorial, where we convert Brainfuck to NASM, and add some optimizations.

Along with this series, I'm gonna be starting to work on a series on making a cryptocurrency, you should check that out too when it comes out!

Don't forget to post below! Ask questions, comment on this, and discuss anything as long as it's related.
(This post was last modified: 12-01-2017, 07:56 AM by Blink.)


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.

[+] 2 users Like Blink's post
Reply

RE: How to make a Brainfuck: Part 1, implementing the data instructions in C #2
I have a question, why does interpret have an integer signature when it returns nothing? I'm assuming the signature is designed for error feedback (0 on success kind of thing)

Reply

RE: How to make a Brainfuck: Part 1, implementing the data instructions in C #3
(12-01-2017, 05:46 AM)phyrrus9 Wrote: I have a question, why does interpret have an integer signature when it returns nothing? I'm assuming the signature is designed for error feedback (0 on success kind of thing)

It'll eventually return something other than 0 on error, but for now I forgot to add return 0 to the end.

It still makes sense to add it in part 3, so I'll do it then.


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.

Reply

RE: How to make a Brainfuck: Part 2, implementing the data instructions in C #4
I have read about BF some time back. Looks good for sending out some computer details back to botnet C2C with BF implementation. Wink

Reply

RE: How to make a Brainfuck: Part 2, implementing the data instructions in C #5
(12-04-2017, 12:45 PM)alfascanner Wrote: I have read about BF some time back. Looks good for sending out some computer details back to botnet C2C with BF implementation. Wink

That would be incredibly difficult to do, unless you're using some rt engine and netcat, but then why bother with bf when you could substitute for ELF's

Reply

RE: How to make a Brainfuck: Part 2, implementing the data instructions in C #6
(12-05-2017, 02:16 AM)phyrrus9 Wrote: That would be incredibly difficult to do, unless you're using some rt engine and netcat, but then why bother with bf when you could substitute for ELF's

Bro, I was talking about something like this..
Code:
base64(brainfuck_encode($_POST['data']));

Just for basic information. Wink

Reply

RE: How to make a Brainfuck: Part 2, implementing the data instructions in C #7
(12-05-2017, 04:22 AM)alfascanner Wrote:
(12-05-2017, 02:16 AM)phyrrus9 Wrote: That would be incredibly difficult to do, unless you're using some rt engine and netcat, but then why bother with bf when you could substitute for ELF's

Bro, I was talking about something like this..
Code:
base64(brainfuck_encode($_POST['data']));

Just for basic information. Wink

Why don't you just encrypt the data, instead of encoding it?
Brainfuck was made to be a programming language, not a cipher.
(This post was last modified: 12-05-2017, 07:24 AM by Blink.)


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.

Reply

RE: How to make a Brainfuck: Part 2, implementing the data instructions in C #8
(12-05-2017, 07:24 AM)Ender Wrote:
(12-05-2017, 04:22 AM)alfascanner Wrote:
(12-05-2017, 02:16 AM)phyrrus9 Wrote: That would be incredibly difficult to do, unless you're using some rt engine and netcat, but then why bother with bf when you could substitute for ELF's

Bro, I was talking about something like this..
Code:
base64(brainfuck_encode($_POST['data']));

Just for basic information. Wink

Why don't you just encrypt the data, instead of encoding it?
Brainfuck was made to be a programming language, not a cipher.

^^^^^^^^^

plus, why would you use brainfuck for that anyways? it would easily make your data length 20 times larger than the plaintext. Just seems like a stupid idea

[+] 1 user Likes phyrrus9's post
Reply

RE: How to make a Brainfuck: Part 2, implementing the data instructions in C #9
(12-05-2017, 08:22 AM)phyrrus9 Wrote:
(12-05-2017, 07:24 AM)Ender Wrote:
(12-05-2017, 04:22 AM)alfascanner Wrote: Bro, I was talking about something like this..
Code:
base64(brainfuck_encode($_POST['data']));

Just for basic information. Wink

Why don't you just encrypt the data, instead of encoding it?
Brainfuck was made to be a programming language, not a cipher.

^^^^^^^^^

plus, why would you use brainfuck for that anyways? it would easily make your data length 20 times larger than the plaintext. Just seems like a stupid idea

Not if your text is
Code:
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.

Reply

RE: How to make a Brainfuck: Part 2, implementing the data instructions in C #10
(12-05-2017, 09:24 PM)Ender Wrote:
(12-05-2017, 08:22 AM)phyrrus9 Wrote:
(12-05-2017, 07:24 AM)Ender Wrote: Why don't you just encrypt the data, instead of encoding it?
Brainfuck was made to be a programming language, not a cipher.

^^^^^^^^^

plus, why would you use brainfuck for that anyways? it would easily make your data length 20 times larger than the plaintext. Just seems like a stupid idea

Not if your text is
Code:
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

lmao but why would you do that in the first place then

Reply







Users browsing this thread: 1 Guest(s)