Search

mardi 21 mars 2017

16-bit values: concept, structures, IO and moves (1)

Context

What may be annoying using Brainfuck is the size of the cell. You can't (in theory) have integers greater than 255. Of course, this is the case for most of the programming languages, where integers are encoded using either 32 or 64 bits, but the limit is far higher in those cases.
Obviously, the 'cheating' solution would be to use another BF interpreter implementation that supports 16-, 32- or 64-bit integers. But how to use, let's say, 16-bit integers (up to 65,535) using regular Brainfuck?
Our only option would be to

  1. Define a new data structure
  2. Re-define all the 8 operators using this new data structure
    What does '+' means ? Add 1. So, how to modify our data that represents an integer N to represents integer 'N+1' instead, and same for - , . < > [ ]

Structure(s)

Actually, I found 2 different data structures, one that uses more memory cells but with faster overrides of operators, and one using less cells but with longer processes.
Actually, both needs 4 cells, but when you have multiple integers in memory, first one uses 4 cells per integer, while second one can use only 3 cells (reusing a cell from previous integer)
In this post and all the coming ones, we'll define the 2 structures and all their operators.

Structure 1 - "4 cells"

Let's have 4 cells A, B, C, D to represent integer N; with
  • A = 0
  • B = 0
  • C and D so that N = D * 256 + C
A and B are used for logical branching that will occur in our future code.
Using this structure, "having on a value" means having cursor on C

Structure 2 - "3 cells"

Let's have 4 cells A, B, C to represent integer N; with
  • A and B so that N = B * 256 + A
  • C = 0
Again, C is used for logical branching, but also "D", which is the "C" from the previous integer.
Using this structure, "having on a value" means having cursor on A

IO

Using those 2 structures, let's define how to read or print a value. Read means reinitialize the "quotient" (D, or B, according to the structure) and read value in "remainder" (C, or A)


Operation"4 cells""3 cells"
,(read) ,>[-]< ,>[-]<
.(print) . .

Moves

Now, let's do the same wit


Operation"4 cells""3 cells"
<(move left) <<<< <<<
>(move right) >>>> >>>

Aucun commentaire:

Enregistrer un commentaire