Here is an outline of how to build a compiler that takes BF as input, and generates a Malbolge program to do the same thing. Here we assume we have a loader that lets us put arbitrary instructions and data in arbirtary locations.

We also assume we can work around this problems of self-modifying instructions, using some of the techniques from the original Malboge analysis.

It is easy to convert BF to this model. basically there are ADDRESS instructions '>' and '<', and DATA instructions (all the rest). When you switch from an address instruction to a data instruction, you need to do a 'load', when you switch back you do a 'store'. Otherwise all operations work on the accumulator.

Now, at location TTTTT00000, we start a table of 243 (immutable) NOPs, followed by a LOAD instruction, then 243 more NOPS (actually we want a few more NOPs to get the LOAD on a good cycle location, but the principle is the same.). Then we do this:

Program counter |
D location |
YYYYY=0 |
YYYYY=1 |
... |
YYYYY=242 |

ROTATE |
2222222222 |
||||

BRANCH |
TTTTTYYYYY |
||||

ANSWER 242 |
NOP |
NOP |
OP |
||

ANSWER 241 |
NOP |
NOP |
NOP |
||

..... |
NOP |
||||

ANSWER 2 |
NOP |
NOP |
NOP |
||

ANSWER 1 |
NOP |
OP |
NOP |
||

ANSWER 0 |
OP |
NOP |
NOP |

Unfortunately, this scrambles the value in the lookup table as well. However, if do the same lookup again, it will scramble it back. By pre-loading the table correctly, we can make either the first or the second lookup come out right (but not both).

Performance: take a time proportional to the number of possible cell values. So it won't be efficient, but that's not our goal here.

For a load, we set A to 2222222222. when we do the OP, this sets both A and the location to original value at the location, but with 1 and 2 swapped. Then exactly the same thing again. This restores the location to the original value, and sets the A register to the desired result.

A store is more complex. First, we need to save the A value we want to store (OP it into a location with all 1s - this will swaps the 0s and 1s). Then we need to prepare the value at the destination address.

- First, set A to all 1s and OP in into the location. Now the location can contain only 0s and 2s.
- Next, set A to all 2s and OP into the location. Now it has just 0s and 1s.
- Next, set A to all 0s and OP into the location. This will force the location to all 1s.
- Next, retrieve the saved A (this can be done with a 2 ops or 10 rotates.) remember this had the original value with 0 and 1 swapped)
- Finally, OP this A into the desired location, which contains 1111111111. This will unswap the 0s and 1s.