Copy byte value from an address to the specified address

80 views Asked by At

I have this bf code:

> ++++ ++ @1=6

How do I copy current value (6) in address @1 to the specified address for example address @8

I can do it with this:

> ++++ ++ @1=6
[
 >>>> >>>+ @8 increments by 1
 <<<< <<<- @1 decrements by 1
]
In this line, @1=0 and @8=6

It overwrites @1 value to the zero while I expect hold value inside @1.

2

There are 2 answers

0
Daniel Cristofani On
  1. That code is wrong and doesn't even move or zero the 6 in cell 1. The loop is skipped.

  2. To copy arbitrary values does require breaking them down to 0 in order to check the value. The usual workaround is, copy value to two places (destination and a temp location), then move value from temp location back to source location using another loop.

  3. However, copying values a lot is bad for concision. It's often better to move values and use or check them in the process, rather than make copies/use copies/wipe copies.

0
user17301834 On

Your answer doesn't work because you initialise the loop when the pointer is at 0, which points to an empty cell, but if you just want to copy a value from @1 to @8, you can do the following:

>++++++ @1=6

[
 >+>>>>>>+ increments @2 and @8
 <<<<<<<- decrements @1
] at the end of this line @1=0, @2=6 and @=6

>[-<+>] @2=0 and @1=8

>>>>>> moves the pointer back to @8

Here's a general solution to move a value n from any cell to the cell c cells in front of it:

c is stored at the current address, and n in the previous one
[[->+<]<[->+<]>>-]<

E.g. if you store @3=5 and @4=7, with the pointer being at address @4, this code will store @10=5 and @3=5, with the remaining cells being at 0.