-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_bf_interpreter.hell
More file actions
3698 lines (2872 loc) · 65.5 KB
/
example_bf_interpreter.hell
File metadata and controls
3698 lines (2872 loc) · 65.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BRAINFUCK INTERPRETER IN MALBOLGE UNSHACKLED
// Copyright (C) 2016 Matthias Lutter
// License: GNU General Public License 3.0
// organization of this file:
// .DATA section:
// the more highlevel-things are located at the top of this file.
// highest level: initialization of bf program from STDIN and implementation of bf commands
// middle level: modification of brainfuck memory and pointers
// low level: increment, decrement
// lowest level: loop over rotwidth
// .CODE section:
// Malbolge commands like MOVD, OUT, ..., and some loops (xlat2 cycles)
// a bunch of FLAGs used to return from function calls
.DATA
// BF MACHINE STATES:
// - program_counter
// - memory_pointer
// - DONT_GO_LEFT_FLAG (current memory cell ist first one)
// - MOVE_TO_BF_STOPLOOP_FLAG (skip every command, ount level of loop-nesting)
// - MOVE_TO_BF_STARTLOOP_FLAG (go backwards, count level of loop-nesting)
// - nesting_counter (used to scan for matching square brackets (bf loops))
// BF INSTRUCTION MEMORY: begin and end of brainfuck program is indicated by
// a memory cell containing BF_EOF.
// the instruction memory starts at the end of the interpreter logic,
// growing into unused memory.
// no fixed offset or label is used to bypass LMFAOs memory arrangement.
// the fixed offset is 1t2200000000.
// If the interpreter is extended and consumes more space,
// the fixed offset must be moved.
// it must only be moved by an even number of positions.
// every brainfuck command is stored as a pointer to its implementation below,
// followed by 1t01111 (preinitialized value; used to jump back during
// initialization of instruction memory)
// BF DATA MEMORY:
// every memory cell consists of:
// STATE, 1t01111, VALUE, 1t01111,
// where 1t01111 is used to jump back after accessing the cell
// and STATE is 0t10000 for uninitialized cells while it is
// 0t0 for initialized non-negative cells and 0t1 for negative cells.
// For negative cells, -1 is stored as 0 in VALUE, -2 as 1 in VALUE and so on.
// memory is growing backwards, starting in front the interpeter
// (to have infinite many memory cells).
// The very first memory cell is: STATE, BF_FIRST_MEM_CELL, VALUE, 1t01111
// On backjump, BF_FIRST_MEM_CELL is detected and the '<' operator can be disabled
// while being at the first memory position.
// brainfuck commands:
// BF_INC, BF_DEC, BF_RIGHT, BF_LEFT, BF_STARTLOOP, BF_ENDLOOP, BF_IN, BF_OUT
// auxiliary functions (middle level)
// read_bf_program_counter
// reset_bf_program_counter
// write_bf_program_counter
// read_bf_memory_pointer
// reset_bf_memory_pointer
// write_bf_memory_pointer
// read_bf_nesting_counter
// reset_bf_nesting_counter
// write_bf_nesting_counter
// read_bf_memory_cell (at pointer position)
// reset_bf_memory_cell (at pointer position)
// write_bf_memory_cell (at pointer position)
// dec_memory_pointer_two
// inc_memory_pointer_two
// movdmovd_bf_program_counter // fetch and execute command at instruction pointer (bf_program_counter)
// auxiliary functions (low level)
// reset_val (set value (and tmp) to C1)
// write_val (opr A into tmp, then into value)
// read_val_C0_offset (read value, starting trits will be set to C0)
// read_val_C1_offset (read value, starting trits will be set to C1)
// inc_val (increment value, CARRY indicates overflow)
// dec_val (decrement value, CARRY indicates overflow)
// increase_rotwidth (double rot_width)
// backjump
@1t01112 return_from_uninitialized_cell:
R_MOVD
MOVDOPRMOVD_FLAG1 movdoprmovd_ret1 R_MOVDOPRMOVD_FLAG1
// restore R_MOVMOVD
R_MOVDMOVD ?- ?- ?- ?-
INC_ROTWIDTH_FLAG3 inc_rotwidth_ret3
BF_FIRST_MEM_CELL:
DONT_GO_LEFT_FLAG BF_FIRST_MEM_CELL
R_MOVD
MOVDOPRMOVD_FLAG1 movdoprmovd_ret1
// brainfuck memory
@1t022222 // even
BF_MEMORY: ? BF_FIRST_MEM_CELL ? ?
// @1t2200000000 BF_INSTRUCTIONS: ? (do NOT uncomment, this would destroy the program due to LMFAOs memory arrangement)
/** BEGIN: initialization */
ENTRY:
// write BF_EOF in front of brainfuck program using memory_pointer (memory_pointer = 1t2200000000 - 3)
// read bf program from STDIN until ! or EOF and write bf program to instruction memory using memory_pointer
// write BF_EOF behind brainfuck program using memory_pointer
// set memory pointer to first brainfuck MEMORY cell
// jump to first brainfuck instruction cell to start bf execution
//
R_RESET_BF_MC_FLAG9
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret9:
ROT BF_EOF << 1 R_ROT
R_WRITE_BF_MC_FLAG9
MOVD write_bf_memory_cell
write_bf_memory_cell_ret9:
R_INC_MP_FLAG10
MOVD inc_memory_pointer_two
read_next_bf_command:
R_MOVD
inc_memory_pointer_two_ret10:
R_RESET_VAL_FLAG18
MOVD reset_val
reset_val_ret18:
// read character
IN ?- R_IN
R_WRITE_VAL_FLAG18
MOVD write_val
write_val_ret18:
// now: increment, test for CARRY -> EOF
R_INC_VAL_FLAG10
MOVD inc_val
inc_val_ret10:
CARRY read_bf_finished // test for EOF
// next interesting value: 34 (! has ASCII code 33, but value has been incremented since it was read above)
// LOOP4, LOOP9 => 36.
// reset loops!
R_MOVD
rl4:
R_MOVD
IN_LOOP4 exit_reset_loop4
MOVD rl4
exit_reset_loop4:
R_MOVD
rl9:
R_MOVD
IN_LOOP9 exit_reset_loop9
MOVD rl9
exit_reset_loop9:
R_IN_LOOP4
R_IN_LOOP4
// 34-LOOP initialized.
R_MOVD
dec_loop_bang:
R_MOVD
R_DEC_VAL_FLAG15
MOVD dec_val
dec_val_ret15:
CARRY inc_memory_pointer_two_ret10 // no bf command has been read. scan for next input character.
IN_LOOP4 exit_inner_bang_loop
MOVD dec_loop_bang
exit_inner_bang_loop:
IN_LOOP9 exit_outer_bang_loop
MOVD dec_loop_bang
exit_outer_bang_loop: // now check for bang (!)
R_DEC_VAL_FLAG16
MOVD dec_val
dec_val_ret16:
CARRY read_bf_finished // a bang (!) has been read. -> finish reading bf from stdin!
// next intresting value: 9 (+ has ASCII code 43, but value has decreased by 34 since it was read above)
R_MOVD
dec_loop_plus:
R_MOVD
R_DEC_VAL_FLAG17
MOVD dec_val
dec_val_ret17:
CARRY inc_memory_pointer_two_ret10 // no bf command has been read. scan for next input character.
IN_LOOP9 exit_plus_loop
MOVD dec_loop_plus
exit_plus_loop:
R_DEC_VAL_FLAG18
MOVD dec_val
dec_val_ret18:
CARRY read_plus_command // a plus (+) has been read. -> add it to bf program
// next intresting value: 0 (, has ASCII code 44, but value has decreased by 44 since it was read above)
R_DEC_VAL_FLAG19
MOVD dec_val
dec_val_ret19:
CARRY read_comma_command // a comma (,) has been read. -> add it to bf program
// next intresting value: 0 (- has ASCII code 45, but value has decreased by 45 since it was read above)
R_DEC_VAL_FLAG20
MOVD dec_val
dec_val_ret20:
CARRY read_minus_command // a minus (-) has been read. -> add it to bf program
// next intresting value: 0 (. has ASCII code 46, but value has decreased by 46 since it was read above)
R_DEC_VAL_FLAG21
MOVD dec_val
dec_val_ret21:
CARRY read_dot_command // a dot (.) has been read. -> add it to bf program
// next intresting value: 13 (< has ASCII code 60, but value has decreased by 47 since it was read above)
R_MOVD
dec_loop1_lt:
R_MOVD
R_DEC_VAL_FLAG22
MOVD dec_val
dec_val_ret22:
CARRY inc_memory_pointer_two_ret10 // no bf command has been read. scan for next input character.
IN_LOOP9 exit_lt_loop1
MOVD dec_loop1_lt
exit_lt_loop1:
R_MOVD
dec_loop2_lt:
R_MOVD
R_DEC_VAL_FLAG23
MOVD dec_val
dec_val_ret23:
CARRY inc_memory_pointer_two_ret10 // no bf command has been read. scan for next input character.
IN_LOOP4 exit_lt_loop2
MOVD dec_loop2_lt
exit_lt_loop2:
R_DEC_VAL_FLAG24
MOVD dec_val
dec_val_ret24:
CARRY read_lt_command // a lt (<) has been read. -> add it to bf program
// next intresting value: 1 (> has ASCII code 62, but value has decreased by 61 since it was read above)
R_DEC_VAL_FLAG25
MOVD dec_val
dec_val_ret25:
CARRY inc_memory_pointer_two_ret10 // no bf command has been read. scan for next input character.
R_DEC_VAL_FLAG26
MOVD dec_val
dec_val_ret26:
CARRY read_gt_command // a gt (>) has been read. -> add it to bf program
// next intresting value: 28 ([ has ASCII code 91, but value has decreased by 63 since it was read above)
R_IN_LOOP4
// 27-LOOP initialized.
R_MOVD
dec_loop_open_sq_bracket:
R_MOVD
R_DEC_VAL_FLAG27
MOVD dec_val
dec_val_ret27:
CARRY inc_memory_pointer_two_ret10 // no bf command has been read. scan for next input character.
IN_LOOP9 exit_inner_open_sq_bracket_loop
MOVD dec_loop_open_sq_bracket
exit_inner_open_sq_bracket_loop:
IN_LOOP4 exit_outer_open_sq_bracket_loop
MOVD dec_loop_open_sq_bracket
exit_outer_open_sq_bracket_loop:
R_DEC_VAL_FLAG28
MOVD dec_val
dec_val_ret28:
CARRY inc_memory_pointer_two_ret10 // no bf command has been read. scan for next input character.
R_DEC_VAL_FLAG29
MOVD dec_val
dec_val_ret29:
CARRY read_osb_command // a open sq bracket ([) has been read. -> add it to bf program
// next intresting value: 1 (] has ASCII code 93, but value has decreased by 92 since it was read above)
R_DEC_VAL_FLAG30
MOVD dec_val
dec_val_ret30:
CARRY inc_memory_pointer_two_ret10 // no bf command has been read. scan for next input character.
R_DEC_VAL_FLAG31
MOVD dec_val
dec_val_ret31:
CARRY read_csb_command // a closed sq bracket (]) has been read. -> add it to bf program
MOVD read_next_bf_command // no bf command has been read. scan for next input character.
// at the end of initialization: write BF_EOF and start execution
read_bf_finished:
// write EOF at end
R_RESET_BF_MC_FLAG10
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret10:
ROT BF_EOF << 1 R_ROT
R_WRITE_BF_MC_FLAG10
MOVD write_bf_memory_cell
write_bf_memory_cell_ret10:
R_RESET_BF_MP_FLAG3
MOVD reset_bf_memory_pointer
reset_bf_memory_pointer_ret3:
ROT (BF_MEMORY - 2) << 1 R_ROT
R_WRITE_BF_MP_FLAG3
MOVD write_bf_memory_pointer
write_bf_memory_pointer_ret3:
DONT_GO_LEFT_FLAG write_bf_memory_pointer_ret3 // set FLAG: first cell active
MOVD movdmovd_bf_program_counter // start bf command by fetching the first command
// below: generate bf commands:
read_plus_command:
R_RESET_BF_MC_FLAG19
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret19:
ROT C1 R_ROT
OPR C02222222222 R_OPR
OPR C10000000000 R_OPR
OPR C01111111111 R_OPR
OPR C1!(C12222222222!BF_INC) R_OPR
OPR BF_INC!C0 R_OPR
OPR BF_INC R_OPR
R_WRITE_BF_MC_FLAG11
MOVD write_bf_memory_cell
write_bf_memory_cell_ret11:
R_INC_MP_FLAG10
MOVD inc_memory_pointer_two
read_comma_command:
R_RESET_BF_MC_FLAG20
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret20:
ROT C1 R_ROT
OPR C02222222222 R_OPR
OPR C10000000000 R_OPR
OPR C01111111111 R_OPR
OPR C1!(C12222222222!BF_IN) R_OPR
OPR BF_IN!C0 R_OPR
OPR BF_IN R_OPR
R_WRITE_BF_MC_FLAG11
MOVD write_bf_memory_cell
read_minus_command:
R_RESET_BF_MC_FLAG21
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret21:
ROT C1 R_ROT
OPR C02222222222 R_OPR
OPR C10000000000 R_OPR
OPR C01111111111 R_OPR
OPR C1!(C12222222222!BF_DEC) R_OPR
OPR BF_DEC!C0 R_OPR
OPR BF_DEC R_OPR
R_WRITE_BF_MC_FLAG11
MOVD write_bf_memory_cell
read_dot_command:
R_RESET_BF_MC_FLAG22
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret22:
ROT C1 R_ROT
OPR C02222222222 R_OPR
OPR C10000000000 R_OPR
OPR C01111111111 R_OPR
OPR C1!(C12222222222!BF_OUT) R_OPR
OPR BF_OUT!C0 R_OPR
OPR BF_OUT R_OPR
R_WRITE_BF_MC_FLAG11
MOVD write_bf_memory_cell
read_lt_command:
R_RESET_BF_MC_FLAG23
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret23:
ROT C1 R_ROT
OPR C02222222222 R_OPR
OPR C10000000000 R_OPR
OPR C01111111111 R_OPR
OPR C1!(C12222222222!BF_LEFT) R_OPR
OPR BF_LEFT!C0 R_OPR
OPR BF_LEFT R_OPR
R_WRITE_BF_MC_FLAG11
MOVD write_bf_memory_cell
read_gt_command:
R_RESET_BF_MC_FLAG24
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret24:
ROT C1 R_ROT
OPR C02222222222 R_OPR
OPR C10000000000 R_OPR
OPR C01111111111 R_OPR
OPR C1!(C12222222222!BF_RIGHT) R_OPR
OPR BF_RIGHT!C0 R_OPR
OPR BF_RIGHT R_OPR
R_WRITE_BF_MC_FLAG11
MOVD write_bf_memory_cell
read_osb_command:
R_RESET_BF_MC_FLAG25
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret25:
ROT C1 R_ROT
OPR C02222222222 R_OPR
OPR C10000000000 R_OPR
OPR C01111111111 R_OPR
OPR C1!(C12222222222!BF_STARTLOOP) R_OPR
OPR BF_STARTLOOP!C0 R_OPR
OPR BF_STARTLOOP R_OPR
R_WRITE_BF_MC_FLAG11
MOVD write_bf_memory_cell
read_csb_command:
R_RESET_BF_MC_FLAG26
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret26:
ROT C1 R_ROT
OPR C02222222222 R_OPR
OPR C10000000000 R_OPR
OPR C01111111111 R_OPR
OPR C1!(C12222222222!BF_ENDLOOP) R_OPR
OPR BF_ENDLOOP!C0 R_OPR
OPR BF_ENDLOOP R_OPR
R_WRITE_BF_MC_FLAG11
MOVD write_bf_memory_cell
/** END: initialization */
/** BEGIN: IMPLEMENTATION OF BRAINFUCK-COMMANDOS */
BF_INC:
R_MOVDMOVD ?- ?- ?- ?-
R_MOVD
MOVE_TO_BF_STOPLOOP_FLAG bf_skip_right R_MOVE_TO_BF_STOPLOOP_FLAG
MOVE_TO_BF_STARTLOOP_FLAG bf_skip_left R_MOVE_TO_BF_STARTLOOP_FLAG
// reset_val
R_RESET_VAL_FLAG9
MOVD reset_val
reset_val_ret9:
// read_bf_memory_cell
R_READ_BF_MC_FLAG3
MOVD read_bf_memory_cell
read_bf_memory_cell_ret3:
// write_val
R_WRITE_VAL_FLAG9
MOVD write_val
write_val_ret9:
// dec_val
R_DEC_VAL_FLAG8
MOVD dec_val
dec_val_ret8:
CARRY bf_inc_initialized_cell
// cell is negative or not initialized
// dec_val
R_DEC_VAL_FLAG32
MOVD dec_val
dec_val_ret32:
CARRY bf_inc_negative_cell
// cell is not initialized
// reset_bf_memory_cell
R_RESET_BF_MC_FLAG1
MOVD reset_bf_memory_cell
bf_inc_negative_cell:
BF_INCREMENTING_NEGATIVE_CELL bf_inc_negative_cell // set flag!
R_CARRY CARRY bf_dec_initialized_cell // use carry to jump without destroying anything important
reset_bf_memory_cell_ret1:
ROT C0 R_ROT
// write_bf_memory_cell
R_WRITE_BF_MC_FLAG1
MOVD write_bf_memory_cell
write_bf_memory_cell_ret1:
// inc_memory_pointer_two
R_INC_MP_FLAG2
MOVD inc_memory_pointer_two
inc_memory_pointer_two_ret2:
// reset_bf_memory_cell
R_RESET_BF_MC_FLAG2
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret2:
BF_INCREMENTING_NEGATIVE_CELL load_zero R_BF_INCREMENTING_NEGATIVE_CELL
ROT C1 R_ROT
OPR 0t2 R_OPR
OPR 1t0 R_OPR
OPR 0t1 R_OPR
continue_init_cell:
// write_bf_memory_cell
R_WRITE_BF_MC_FLAG2
MOVD write_bf_memory_cell
load_zero:
ROT C0 R_ROT
R_BF_INCREMENTING_NEGATIVE_CELL
BF_INCREMENTING_NEGATIVE_CELL continue_init_cell
write_bf_memory_cell_ret2:
// dec_memory_pointer_two
R_DEC_MP_FLAG2
MOVD dec_memory_pointer_two
dec_memory_pointer_two_ret2:
MOVD BF_NEXT_CMD
bf_inc_initialized_cell:
// cell is initialized
// inc_memory_pointer_two
R_INC_MP_FLAG3
MOVD inc_memory_pointer_two
inc_memory_pointer_two_ret3:
inc_rotwidth_ret3: // from overflow-handling
// reset_val
R_RESET_VAL_FLAG10
MOVD reset_val
reset_val_ret10:
// read_bf_memory_cell
R_READ_BF_MC_FLAG4
MOVD read_bf_memory_cell
read_bf_memory_cell_ret4:
// write_val
R_WRITE_VAL_FLAG10
MOVD write_val
write_val_ret10:
// inc_val
R_INC_VAL_FLAG7
MOVD inc_val
inc_val_ret7:
// if CARRY: overflow
CARRY bf_inc_overflow
// reset_bf_memory_cell
R_RESET_BF_MC_FLAG3
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret3:
// read_val_C0
R_READ_VAL_C0_RET_FLAG4
MOVD read_val_C0_offset
read_val_C0_offset_ret4:
// write_bf_memory_cell
R_WRITE_BF_MC_FLAG3
MOVD write_bf_memory_cell
write_bf_memory_cell_ret3:
// dec_memory_pointer_two
R_DEC_MP_FLAG3
MOVD dec_memory_pointer_two
dec_memory_pointer_two_ret3:
MOVD BF_NEXT_CMD
bf_inc_overflow:
// increase_rotwidth
R_INC_ROTWIDTH_FLAG3
MOVD increase_rotwidth
BF_DEC:
R_MOVDMOVD ?- ?- ?- ?-
R_MOVD
MOVE_TO_BF_STOPLOOP_FLAG bf_skip_right R_MOVE_TO_BF_STOPLOOP_FLAG
MOVE_TO_BF_STARTLOOP_FLAG bf_skip_left R_MOVE_TO_BF_STARTLOOP_FLAG
// reset_val
R_RESET_VAL_FLAG11
MOVD reset_val
reset_val_ret11:
// read_bf_memory_cell
R_READ_BF_MC_FLAG7
MOVD read_bf_memory_cell
read_bf_memory_cell_ret7:
// write_val
R_WRITE_VAL_FLAG11
MOVD write_val
write_val_ret11:
// dec_val
R_DEC_VAL_FLAG9
MOVD dec_val
dec_val_ret9:
CARRY bf_dec_initialized_cell
// dec_val
R_DEC_VAL_FLAG33
MOVD dec_val
dec_val_ret33:
CARRY bf_inc_initialized_cell // decrement negative cell like increment of positive cell
// cell is not initialized
// initialize with -1
// reset_bf_memory_cell
R_RESET_BF_MC_FLAG11
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret11:
ROT C1 R_ROT
OPR 0t2 R_OPR
OPR 1t0 R_OPR
OPR 0t1 R_OPR
// write_bf_memory_cell
R_WRITE_BF_MC_FLAG12
MOVD write_bf_memory_cell
write_bf_memory_cell_ret12:
// inc_memory_pointer_two
R_INC_MP_FLAG11
MOVD inc_memory_pointer_two
inc_memory_pointer_two_ret11:
// reset_bf_memory_cell
R_RESET_BF_MC_FLAG12
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret12:
ROT C0 R_ROT
// write_bf_memory_cell
R_WRITE_BF_MC_FLAG13
MOVD write_bf_memory_cell
write_bf_memory_cell_ret13:
// dec_memory_pointer_two
R_DEC_MP_FLAG12
MOVD dec_memory_pointer_two
dec_memory_pointer_two_ret12:
MOVD BF_NEXT_CMD
bf_dec_initialized_cell:
// cell is initialized
// inc_memory_pointer_two
R_INC_MP_FLAG6
MOVD inc_memory_pointer_two
inc_memory_pointer_two_ret6:
// reset_val
R_RESET_VAL_FLAG12
MOVD reset_val
reset_val_ret12:
// read_bf_memory_cell
R_READ_BF_MC_FLAG8
MOVD read_bf_memory_cell
read_bf_memory_cell_ret8:
// write_val
R_WRITE_VAL_FLAG12
MOVD write_val
write_val_ret12:
// dec_val
R_DEC_VAL_FLAG10
MOVD dec_val
dec_val_ret10:
CARRY bf_dec_underflow
// no underflow
// reset_bf_memory_cell
R_RESET_BF_MC_FLAG4
MOVD reset_bf_memory_cell
reset_bf_memory_cell_ret4:
// read_val_C0
R_READ_VAL_C0_RET_FLAG5
MOVD read_val_C0_offset
read_val_C0_offset_ret5:
// write_bf_memory_cell
R_WRITE_BF_MC_FLAG4
MOVD write_bf_memory_cell
write_bf_memory_cell_ret4:
// endif
R_DEC_MP_FLAG6
MOVD dec_memory_pointer_two
dec_memory_pointer_two_ret6:
// endif
MOVD BF_NEXT_CMD
bf_dec_underflow: // set cell to -1 or 0
// set cell to -1 or 0
// dec_memory_pointer_two
R_DEC_MP_FLAG13
MOVD dec_memory_pointer_two
dec_memory_pointer_two_ret13:
BF_INCREMENTING_NEGATIVE_CELL cell_to_zero R_BF_INCREMENTING_NEGATIVE_CELL
R_RESET_BF_MC_FLAG11 // like uninitialized cell!
MOVD reset_bf_memory_cell
cell_to_zero:
R_BF_INCREMENTING_NEGATIVE_CELL
R_RESET_BF_MC_FLAG1 // like uninitialized cell!
MOVD reset_bf_memory_cell
BF_STARTLOOP:
R_MOVDMOVD ?- ?- ?- ?-
R_MOVD
MOVE_TO_BF_STOPLOOP_FLAG bf_startloop_stoploop_searching R_MOVE_TO_BF_STOPLOOP_FLAG
MOVE_TO_BF_STARTLOOP_FLAG bf_startloop_startloop_searching R_MOVE_TO_BF_STARTLOOP_FLAG
// NOW: normal processing of command: test current cell for zero
R_RESET_VAL_FLAG13
MOVD reset_val
bf_startloop_stoploop_searching:
// (restore FLAG)
R_MOVE_TO_BF_STOPLOOP_FLAG
// increment bf_nesting_counter
R_INCREMENT_BF_NC_FLAG2
MOVD increment_bf_nesting_counter
increment_bf_nesting_counter_ret2:
MOVD BF_NEXT_CMD
bf_startloop_startloop_searching:
// decrement bf_nesting_counter
R_DECREMENT_BF_NC_FLAG2
MOVD decrement_bf_nesting_counter
decrement_bf_nesting_counter_ret2:
CARRY bf_startloop_found
// (resore MOVE_TO_BF_STARTLOOP_FLAG)
R_MOVE_TO_BF_STARTLOOP_FLAG
MOVD BF_MOVE_BACKWARDS
bf_startloop_found:
MOVD BF_NEXT_CMD
reset_val_ret13:
// read_bf_memory_cell
R_READ_BF_MC_FLAG9
MOVD read_bf_memory_cell
read_bf_memory_cell_ret9:
// write_val
R_WRITE_VAL_FLAG13
MOVD write_val
write_val_ret13:
// dec_val
R_DEC_VAL_FLAG11
MOVD dec_val
dec_val_ret11:
CARRY bf_startloop_initialized_cell
// cell is zero or negative
// dec_val
R_DEC_VAL_FLAG34
MOVD dec_val
dec_val_ret34:
CARRY dec_memory_pointer_two_ret7 // go into loop
// cell is zero => FINE!!!
MOVD bf_startloop_do_skip
bf_startloop_initialized_cell:
// cell is initialized
// inc_memory_pointer_two
R_INC_MP_FLAG7
MOVD inc_memory_pointer_two
inc_memory_pointer_two_ret7:
// reset_val
R_RESET_VAL_FLAG14
MOVD reset_val
reset_val_ret14:
// read_bf_memory_cell
R_READ_BF_MC_FLAG10
MOVD read_bf_memory_cell
read_bf_memory_cell_ret10:
// write_val
R_WRITE_VAL_FLAG14
MOVD write_val
write_val_ret14:
// dec_val
R_DEC_VAL_FLAG12
MOVD dec_val
dec_val_ret12:
CARRY bf_startloop_underflow
// cell is NOT zero
R_DEC_MP_FLAG7
MOVD dec_memory_pointer_two
dec_memory_pointer_two_ret7:
MOVD BF_NEXT_CMD
bf_startloop_underflow:
R_DEC_MP_FLAG10
MOVD dec_memory_pointer_two
bf_startloop_do_skip:
R_MOVD
dec_memory_pointer_two_ret10:
// -> set bf_nesting_counter to 0
// set MOVE_TO_BF_STOPLOOP_FLAG
// MOVD BF_NEXT_CMD
//
R_RESET_BF_NC_FLAG3
MOVD reset_bf_nesting_counter
reset_bf_nesting_counter_ret3:
ROT C0 R_ROT
// write_bf_nesting_counter
R_WRITE_BF_NC_FLAG3
MOVD write_bf_nesting_counter
write_bf_nesting_counter_ret3:
bf_startloop_set_mv:
MOVE_TO_BF_STOPLOOP_FLAG bf_startloop_set_mv
MOVD BF_NEXT_CMD
BF_ENDLOOP:
R_MOVDMOVD ?- ?- ?- ?-
R_MOVD
// if MOVE_TO_BF_STARTLOOP_FLAG
// (restore FLAG)
// increment bf_nesting_counter
// MOVD BF_MOVE_BACKWARDS
// endif
// if MOVE_TO_BF_STOPLOOP_FLAG
// decrement bf_nesting_counter
// if CARRY
// continue execution here
// MOVD BF_NEXT_CMD
// else
// (resore MOVE_TO_BF_STOPLOOP_FLAG)
// MOVD BF_NEXT_CMD
// endif
// endif
// check current memory cell for 0, go back to STARTLOOP if it is nonzero.
//
MOVE_TO_BF_STOPLOOP_FLAG bf_endloop_stoploop_searching R_MOVE_TO_BF_STOPLOOP_FLAG
MOVE_TO_BF_STARTLOOP_FLAG bf_endloop_startloop_searching R_MOVE_TO_BF_STARTLOOP_FLAG
// reset_val
R_RESET_VAL_FLAG15
MOVD reset_val
bf_endloop_stoploop_searching:
// decrement bf_nesting_counter
R_DECREMENT_BF_NC_FLAG1
MOVD decrement_bf_nesting_counter
decrement_bf_nesting_counter_ret1:
CARRY bf_endloop_found
// (resore MOVE_TO_BF_STARTLOOP_FLAG)
R_MOVE_TO_BF_STOPLOOP_FLAG
MOVD BF_NEXT_CMD
// endif
bf_endloop_found:
// -> continue execution here
MOVD BF_NEXT_CMD
bf_endloop_startloop_searching:
// (restore FLAG)
R_MOVE_TO_BF_STARTLOOP_FLAG
// increment bf_nesting_counter
R_INCREMENT_BF_NC_FLAG1
MOVD increment_bf_nesting_counter
increment_bf_nesting_counter_ret1:
MOVD BF_MOVE_BACKWARDS
reset_val_ret15:
// read_bf_memory_cell
R_READ_BF_MC_FLAG11
MOVD read_bf_memory_cell
read_bf_memory_cell_ret11:
// write_val
R_WRITE_VAL_FLAG15
MOVD write_val
write_val_ret15:
// dec_val
R_DEC_VAL_FLAG13
MOVD dec_val
dec_val_ret13:
CARRY bf_endloop_initialized_cell
// dec_val
R_DEC_VAL_FLAG35
MOVD dec_val
dec_val_ret35:
CARRY dec_memory_pointer_two_ret8 // negative cell
// cell is zero
MOVD BF_NEXT_CMD
bf_endloop_underflow:
R_DEC_MP_FLAG9
MOVD dec_memory_pointer_two
dec_memory_pointer_two_ret9:
MOVD BF_NEXT_CMD
bf_endloop_initialized_cell:
// cell is initialized
// inc_memory_pointer_two
R_INC_MP_FLAG8
MOVD inc_memory_pointer_two
inc_memory_pointer_two_ret8: