Context
Here is a summary of our structures, and operations implemented to handle the basic 8 onesStructure 1 - "4 cells"
A, B, C, D to represent integer N; with
- A = 0
- B = 0
- C and D so that N = D * 256 + C
Cursor on C
Structure 2 - "3 cells"
A, B, C to represent integer N; with
- A and B so that N = B * 256 + A
- C = 0
Cursor on A
Operation | "4 cells" | "3 cells" |
---|---|---|
,(read) | ,>[-]< | ,>[-]< |
.(print) | . | . |
<(move left) | <<<< | <<< |
>(move right) | >>>> | >>> |
+(inc) | +<+>[<-]<[->>+<<<]>> | +[-<+>>>+<<]<[->+<]+>>>[[-]<<<->>>]<<<[->>+<<]> |
-(dec) | <+>[<-]<[->>-<<<]>>- | [-<+>>>+<<]<[->+<]+>>>[[-]<<<->>>]<<<[->>-<<]>- |
[(while) | >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]> | >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]> |
](loop) | >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]> | >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]> |
Application
Let's reuse our previous "decimal printing" algorithm, and replace each instruction by its new implementation, to print a large number (let's say 2056)
Code - 4 cells structure - try it
generate 2056 using 4 cells structureNote: 3 cells version's code is about 50% longer, but it's also 3 times longer to execute...
>>>>>>++++++++>++++++++<
decimal print algorithm rewritten using instruction replacements
>[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]>>>>>>>>>>>>>>>>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>>+<+>[<-]<[->>+<<<]>><<<<<<<<<<<<<<<<>[[->+<]<<+>>]<[[->+<]<+>]>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