├── INSTALL
├── .gitignore
├── src
├── Makefile
├── globals.h
├── malbolge.c
├── initialize.h
├── main.h
├── malbolge.h
├── gen_init.h
├── lmao.l
├── typedefs.h
├── initialize.c
└── lmao.y
├── Makefile
├── datamodule.txt
├── example_simple_cat.hell
├── CHANGELOG
├── example_simple_hello_world.hell
├── README
├── example_hello_world.hell
├── example_cat_halt_on_eof.hell
├── example_digital_root.hell
├── example_adder.hell
└── LICENSE
/INSTALL:
--------------------------------------------------------------------------------
1 | You need bison, flex, gcc, and make to build LMAO.
2 |
3 | Simply call make to build the lmao binary.
4 | Then type ./lmao to run it.
5 |
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | lmao
3 | *.mb
4 | *.dbg
5 | bin/
6 | .cproject
7 | .project
8 | .settings
9 | doc/
10 | lmao.tar.gz
11 | Debug/
12 | *.user
13 | *.ncb
14 | *.sln
15 | *.suo
16 | *.vcproj
17 |
18 |
--------------------------------------------------------------------------------
/src/Makefile:
--------------------------------------------------------------------------------
1 | CC=gcc
2 | CFLAGS=-O3 -Wall -c -I.
3 | LEX=flex
4 | YACC=bison -d -v
5 | OBJ=../bin/bison.o ../bin/lex.o ../bin/gen_init.o ../bin/initialize.o ../bin/main.o ../bin/malbolge.o
6 |
7 | all: lmao
8 |
9 | ../bin:
10 | mkdir ../bin/
11 |
12 | ../bin/lmao.tab.c: lmao.y
13 | $(YACC) -o ../bin/lmao.tab.c lmao.y
14 |
15 | ../bin/bison.o: ../bin/lmao.tab.c
16 | $(CC) $(CFLAGS) -o ../bin/bison.o ../bin/lmao.tab.c
17 |
18 | ../bin/lex.yy.c: lmao.l ../bin/lmao.tab.c
19 | $(LEX) -o ../bin/lex.yy.c lmao.l
20 |
21 | ../bin/lex.o: ../bin/lex.yy.c
22 | $(CC) $(CFLAGS) -o ../bin/lex.o ../bin/lex.yy.c
23 |
24 | ../bin/%.o: %.c *.h
25 | $(CC) $(CFLAGS) -o $@ $<
26 |
27 | lmao: ../bin $(OBJ)
28 | gcc $(OBJ) -o ../bin/lmao
29 |
30 | clean:
31 | rm -rf ../bin/
32 |
33 |
--------------------------------------------------------------------------------
/src/globals.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | #ifndef GLOBALSINCLUDED
24 | #define GLOBALSINCLUDED
25 | extern int debug_mode;
26 | #endif
27 |
28 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | lmao: bin/lmao
2 | cp bin/lmao .
3 |
4 | all: lmao doc
5 |
6 | doc: Doxyfile src/*.c src/*.h
7 | doxygen Doxyfile
8 |
9 | bin/lmao: src/*.c src/*.h src/lmao.y src/lmao.l
10 | make -C src/
11 |
12 | install: lmao
13 | install -Dm755 ./lmao $(DESTDIR)/usr/bin/lmao
14 | install -Dm644 ./example_cat_halt_on_eof.hell $(DESTDIR)/usr/share/lmao/example_cat_halt_on_eof.hell
15 | install -Dm644 ./example_hello_world.hell $(DESTDIR)/usr/share/lmao/example_hello_world.hell
16 | install -Dm644 ./example_simple_hello_world.hell $(DESTDIR)/usr/share/lmao/example_simple_hello_world.hell
17 | install -Dm644 ./example_simple_cat.hell $(DESTDIR)/usr/share/lmao/example_simple_cat.hell
18 | install -Dm644 ./example_digital_root.hell $(DESTDIR)/usr/share/lmao/example_digital_root.hell
19 | install -Dm644 ./example_adder.hell $(DESTDIR)/usr/share/lmao/example_adder.hell
20 |
21 | uninstall:
22 | rm -f $(DESTDIR)/usr/bin/lmao
23 | rm -f $(DESTDIR)/usr/share/lmao/example_cat_halt_on_eof.hell
24 | rm -f $(DESTDIR)/usr/share/lmao/example_hello_world.hell
25 | rm -f $(DESTDIR)/usr/share/lmao/example_simple_hello_world.hell
26 | rm -f $(DESTDIR)/usr/share/lmao/example_simple_cat.hell
27 | rm -f $(DESTDIR)/usr/share/lmao/example_digital_root.hell
28 | rm -f $(DESTDIR)/usr/share/lmao/example_adder.hell
29 | rmdir $(DESTDIR)/usr/share/lmao/
30 |
31 | clean:
32 | make -C src/ clean
33 | rm -f ./lmao
34 | rm -rf doc/
35 |
36 |
--------------------------------------------------------------------------------
/datamodule.txt:
--------------------------------------------------------------------------------
1 |
2 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
3 | Copyright (C) 2013 Matthias Ernst
4 |
5 | LMAO is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | LMAO is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 |
18 | E-Mail: info@matthias-ernst.eu
19 |
20 |
21 | Constant generation data module:
22 |
23 | 33: C0
24 | 34: C1
25 | 35: C21
26 | 36: VALUE1
27 | 37: C2
28 | 38: 35
29 | 39: 32
30 | 40: 93
31 |
32 | 48: C0
33 | 49: C1
34 | 50: C21
35 | 51: VALUE3
36 | 52: C2
37 | 53: 50
38 | 54: 47
39 | 55: 93
40 |
41 | 71: C0
42 | 72: C1
43 | 73: C21
44 | 74: VALUE2
45 | 75: C2
46 | 76: 73
47 | 77: 70
48 | 78: 93
49 |
50 | 82: C0
51 | 83: 93
52 | 84: 33
53 | 85: 71
54 | 86: 48
55 | 87: C1
56 | 88: TMP (all trits must be 0 or 1)
57 | 89: 0t2222000022
58 | 90: 0t2222200022
59 | 91: 0t2222220022
60 | 92: 0t2222222022
61 | 93: C2
62 | 94: DESTINATION
63 | 95: 87
64 | 96: 81
65 |
66 |
--------------------------------------------------------------------------------
/src/malbolge.c:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | #include "malbolge.h"
24 |
25 | unsigned int crazy(unsigned int a, unsigned int d){
26 | unsigned int crz[] = {1,0,0,1,0,2,2,2,1};
27 | int position = 0;
28 | unsigned int output = 0;
29 | while (position < 10){
30 | unsigned int i = a%3;
31 | unsigned int j = d%3;
32 | unsigned int out = crz[i+3*j];
33 | unsigned int multiple = 1;
34 | int k;
35 | for (k=0;k.
17 | *
18 | * E-Mail: matthias@lutter.cc
19 | *
20 | *
21 | * Example in HeLL: simple cat
22 | */
23 |
24 | .CODE
25 | // loop-resistant commands MOVD, IN, OUT
26 | MOVD:
27 | MovD/Nop
28 | Jmp
29 |
30 | IN:
31 | In/Nop
32 | Jmp
33 |
34 | OUT:
35 | Out/Nop
36 | Jmp
37 |
38 | .DATA {
39 | // The program logic follows here. Repeat IN/OUT in an infinite loop.
40 |
41 | loop:
42 | // restore MOVD-command
43 | R_MOVD
44 |
45 | // program starts here
46 | ENTRY:
47 | //read input character from stdin to A register;
48 | IN ?-
49 | // restore input command
50 | R_IN
51 | // write A register to stdout
52 | OUT ?-
53 | // restore output command
54 | R_OUT
55 | // infinite loop
56 | MOVD loop
57 | }
58 |
--------------------------------------------------------------------------------
/CHANGELOG:
--------------------------------------------------------------------------------
1 | v0.5.6b
2 | - Continue building if problems occur initially. This may increase the
3 | chance for building large Malbolge programs near the size limit.
4 | - Revised README file.
5 |
6 | v0.5.6
7 | - Added more debugging information.
8 |
9 | v0.5.5
10 | - Bugfix
11 |
12 | v0.5.4
13 | - Slightly improved handling of fixed offsets in CODE section.
14 |
15 | v0.5.3
16 | - Automatic disabling of fast mode on failure.
17 |
18 | v0.5.2
19 | - Slightly improved handling of fixed offsets.
20 |
21 | v0.5.1
22 | - If LMAO writes debugging information, it reserves memory cells for
23 | unused commands "?-" (considers them as "?").
24 | This may make Malbolge programs a litte bit larger, but debugging
25 | them with HeLL IDE becomes a bit easier.
26 |
27 | v0.5
28 | - LMAO can write debugging information now (e.g. for HeLL IDE)
29 |
30 | v0.4.4
31 | - Added @ (short for .OFFSET) and braces for block identification to HeLL
32 | - Added line numbers to error messages
33 |
34 | v0.4.3
35 | - Decreased size of generated programs a little bit
36 |
37 | v0.4.2
38 | - Added support for calculation operators with label names
39 |
40 | v0.4.1
41 | - Added command line argument for line length
42 | - Command line argument for output file name is not required any more
43 | - Decreased size of generated programs a little bit
44 | - Doxygen documentation
45 | - Fixed bugs
46 |
47 | v0.4
48 | - Added operators for constant calculation
49 | - Changed symbol for unused memory cell from "-" to "?-"
50 | - Added support for Strings in HeLL program files
51 |
52 | v0.3.2
53 | - Decreased size of generated programs
54 |
55 | v0.3.1
56 | - Fixed a bug
57 |
58 | v0.3
59 | - Added new data module for constant generation to decrease size of
60 | (complex) programs
61 |
62 | v0.2.1
63 | - Decreased size of generated programs
64 |
65 | v0.2
66 | - Decreased size of generated programs
67 |
68 | v0.1.1
69 | - Fixed a bug that may occur if .OFFSET is used in the code section
70 | - Added example code in HeLL for a cat program that halts on EOF
71 |
72 | v0.1
73 | - Basic implementation
74 |
75 |
--------------------------------------------------------------------------------
/example_simple_hello_world.hell:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an
3 | * assembler for Malbolge.
4 | * Copyright (C) 2013-2017 Matthias Lutter
5 | *
6 | * LMAO is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * LMAO is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with this program. If not, see .
18 | *
19 | * E-Mail: matthias@lutter.cc
20 | *
21 | *
22 | * Example in HeLL: Printing out a string without loops.
23 | *
24 | * This HeLL program prints out a string by reading its characters with
25 | * OPR and ROT commands. The characters are modified, so the string cannot
26 | * be printed out twice by looping.
27 | * This consumes much more memory of the virtual Malbolge machine than
28 | * other well known methods for printing out a string in Malbolge.
29 | * For advanced string output, see example_hello_world.hell.
30 | */
31 |
32 | .CODE
33 | // loop-resistant commands CRAZY, ROT, OUT
34 | CRAZY:
35 | Opr/Nop
36 | Jmp
37 |
38 | ROT:
39 | Rot/Nop
40 | Jmp
41 |
42 | OUT:
43 | Out/Nop
44 | Jmp
45 |
46 | // the following commands are executed once and need not be loop-resistant
47 | OUTPUT_WORLD:
48 | Rot
49 | Out
50 | Rot
51 | Out
52 | Rot
53 | Out
54 | Rot
55 | Out
56 | Rot
57 | Out
58 | Rot
59 | Out
60 | Rot
61 | Out
62 | Hlt
63 |
64 | .DATA
65 | // The program logic follows here. Read characters and print them out.
66 | ENTRY:
67 | ROT C2 R_ROT
68 | CRAZY C2!'H' R_CRAZY
69 | OUT ?- R_OUT
70 | ROT 'e'<<1 R_ROT
71 | OUT ?- R_OUT
72 | ROT C2 R_ROT
73 | CRAZY C2!('l'!C1!C1) R_CRAZY
74 | OUT ?- R_OUT
75 | OUT ?- R_OUT
76 | ROT 'o'>>9 R_ROT
77 | OUT ?- R_OUT
78 | ROT ','<<1 R_ROT
79 | OUT ?- R_OUT
80 | ROT ' '<<1 R_ROT
81 | OUT ?- R_OUT
82 | OUTPUT_WORLD
83 | 'W'<<1 ?- 'o'<<1 ?- 'r'<<1 ?- 'l'<<1 ?- 'd'<<1 ?- '!'<<1 ?- '\n'<<1
84 |
85 |
--------------------------------------------------------------------------------
/src/initialize.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | #ifndef INITIALIZEINCLUDED
24 | #define INITIALIZEINCLUDED
25 |
26 | #include "typedefs.h"
27 |
28 | /**
29 | * This function sets the offsets stored in the codeblocks and datablocks to fit to the given memory layout.
30 | *
31 | * \param memory Array of GeneralMemoryCells corresponding to current memory layout. This array must contain exactly C2+1=59049 elements. The offset of each codeblock and datablock referenced by the memory cells will be updated.
32 | */
33 | void update_offsets(MemoryCell* memory);
34 |
35 | /**
36 | * \brief Generates opcodes for memory cells of the final initialized Malbolge program.
37 | *
38 | * After that step the initialization code can be generated to get the final Malbolge code.
39 | *
40 | * \param memory_layout Memory layout that should be converted into opcodes. The size of this array must be C2+1=59049.
41 | * \param last_preinitialized Address of last memory cell that will be initialized directly after the Malbolge interpreter read in the source file.
42 | * \param opcodes The generated opcodes will be written into this array. If no opcode is generated for a memory cell, it will be set to -1. The size of this array must be C2+1=59049.
43 | * \param no_error_printing If set to non-zero, error messages won't be printed to stdout.
44 | * \return If an error occurs, zero will be returned. Otherwise, a non-zero value will be returned.
45 | */
46 | int generate_opcodes_from_memory_layout(MemoryCell* memory_layout, int last_preinitialized, int* opcodes, LabelTree* labeltree, int no_error_printing, int ignore_fixed_offsets_in_preinitialized_section);
47 |
48 | /**
49 | * \brief This function creates a Malbolge program from given memory cells.
50 | *
51 | * Most cell values won't be valid initial values for Malbolge, so this function generates code to initiate that cells to their values.
52 | * \param program TODO
53 | * \param last_preinitialized TODO
54 | * \param labeltree TODO
55 | * \param entrypoint TODO
56 | * \param malbolge_code TODO
57 | * \param no_error_printing TODO
58 | * \return -1 on error; otherwise: size needed for pure initialization code (without NOPs used for filling up; without preinitialized code words.)
59 | */
60 | int generate_malbolge_initialization_code(int program[], int last_preinitialized, int entrypoint, char malbolge_code[], int no_error_printing, int* execution_steps_until_entry_point, int ignore_wrong_size);
61 |
62 | #endif
63 |
64 |
--------------------------------------------------------------------------------
/src/main.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | #ifndef MAININCLUDED
24 | #define MAININCLUDED
25 |
26 | #include "typedefs.h"
27 | #include
28 |
29 | /**
30 | * Searches DataBlock or CodeBlock referenced by a given string in the LabelTree.
31 | *
32 | * \param destination_code The pointer to the CodeBlock will be written here. If the label is defined in the data section, NULL will be written here.
33 | * If the label is expected to be defined in the data section, NULL can be passed for this parameter.
34 | * If this parameter is NULL and the label is defined in the code section, this function will fail.
35 | * \param destination_data The pointer to the DataBlock will be written here. If the label is defined in the code section, NULL will be written here.
36 | * If the label is expected to be defined in the code section, NULL can be passed for this parameter.
37 | * If this parameter is NULL and the label is defined in the data section, this function will fail.
38 | * \param label Label string that should be searched in the LabelTree.
39 | * \param labeltree LabelTree that should be searched.
40 | * \return One if the Block has been found. Zero otherwise.
41 | */
42 | int get_label(CodeBlock** destination_code, DataBlock** destination_data, const char* label, LabelTree* labeltree);
43 |
44 | /**
45 | * Finds out if a character can be placed at a specific position in the Malbolge program code or not.
46 | *
47 | * \param position Desired position for the character.
48 | * \param character Desired character.
49 | * \return If the character value cannot be placed into the Malbolge program code directly and has to be generated during Malbolge program execution, this funciton returns 0.
50 | * Otherwise it returns a non-zero value.
51 | */
52 | int is_valid_initial_character(int position, char character);
53 |
54 | /**
55 | * Gets a single xlat2 cycle and its position. Computates whether the given xlat2 cycle is possible at the given position or not.
56 | * If start_symbol is not null and the gevien xlat2 cycle exists, the first ASCII value of the xlat2 cycle will be written to *start_symbol.
57 | *
58 | * \param cycle Xlat2 cycle that should be checked for existence.
59 | * \param position Desired position of the xlat2 cycle at the Malbolge virtual machine memory.
60 | * \param start_symbol An ASCII character that will result in the given xlat2 cycle will be written here if such a cycle exists.
61 | * If this pointer is set to NULL, the ASCII character won't be written.
62 | * \return If the given xlat2 cycle exists at the given position, this funciton returns a non-zero value. Otherwise zero is returned.
63 | */
64 | int is_xlatcycle_existent(XlatCycle* cycle, int position, char* start_symbol);
65 |
66 | /* debug-informations */
67 | void print_labeltree(FILE* destination, LabelTree* labeltree);
68 | void print_source_positions(FILE* destination, MemoryCell memory[C2+1]);
69 | void print_xlat2_positions(FILE* destination, MemoryCell memory[C2+1]);
70 |
71 | #endif
72 |
73 |
--------------------------------------------------------------------------------
/src/malbolge.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | #ifndef MALBOLGEINCLUDED
24 | #define MALBOLGEINCLUDED
25 |
26 | /**
27 | * Trinary constant: 0t0000000000
28 | */
29 | #define C0 0
30 |
31 | /**
32 | * Trinary constant: 0t1111111111
33 | */
34 | #define C1 29524
35 |
36 | /**
37 | * Trinary constant: 0t2222222222
38 | */
39 | #define C2 59048
40 |
41 | /**
42 | * Trinary constant: 0t2222222221
43 | */
44 | #define C21 (C2-1)
45 |
46 | /**
47 | * Malbolge opcode for Nop command.
48 | *
49 | * If executed by the Malbolge interpreter, Nop does not causes the interpreter to do anything.
50 | */
51 | #define MALBOLGE_COMMAND_NOP 68
52 |
53 | /**
54 | * Malbolge opcode for MovD command.
55 | *
56 | * If executed by the Malbolge interpreter, MovD causes the interpreter to set the D register to the value of [D].
57 | */
58 | #define MALBOLGE_COMMAND_MOVED 40
59 |
60 | /**
61 | * Malbolge opcode for Opr command.
62 | *
63 | * If executed by the Malbolge interpreter, Opr causes the interpreter to execute the crazy function with the values of the A register and [D]. The result will be stored in the A register and in [D].
64 | * \sa crazy(unsigned int, unsigned int)
65 | */
66 | #define MALBOLGE_COMMAND_OPR 62
67 |
68 | /**
69 | * Malbolge opcode for Jmp command.
70 | *
71 | * If executed by the Malbolge interpreter, Jmp causes the interpreter to set the C register to the value of [D].
72 | */
73 | #define MALBOLGE_COMMAND_JMP 4
74 |
75 | /**
76 | * Malbolge opcode for Rot command.
77 | *
78 | * If executed by the Malbolge interpreter, Rot causes the interpreter to rotate [A] tritwise right and copy the result to the A register.
79 | * \sa rotate_right(unsigned int)
80 | */
81 | #define MALBOLGE_COMMAND_ROT 39
82 |
83 | /**
84 | * Malbolge opcode for Out command.
85 | *
86 | * If executed by the Malbolge interpreter, Out causes the interpreter to write an ASCII character from the A register modulo 256 to stdout.
87 | */
88 | #define MALBOLGE_COMMAND_OUT 5
89 |
90 | /**
91 | * Malbolge opcode for In command.
92 | *
93 | * If executed by the Malbolge interpreter, In causes the interpreter to read an ASCII character from stdin and stores it in the A register.
94 | * If end-of-file is read, A is set to C2=59048.
95 | */
96 | #define MALBOLGE_COMMAND_IN 23
97 |
98 | /**
99 | * Malbolge opcode for Hlt command.
100 | *
101 | * If executed by the Malbolge interpreter, Hlt causes the interpreter to stop execution and terminate.
102 | */
103 | #define MALBOLGE_COMMAND_HALT 81
104 |
105 | /**
106 | * xlat2 translation.
107 | */
108 | #define XLAT2 "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1CB6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@"
109 |
110 | /**
111 | * Malbolge crazy operator.
112 | *
113 | * \param a TODO
114 | * \param d TODO
115 | * \return TODO
116 | */
117 | unsigned int crazy(unsigned int a, unsigned int d);
118 |
119 | /**
120 | * Tritwise rotation.
121 | *
122 | * \param d TODO
123 | * \return TODO
124 | */
125 | unsigned int rotate_right(unsigned int d);
126 |
127 | #endif
128 |
129 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | LMAO (Low-level Malbolge Assembler, Ooh!) is an assembler for Malbolge.
2 | It takes a program written in HeLL (Hellish Low-level Language) and
3 | converts it to Malbolge.
4 |
5 | Usage: ./lmao [options]
6 |
7 | Unfortunately, there is no full specification of the HeLL programming
8 | language. However, at least a short overview is given at the end of this
9 | file.
10 |
11 | To start with HeLL, the following resources are recommended:
12 |
13 | - Beginner's tutorial:
14 | https://lutter.cc/malbolge/tutorial/cat.html
15 |
16 | - Examples written in HeLL that come with LMAO:
17 | - example_simple_cat.hell
18 | - example_simple_hello_world.hell
19 | - example_cat_halt_on_eof.hell
20 | - example_hello_world.hell
21 | - example_digital_root.hell
22 | - example_adder.hell
23 | The ordering above matches their complexity.
24 |
25 |
26 | Short overview of HeLL
27 | ----------------------
28 |
29 | Sections:
30 | ".CODE" and ".DATA"
31 |
32 | Comments:
33 | One line: start comment with ";", "%", "#", or "//"
34 | Multi-line: start comment with "/*" and end with "*/"
35 |
36 | Blocks:
37 | Separated by blank lines that do not even contain a comment.
38 | Alternatively: surrounded by "{" and "}"
39 |
40 | At the begin of each block, the ".OFFSET" directive, or "@" for short,
41 | may be used. It must be followed by a number.
42 | Only use if really necessary.
43 | Examples: "@C1", ".OFFSET 40000".
44 |
45 | Labels: <> followed by ":"
46 |
47 | Each block must start with a label (or with an offset definition
48 | followed by a label).
49 |
50 |
51 | .DATA cells may contain:
52 | ------------------------
53 |
54 | Numbers:
55 | Decimal numbers (plain), ternary numbers (preceeded by "0t"), or one of
56 | the following constants:
57 | - "C0" = 0t0000000000 = 0
58 | - "C1" = 0t1111111111 = 29524
59 | - "C2" = 0t2222222222 = 59048
60 | - "C20" = 0t2222222220 = 59046
61 | - "C21" = 0t2222222221 = 59047
62 | - "EOF" = "C2"
63 | ASCII characters enquoted with single quotes "'" can be used as well.
64 |
65 | Operators:
66 | Plus (+), Minus (-), Times (*), Divided (/),
67 | Rotate Tritwise Right (>>), Rotate Tritwise Left (<<), Crazy (!)
68 | Example for using operators: (0t1210>>1)-1
69 |
70 | Don't care symbols:
71 | "?" or "?-"
72 | "?" reserves the cell, but the initial value is irrelevant.
73 | "?-" is an unused cell that may be used otherwise. Never access this
74 | cell in any way, or you may cause undefined behavior. Must not follow a
75 | label definition (in this case, use "?" instead).
76 |
77 | References to label:
78 | References may (but need not) be prefixed by a "R_" or "U_" modifier.
79 | The "R_" modifier is the same as incrementing the value by one.
80 | If the "U_" modifier is used, the prefixed reference must be followed
81 | by a further reference. The second reference must point to a label
82 | within the same block at a later memory position. The distance to this
83 | label is subtracted from the prefixed reference.
84 | Example for "U_" prefix: "U_ref foo 0 0 foo: 1"
85 | The value of 2 is subtracted from "ref".
86 |
87 | Strings:
88 | Use quotes. Supported escape sequences:
89 | "\r", "\n", "\t", "\\", "\"" "\0".
90 | The string may be followed by a comma and a value. In this case,
91 | the value will be placed between each character of the string.
92 | Example 1: "Test" is equivalent to 'T' 'e' 's' 't'
93 | Example 2: "Test", 42 is equivalent to 'T' 42 'e' 42 's' 42 't'
94 |
95 | Important note:
96 | Within the .DATA section, a label called "ENTRY" must be defined.
97 | The D register will point at this position when the program starts.
98 | The C register will point at any Jmp instruction.
99 | The value of the A register is not defined.
100 |
101 |
102 | .CODE cells must only contain xlat2 cycle descriptions
103 | ------------------------------------------------------
104 |
105 | xlat2 cycle description:
106 | Either a single command (for non loop-resistant cells) or a cycle of
107 | commands separated by "/".
108 |
109 | Commands:
110 | "Nop", "Jmp", "MovD", "Opr", "Rot", "In", "Out", "Hlt"
111 |
112 | Abbreviation for "Nop/Nop": "RNop" (loop-*r*esistant _Nop_)
113 |
114 |
--------------------------------------------------------------------------------
/src/gen_init.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | /*
24 | * This file provides generation of Malbolge initialization code.
25 | * The Malbolge initialization code is necessary to initialize cells used by the Malbolge program with arbitrary values and is executed at the beginning.
26 | * It ends with a jump to the entry point of the converted HeLL program.
27 | */
28 |
29 | #ifndef GENINITINCLUDED
30 | #define GENINITINCLUDED
31 |
32 | /**
33 | * TODO
34 | */
35 | typedef struct DRegPos {
36 | /**
37 | * -1 if pos is absolute address; 0-3 if pos is relative address
38 | */
39 | int module;
40 | /**
41 | * TODO
42 | */
43 | int pos;
44 | } DRegPos;
45 |
46 | /**
47 | * TODO
48 | */
49 | #define CELLTYPE_PTR 0
50 |
51 | /**
52 | * TODO
53 | */
54 | #define CELLTYPE_CONST 1
55 |
56 | /**
57 | * TODO
58 | */
59 | #define CELLTYPE_VAR 2
60 |
61 | /**
62 | * TODO
63 | */
64 | #define CELLTYPE_C20_OR_C21 3
65 |
66 | /**
67 | * TODO
68 | */
69 | typedef struct Cell {
70 | /**
71 | * TODO
72 | */
73 | int type;
74 | /**
75 | * TODO
76 | */
77 | int value; /* if type != PTR */
78 | /**
79 | * TODO
80 | */
81 | DRegPos destination; /* if type == PTR */
82 | } Cell;
83 |
84 | /**
85 | * This struct stores the values of the memory cells that are related to a data module.
86 | */
87 | typedef struct Module {
88 | /**
89 | * Number of cells in the data module. Must not exceed 15.
90 | */
91 | int num_of_cells;
92 | /**
93 | * Current value of the cells. Only the first num_of_cells entries will be used.
94 | */
95 | Cell cells[15]; /* maximum number used at the moment */
96 | } Module;
97 |
98 | /**
99 | * This struct represents the state of the constant generation data module during execution of the Malbolge initialization code.
100 | */
101 | typedef struct State {
102 | /**
103 | * Content of the A register.
104 | */
105 | int a_reg;
106 | /**
107 | * Position the D register points to.
108 | */
109 | DRegPos d_reg;
110 | /**
111 | * \brief State of the memory cells that belong to the constant generation data module.
112 | *
113 | * The constant generation data module is is composed of multiple data modules. The states of all these data modules are stored in this array.
114 | */
115 | Module modules[4]; /* 4 modules at the moment */
116 | /**
117 | * Address of the last preinitialized cell. Memory cells behing this cell are assumed to store the value 81 or (C1-81) dependend on their distance to the last_preinitialized memory cell.
118 | * The value of memory cells outside the constant generation data module with a lower offset than last_preinitialized is not stored by this struct, so their value is completely unknown.
119 | */
120 | int last_preinitialized; /* address of last preinitialized cell */
121 | } State;
122 |
123 | /**
124 | * Converts normalized Malbolge code to denormalized Malbolge code that can be executed by the Malbolge interpreter.
125 | *
126 | * \param normalized_code Buffer with normalized Malbolge code. It will be replaced by the denormalized code. This buffer must contain only these characters any number of times: 'o', 'j', 'p', '*', 'i', '<', '/', and/or 'v'
127 | * \param normalized_code_offset Offset of the given normalized code in the Malbolge program. This information is necessary for denormalizing code that does not start at offset 0 because the ASCII characters of denormalized code are dependent on their position.
128 | * \param code_len Number of bytes that should be denormalized.
129 | * \param no_error_printing TODO
130 | * \return A non-zero value is returned in the case that this function succeeds. Otherwise, zero is returned.
131 | */
132 | int denormalize_malbolge(char* normalized_code, int normalized_code_offset, int code_len, int no_error_printing);
133 |
134 | /**
135 | * Generates a sequence of normalized Malbolge commands that initializes a given memory cell with a given value using the data module for constant generation.
136 | * The state of the data module may be changed by this function. The given memory cell and all memory cells behind the given cell must not be initialized. Their value must be 81 or (C1-81) depending on their distance to the last_preinitialized field in State.
137 | * The generated Malbolge code only works with the correct data module and if the state of the virtual Malbolge machine is equivalent to the state given to this function right before executing the first command of the code that will be generated by this function.
138 | *
139 | * \param init_position Position of the memory cell that should be initialized.
140 | * \param init_value Target value of the memory cell.
141 | * \param normalized_init_code The generated normalized Malbolge code will be written to this buffer.
142 | * \param current_state Current state of the data module in the virtual Malbolge machine right before executing the code that will be generated by this function.
143 | * \param max_init_code_length Size of the normalized_init_code buffer.
144 | * \param no_error_printing TODO
145 | * \return Length of generated Malbolge initialization code for the given memory cell or -1 if an error occurred.
146 | */
147 | int generate_normalized_init_code_for_word_with_module_system(int init_position, int init_value, char* normalized_init_code, State* current_state, int max_init_code_length, int no_error_printing);
148 |
149 | /**
150 | * TODO
151 | *
152 | * \param entry_point TODO
153 | * \param normalized_init_code TODO
154 | * \param current_state TODO
155 | * \param max_init_code_length TODO
156 | * \param no_error_printing TODO
157 | * \return length of generated init code or -1 if an error occurs.
158 | */
159 | int generate_jump_to_entrypoint_with_module_system(int entry_point, char* normalized_init_code, State* current_state, int max_init_code_length, int no_error_printing);
160 |
161 | #endif
162 |
163 |
--------------------------------------------------------------------------------
/src/lmao.l:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | %option noyywrap
24 |
25 | %{
26 | #include "malbolge.h"
27 | #include "typedefs.h"
28 | #include "globals.h"
29 |
30 | #include "lmao.tab.h"
31 |
32 | #define YYABORT return 1;
33 |
34 |
35 | #ifndef YYLTYPE_IS_DECLARED
36 | typedef struct YYLTYPE
37 | {
38 | int first_line;
39 | int first_column;
40 | int last_line;
41 | int last_column;
42 | } YYLTYPE;
43 | #define YYLTYPE_IS_DECLARED
44 | #endif
45 | extern YYLTYPE yylloc;
46 |
47 |
48 | YYLTYPE last_curly_bracket;
49 |
50 | #define COPY_CODE_POSITION(to, from_start, from_end) {(to).first_line = (from_start).first_line;(to).first_column = (from_start).first_column;(to).last_line = (from_end).last_line;(to).last_column = (from_end).last_column;};
51 |
52 | static void update_loc(){
53 | static int curr_line = 1;
54 | static int curr_col = 1;
55 |
56 | yylloc.first_line = curr_line;
57 | yylloc.first_column = curr_col;
58 |
59 | {char * s; for(s = yytext; *s != '\0'; s++){
60 | if(*s == '\n'){
61 | curr_line++;
62 | curr_col = 1;
63 | }else{
64 | curr_col++;
65 | }
66 | }}
67 |
68 | yylloc.last_line = curr_line;
69 | yylloc.last_column = curr_col-1;
70 | }
71 |
72 | #define YY_USER_ACTION update_loc();
73 |
74 |
75 | int yyerror(const char *s);
76 |
77 | /* force whitespaces if needed. */
78 | int require_whitespace = 0;
79 | int in_data_section = 0;
80 | #define CHECK_WHITESPACE() if(require_whitespace){yyerror("Misformed identifier"); YYABORT;}
81 | #define REQUIRE_WHITESPACE() require_whitespace = 1;
82 |
83 | char suppress_emptyline = 0;
84 | %}
85 | %option nounput
86 | %option noinput
87 | %option yylineno
88 |
89 | trit [0-2]
90 | tritail ({trit}){1,10}
91 | trinary 0t{tritail}
92 | digit [0-9]
93 | decimal 0*{digit}{1,5}
94 | chr [!-&(-\[\]-~ ]|\\'|\\n|\\r|\\t|\\\\
95 | character '({chr})'
96 | constant C0|C1|C20|C21|C2|EOF
97 | command MovD|Nop|Jmp|In|Out|Opr|Rot|Hlt
98 | identifier [A-Za-z_][0-9A-Za-z_]{0,99}
99 | label {identifier}:
100 | whitespace [ \t\r]
101 | comment ([;%#]|\/\/)[^\n]*|\/\*\/*(([^\*\/]\/*)*\*+)+\/
102 | part [\n]{whitespace}*[\n]
103 | section \.(CODE|DATA)
104 | offset \.OFFSET|@
105 | uprefixed U_{identifier}
106 | rprefixed R_{identifier}
107 | string \"(\\.|[^\"\\\r\n])*\"
108 |
109 | %%
110 | {comment}
111 |
112 | {string} { CHECK_WHITESPACE(); yylval.s_val = strdup(yytext); REQUIRE_WHITESPACE(); return STRING; }
113 | , {require_whitespace=0; return COMMA;}
114 |
115 | {character} {CHECK_WHITESPACE();
116 | if (yytext[1] == '\\')
117 | switch (yytext[2]) {
118 | case 'n':
119 | yylval.i_val = '\n'; break;
120 | case 'r':
121 | yylval.i_val = '\r'; break;
122 | case 't':
123 | yylval.i_val = '\t'; break;
124 | case '\\':
125 | yylval.i_val = '\\'; break;
126 | }
127 | else{
128 | yylval.i_val = yytext[1];
129 | }
130 | REQUIRE_WHITESPACE();
131 | return CONSTANT;}
132 |
133 | \{ { require_whitespace = 0;
134 | if (suppress_emptyline == 0) {
135 | suppress_emptyline = 1;
136 | COPY_CODE_POSITION(last_curly_bracket,yylloc,yylloc);
137 | return EMPTYLINE;
138 | } else {
139 | yyerror("Opening curly bracket found, but previous curly bracket has not been closed");
140 | fprintf(stderr,"Previous curly bracket was at line %d column %d.\n",
141 | last_curly_bracket.first_line, last_curly_bracket.first_column);
142 | YYABORT;
143 | } }
144 |
145 | \} { require_whitespace = 0;
146 | if (suppress_emptyline == 1) {
147 | suppress_emptyline = 0;
148 | COPY_CODE_POSITION(last_curly_bracket,yylloc,yylloc);
149 | return EMPTYLINE;
150 | } else {
151 | yyerror("Closing curly bracket without opening curly bracket found");
152 | YYABORT;
153 | } }
154 |
155 | {trinary} {unsigned short num=0; unsigned char position=2;
156 | CHECK_WHITESPACE();
157 | for(;yytext[position]!=0;position++) { num*=3; num+=yytext[position]-'0'; }
158 | yylval.i_val = num;
159 | REQUIRE_WHITESPACE();
160 | return CONSTANT;}
161 |
162 | {decimal} {unsigned int num=0; unsigned char position=0;
163 | CHECK_WHITESPACE();
164 | for(;yytext[position]!=0;position++) { num*=10; num+=yytext[position]-'0'; }
165 | if (num>59048) {yyerror("Integer too big"); YYABORT; }
166 | yylval.i_val = num;
167 | REQUIRE_WHITESPACE();
168 | return CONSTANT;}
169 |
170 | {constant} {CHECK_WHITESPACE();
171 | if (yytext[0] == 'E') { /* EOF (0t2222222222) */ yylval.i_val = 59048; }
172 | else if (yytext[1] == '0') { /* C0 (0t0000000000) */ yylval.i_val = 0; }
173 | else if (yytext[1] == '1') { /* C1 (0t1111111111) */ yylval.i_val = 29524; }
174 | else if (yytext[2] == 0) { /* C2 (0t2222222222) */ yylval.i_val = 59048; }
175 | else if (yytext[2] == '0') { /* C20 (0t2222222220) */ yylval.i_val = 59046; }
176 | else { /* C21 (0t2222222221) */ yylval.i_val = 59047; }
177 | REQUIRE_WHITESPACE();
178 | return CONSTANT;}
179 |
180 | RNop {CHECK_WHITESPACE();
181 | REQUIRE_WHITESPACE();
182 | return RNOP;}
183 |
184 | {command}: {yyerror("Label must not be a Malbolge command"); YYABORT;}
185 |
186 | {command} {CHECK_WHITESPACE();
187 | /* identify command */
188 | if (!strncmp(yytext,"Nop",4)) yylval.c_val = MALBOLGE_COMMAND_NOP;
189 | else if (!strncmp(yytext,"MovD",5)) yylval.c_val = MALBOLGE_COMMAND_MOVED;
190 | else if (!strncmp(yytext,"Jmp",4)) yylval.c_val = MALBOLGE_COMMAND_JMP;
191 | else if (!strncmp(yytext,"In",3)) yylval.c_val = MALBOLGE_COMMAND_IN;
192 | else if (!strncmp(yytext,"Out",4)) yylval.c_val = MALBOLGE_COMMAND_OUT;
193 | else if (!strncmp(yytext,"Hlt",4)) yylval.c_val = MALBOLGE_COMMAND_HALT;
194 | else if (!strncmp(yytext,"Opr",4)) yylval.c_val = MALBOLGE_COMMAND_OPR;
195 | else if (!strncmp(yytext,"Rot",4)) yylval.c_val = MALBOLGE_COMMAND_ROT;
196 | else {yyerror("Internal error"); YYABORT;}
197 | REQUIRE_WHITESPACE();
198 | return COMMAND;}
199 |
200 | {uprefixed}: {yyerror("Label must not start with U_"); YYABORT;}
201 | {rprefixed}: {yyerror("Label must not start with R_"); YYABORT;}
202 |
203 | {label} {char identifier[101]; int len = (int)strlen(yytext);
204 | CHECK_WHITESPACE();
205 | memcpy(identifier,yytext,len-1); identifier[len-1] = 0; /* remove colon */
206 | yylval.s_val = strdup(identifier);
207 | return LABEL;
208 | }
209 |
210 | {uprefixed} {CHECK_WHITESPACE();
211 | yylval.s_val = strdup(yytext+2); /* remove U_-prefix */
212 | REQUIRE_WHITESPACE();
213 | return U_PREFIXED_IDENTIFIER;}
214 |
215 | {rprefixed} {CHECK_WHITESPACE();
216 | yylval.s_val = strdup(yytext+2); /* remove R_-prefix */
217 | REQUIRE_WHITESPACE();
218 | return R_PREFIXED_IDENTIFIER;}
219 |
220 | {identifier} {CHECK_WHITESPACE();
221 | yylval.s_val = strdup(yytext);
222 | REQUIRE_WHITESPACE();
223 | return IDENTIFIER;}
224 |
225 | \/ {require_whitespace = 0;
226 | if (in_data_section == 1) {
227 | yylval.c_val = '/';
228 | return MULDIV;
229 | }
230 | return SLASH;}
231 |
232 | {section} {if (suppress_emptyline == 0) {
233 | CHECK_WHITESPACE();
234 | REQUIRE_WHITESPACE();
235 | if (yytext[1]=='C') { /* .CODE or .DATA */
236 | in_data_section = 0;
237 | return CSEC;
238 | } else {
239 | in_data_section = 1;
240 | return DSEC;
241 | }
242 | }else{
243 | if (yytext[1]=='C') { /* .CODE or .DATA */
244 | yyerror("Unexpected .CODE inside curly bracktes");
245 | YYABORT;
246 | }else{
247 | yyerror("Unexpected .DATA inside curly bracktes");
248 | YYABORT;
249 | }
250 | }}
251 |
252 | ({part})+ {require_whitespace = 0; if (suppress_emptyline == 0) { return EMPTYLINE; }}
253 |
254 | {offset} {if (yytext[0]!='@') {CHECK_WHITESPACE();
255 | REQUIRE_WHITESPACE();}else{require_whitespace = 0;}
256 | return OFFSET;}
257 |
258 | \?\-? {CHECK_WHITESPACE(); REQUIRE_WHITESPACE(); if (yytext[1] == '-' && !debug_mode) {return NOTUSED;} return DONTCARE;}
259 |
260 | \+|- { yylval.c_val = yytext[0];
261 | require_whitespace=0;
262 | return PLUSMINUS;}
263 |
264 | \* { yylval.c_val = '*';
265 | require_whitespace=0;
266 | return MULDIV;}
267 |
268 | >>|<< { yylval.c_val = yytext[0];
269 | require_whitespace=0;
270 | return SHIFT;}
271 |
272 | ! { require_whitespace=0;
273 | return CRAZY;}
274 |
275 | \( { return BRACKETLEFT; }
276 |
277 | \) { return BRACKETRIGHT; }
278 |
279 | {whitespace} require_whitespace = 0;
280 |
281 | \n require_whitespace = 0;
282 |
283 | <> if (suppress_emptyline == 1) {
284 | yyerror("Missing closing curly bracket at EOF");
285 | fprintf(stderr,"Opening curly bracket was at line %d column %d.\n",
286 | last_curly_bracket.first_line, last_curly_bracket.first_column);
287 | YYABORT;
288 | } else {yyterminate();}
289 |
290 | . yyerror("Unexpected character"); YYABORT;
291 |
292 |
293 |
--------------------------------------------------------------------------------
/example_hello_world.hell:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an
3 | * assembler for Malbolge.
4 | * Copyright (C) 2013-2017 Matthias Lutter
5 | *
6 | * LMAO is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * LMAO is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with this program. If not, see .
18 | *
19 | * E-Mail: matthias@lutter.cc
20 | *
21 | *
22 | * Example in HeLL: Printing out a string using loops.
23 | *
24 | * This HeLL program prints out a String by reading it character by character
25 | * from an array. This consumes much more memory of the virtual Malbolge
26 | * machine than other well known methods for printing out a string in Malbolge.
27 | * But it demonstrates how to use conditional branches and loops. Thus, this
28 | * program could be extended to something like the 99 bottles of beer program.
29 | *
30 | * The main techniques used by the code below came from reverse engineering
31 | * of the 99 bottles of beer program by Hisashi Iizawa et al.
32 | * http://www.99-bottles-of-beer.net/language-malbolge-995.html
33 | */
34 |
35 | .DATA
36 | // The string that will be printed out. Each character must be followed by
37 | // data_read, but the last character must be followed by last_data_read.
38 | // Notice that "abc",D is equvalent to 'a' D 'b' D 'c'
39 | STRING:
40 | "Hello, World!\n", data_read
41 | last_data_read
42 |
43 | .CODE
44 | FLAG1:
45 | Nop/MovD
46 | Jmp
47 |
48 | FLAG2:
49 | Nop/MovD
50 | Jmp
51 |
52 | FLAG3:
53 | Nop/MovD
54 | Jmp
55 |
56 | MOVED:
57 | MovD/Nop
58 | Jmp
59 |
60 | LOOP2:
61 | MovD/Nop
62 | Jmp
63 |
64 | LOOP2_2:
65 | Nop/MovD
66 | Jmp
67 |
68 | LOOP2_3:
69 | Nop/MovD
70 | Jmp
71 |
72 | LOOP5:
73 | Nop/Nop/Nop/Nop/MovD
74 | Jmp
75 |
76 | LOOP4:
77 | Nop/Nop/Nop/MovD
78 | Jmp
79 |
80 |
81 | NO_MORE_CARRY_FLAG:
82 | Nop/MovD
83 | Jmp
84 |
85 | NO_MORE_DATA_FLAG:
86 | Nop/MovD
87 | Jmp
88 |
89 |
90 | NOP:
91 | Jmp
92 |
93 | CRAZY:
94 | Opr/Nop
95 | Jmp
96 |
97 | ROT:
98 | Rot/Nop
99 | Jmp
100 |
101 | HALT:
102 | Hlt
103 |
104 | OUT:
105 | Out/Nop
106 | Jmp
107 |
108 | LOAD_CHARACTER:
109 | ; Move D register to the address the string pointer points to.
110 | MovD/Nop/Nop/Nop/Nop/Nop/Nop/Nop/Nop
111 | RNop
112 | RNop
113 | ; Load the current character of the string with the Opr instruction.
114 | Opr/Nop/Nop/Nop/Nop/Rot/Nop/Nop/Nop
115 | ; label for xlat2 restoring the above Opr-command.
116 | PARTIAL_R_LOAD_CHARACTER:
117 | ; Move D register to data_read or last_data_read (see STRING)
118 | MovD/Nop/Nop/Nop/Nop/Nop/Nop/Nop/Nop
119 | Jmp
120 |
121 | @C21 CARRY:
122 | ; The carry variable may be C20 or C21. By executing the code at the
123 | ; carry address the increment of the D register until reaching the
124 | ; Jmp instruction differs. This is used for conditional branching
125 | ; dependent on the value of the carry.
126 | ; RNop at C21
127 | RNop
128 | ; RNop at C2
129 | RNop
130 | ; Jmp at C0
131 | Jmp
132 |
133 |
134 |
135 |
136 | .DATA
137 |
138 | // ******************** \\
139 | // Variable definitions \\
140 | // ******************** \\
141 |
142 |
143 | // var string_ptr
144 | // string ptr: points to the character of the string that should be printed out
145 | // the next time
146 | crazy_string_ptr:
147 | U_CRAZY string_ptr
148 | load_character_string_ptr:
149 | U_LOAD_CHARACTER string_ptr
150 | rot_string_ptr:
151 | U_ROT string_ptr
152 | string_ptr:
153 | STRING-2
154 | FLAG1 return_from_string_ptr_1 R_FLAG1
155 | FLAG2 return_from_string_ptr_2 R_FLAG2
156 |
157 | // var carry
158 | // carry: stores while incrementing the string_ptr whether the next trit has
159 | // to be incremented also or not.
160 | crazy_carry:
161 | U_CRAZY carry
162 | carry:
163 | CARRY
164 | U_NOP execution_not_jumped_to_carry
165 | U_NOP carry_was_not_set
166 | U_NOP carry_was_set
167 | carry_was_set:
168 | R_MOVED
169 | MOVED return_carry_was_set
170 | carry_was_not_set:
171 | R_MOVED
172 | MOVED return_carry_was_not_set
173 | execution_not_jumped_to_carry:
174 | FLAG1 return_from_carry_1 R_FLAG1
175 |
176 |
177 | // var tmp
178 | // tmp: used during increment
179 | crazy_tmp:
180 | U_CRAZY tmp
181 | tmp:
182 | C0 // must not contain any trit that is 2.
183 | FLAG1 return_from_tmp_1 R_FLAG1
184 | FLAG2 return_from_tmp_2 R_FLAG2
185 |
186 |
187 |
188 | // ******************** \\
189 | // Program code \\
190 | // ******************** \\
191 |
192 |
193 | // Increment string_ptr twice to point to the next character
194 | // Code sequence to increment the last trit of the memory cell "value":
195 | //
196 | // SET tmp = C0
197 | // SET carry = C20 or C21
198 | // ROT C2
199 | // OPR value
200 | // OPR tmp
201 | // OPR carry
202 | // OPR value
203 | // OPR tmp
204 | // OPR carry
205 | // if (carry == C20){
206 | // // carry is set
207 | // }else if (carry == C21){
208 | // // no carry
209 | // }
210 | //
211 | // After the lowest trit has been incremented, the variable has to be rotated.
212 | // Depending on the carry, the increment has to be done again or not.
213 | // After 10 rotates the variable is incremented.
214 | ;
215 | // To increment string_ptr by 2, these steps will be executed twice.
216 | increment_string_ptr:
217 | R_MOVED
218 | // We have to set tmp to C0. It won't contain any trit that is 2,
219 | // so we can just crazy C1 into tmp.
220 | set_tmp_to_C0:
221 | ROT C1 R_ROT
222 | R_FLAG1 // set return flag
223 | MOVED crazy_tmp // crazy C1 into tmp variable to set it to C0
224 |
225 | return_from_tmp_1:
226 | R_CRAZY R_MOVED
227 | ROT C2 R_ROT // load C2 to A register
228 | // now we have to execute the command sequence
229 | // OPR value
230 | // OPR tmp
231 | // OPR carry
232 | // twice.
233 | crazy_loop_increment_string_ptr:
234 | //the following sequence must be executed twice;
235 | //label for loop
236 | R_FLAG1 // set return flag
237 | MOVED crazy_string_ptr // crazy A register into tmp variable
238 |
239 | return_from_string_ptr_1:
240 | R_MOVED R_CRAZY
241 | R_FLAG2 // set return flag
242 | MOVED crazy_tmp // crazy into tmp variable
243 |
244 | return_from_tmp_2:
245 | R_CRAZY R_MOVED
246 | R_FLAG1 // set return flag
247 | MOVED crazy_carry // crazy into carry variable
248 |
249 | {
250 | return_from_carry_1:
251 | R_CRAZY R_MOVED
252 | // check loop condition
253 | LOOP2 crazy_loop_increment_string_ptr
254 |
255 | MOVED carry
256 | return_carry_was_not_set:
257 | R_NO_MORE_CARRY_FLAG
258 | return_carry_was_set:
259 | R_MOVED
260 | // rotate string_ptr until it has been rotated 10 times.
261 | // after rotating go to increment or rotate again corresponding to
262 | // the NO_MORE_CARRY_FLAG
263 | R_FLAG2 // set return flag
264 | MOVED rot_string_ptr // rot string_ptr
265 | }
266 |
267 | return_from_string_ptr_2:
268 | R_MOVED R_ROT
269 | // exit loop after 5 times
270 | LOOP5 exit_inner_increment_loop
271 | NO_MORE_CARRY_FLAG restore_no_more_carry_flag_and_rotate_string_ptr
272 | R_NO_MORE_CARRY_FLAG
273 | MOVED increment_string_ptr
274 |
275 | exit_inner_increment_loop:
276 | // exit loop after 2 times
277 | LOOP2_2 exit_increment_loop
278 | NO_MORE_CARRY_FLAG restore_no_more_carry_flag_and_rotate_string_ptr
279 | R_NO_MORE_CARRY_FLAG
280 | MOVED increment_string_ptr
281 |
282 | restore_no_more_carry_flag_and_rotate_string_ptr:
283 | R_NO_MORE_CARRY_FLAG
284 | MOVED carry_was_set
285 |
286 |
287 | exit_increment_loop:
288 | NO_MORE_CARRY_FLAG nomorecarrykilled
289 | R_NO_MORE_CARRY_FLAG
290 | nomorecarrykilled:
291 | LOOP2_3 ENTRY
292 | MOVED increment_string_ptr
293 |
294 |
295 |
296 |
297 | // read character at string_ptr and print it out
298 | ENTRY:
299 | // load character at string_ptr
300 | ROT C2 R_ROT
301 | MOVED load_character_string_ptr
302 |
303 | {
304 | // return labels for load_character_string_ptr-function
305 | // (see definition of STRING)
306 | last_data_read:
307 | // force NO_MORE_DATA_FLAG to be set
308 | NO_MORE_DATA_FLAG last_data_read
309 | // after the flag is set, the last character is read in as any other
310 | // character
311 |
312 | data_read: restore_load_character:
313 | R_MOVED
314 | restore_load_character_no_r_moved:
315 | // restore LOAD_CHARACTER code. This is a bit tricky and requires
316 | // 2 loops.
317 | // 9-cycles in LOAD_CHARACTER;
318 | // containing evil cycle Opr/Nop/Nop/Nop/Nop/Rot/Nop/Nop/Nop
319 | // the Rot operation MUST NOT BE executed during restoring,
320 | // because this would override the character we read into the
321 | // A register
322 | // the PARTIAL_R_LOAD_CHARACTER-restore-opration prevents executing
323 | // the Rot command, but the full command has to be restored 9 times,
324 | // too.
325 | PARTIAL_R_LOAD_CHARACTER ?-
326 | R_LOAD_CHARACTER ?- ?- ?- ?-
327 | LOOP4 half_of_restore_load_character_done
328 | // continue restoring
329 | MOVED restore_load_character
330 | }
331 |
332 | half_of_restore_load_character_done:
333 | // quit at second call
334 | LOOP2_2 restore_load_character_done
335 | // destroy PARTIAL restored LOAD_CHARACTER, so the LOOP4-loop
336 | // can restore the whole command sequence
337 | PARTIAL_R_LOAD_CHARACTER
338 | // render meanwhile restored MovD command harmless
339 | // continue restoring
340 | restore_load_character_no_r_moved
341 |
342 | {
343 | restore_load_character_done:
344 | // we crazied C2 into a character at position (string_ptr+2).
345 | // we must do this twice to read the original character.
346 | LOOP2 ENTRY
347 |
348 | // we copied the character at position (string_ptr+2) into the
349 | // A register. Now we will print it out.
350 | OUT ?- R_OUT
351 | // if it was the last character of the string, we will go to the
352 | // label whole_string_printed and terminate the program
353 | NO_MORE_DATA_FLAG whole_string_printed R_NO_MORE_DATA_FLAG
354 | // if it was not the last character, we have to increment
355 | // string_ptr (twice) to point to the next character
356 | MOVED increment_string_ptr
357 | }
358 |
359 | whole_string_printed:
360 | HALT
361 |
362 |
--------------------------------------------------------------------------------
/src/typedefs.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | #ifndef TYPEDEFSINCLUDED
24 | #define TYPEDEFSINCLUDED
25 |
26 | typedef struct HeLLCodePosition {
27 | int first_line;
28 | int first_column;
29 | int last_line;
30 | int last_column;
31 | } HeLLCodePosition;
32 |
33 | /**
34 | * Represents a Malbolge code word as its complete xlat2-cycle.
35 | * The cycle is stored as a linked list.
36 | */
37 | typedef struct XlatCycle {
38 | /**
39 | * Should be the numeric representation of one of these Malbolge commands: NOP, MOVD, OPR, JMP, ROT, OUT, IN or HLT.
40 | * \sa{malbolge.h}
41 | */
42 | unsigned char cmd;
43 | /**
44 | * Position in .hell file. Used for generating debug informations and error messages.
45 | */
46 | HeLLCodePosition code_position;
47 | /**
48 | * Pointer to the next element in this xlat2 cycle.
49 | */
50 | struct XlatCycle* next;
51 | } XlatCycle;
52 |
53 | /**
54 | * Stores the value of a memory cell in data segment.
55 | *
56 | * The value can be given directly or by the name of a label the data cell value points to. It is also possible to mark the DataCell value as don't-care or unused.
57 | */
58 | typedef struct DataAtom {
59 | /**
60 | * Contains the name of the label where the DataCell is pointing to.
61 | * If it is a NULL pointer the value of the DataCell is a constant stored in the number field.
62 | */
63 | const char* destination_label;
64 | /**
65 | * If destination_label is not NULL this field contains a number that is added to the location referenced by destination_label.
66 | * If destination_label is NULL and this value is not negative it describes the absolute value of the memory cell.
67 | * If destination_label is NULL and this value is -2 it represents a don't-care memory cell.
68 | * If destination_label is NULL and this value is -1 it represents an unused memory cell.
69 | */
70 | int number;
71 | /**
72 | * If this is not a NULL pointer then the relative offset of the memory cell referenced by this label has to be subtracted from this cell.
73 | * This field can be used to store information about an (unresolved) U_ prefix.
74 | */
75 | const char* operand_label;
76 | /**
77 | * Position in .hell file. Used for generating debug informations and error messages.
78 | */
79 | HeLLCodePosition code_position;
80 | } DataAtom;
81 |
82 | #define DATACELL_OPERATOR_LEAF_ELEMENT 0
83 | #define DATACELL_OPERATOR_PLUS 1
84 | #define DATACELL_OPERATOR_MINUS 2
85 | #define DATACELL_OPERATOR_TIMES 3
86 | #define DATACELL_OPERATOR_DIVIDE 4
87 | #define DATACELL_OPERATOR_ROTATE_R 5
88 | #define DATACELL_OPERATOR_ROTATE_L 6
89 | #define DATACELL_OPERATOR_CRAZY 7
90 | #define DATACELL_OPERATOR_DONTCARE 8
91 | #define DATACELL_OPERATOR_NOT_USED 9
92 |
93 | /**
94 | * The value of a Malbolge memory cell that contains an element from the data section can be described by a formula.
95 | * Such a formula is represented by this struct (tree), so the value of the memory cell can be calculated during opcode generation.
96 | */
97 | typedef struct DataCell {
98 | DataAtom* leaf_element;
99 | int _operator;
100 | struct DataCell* left_element;
101 | struct DataCell* right_element;
102 | } DataCell;
103 |
104 |
105 | /**
106 | * Double linked list of contiguous DataCells.
107 | */
108 | typedef struct DataBlock {
109 | /**
110 | * Content of current Malbolge memory cell / DataAtom.
111 | */
112 | DataCell* data;
113 | /**
114 | * \brief Offset of memory cell in final Malbolge program.
115 | *
116 | * Set to -1 for don't care or unknown
117 | */
118 | int offset;
119 | /**
120 | * Position in .hell file. Used for generating debug informations and error messages.
121 | */
122 | HeLLCodePosition code_position;
123 | /**
124 | * Succeeding element in this linked list.
125 | * NULL if the current element is the last element.
126 | */
127 | struct DataBlock* next;
128 | /**
129 | * Previous element in this linked list.
130 | * NULL if the current element is the first element.
131 | */
132 | struct DataBlock* prev;
133 | /**
134 | * Number of memory cells until the end of this list, including the current DataCell.
135 | *
136 | * Set to: 1 for the last cell, 2 for the second last, ..., n for the first.
137 | */
138 | int num_of_blocks;
139 | } DataBlock;
140 |
141 | /**
142 | * Double linked list of contiguous XlatCycles.
143 | */
144 | typedef struct CodeBlock {
145 | /**
146 | * Current Malbolge command / XlatCycle.
147 | */
148 | XlatCycle* command;
149 | /**
150 | * Succeeding element in this linked list.
151 | * NULL if the current element is the last element.
152 | */
153 | int offset;
154 | /**
155 | * Position in .hell file. Used for generating debug informations and error messages.
156 | */
157 | HeLLCodePosition code_position;
158 | /**
159 | * Succeeding element in this linked list.
160 | * NULL if the current element is the last element.
161 | */
162 | struct CodeBlock* next;
163 | /**
164 | * Previous element in this linked list.
165 | * NULL if the current element is the first element.
166 | */
167 | struct CodeBlock* prev;
168 | /**
169 | * Number of Malbolge commands until the end of this list, including the current XlatCycle.
170 | *
171 | * Set to: 1 for the last cell, 2 for the second last, ..., n for the first.
172 | */
173 | int num_of_blocks;
174 | /**
175 | * Indicates that the block is not specified in HeLL source directly,
176 | * but generated as a preceding block for U_ prefix reference.
177 | */
178 | int virtual_block;
179 | } CodeBlock;
180 |
181 | /**
182 | * TODO
183 | *
184 | * Binary search tree containing all label strings defined in the HeLL program as nodes.
185 | * Each node is associated to a DataBlock or a CodeBlock corresponding to its label string.
186 | */
187 | typedef struct LabelTree {
188 | /**
189 | * TODO
190 | *
191 | * pointer to datablock or NULL, if label is defined in code section.
192 | */
193 | DataBlock* destination_data;
194 | /**
195 | * TODO
196 | *
197 | * pointer to codeblock or NULL, if label is defined in data section.
198 | */
199 | CodeBlock* destination_code;
200 | /**
201 | * TODO
202 | *
203 | * name
204 | */
205 | const char* label;
206 | /**
207 | * Position in .hell file. Used for generating debug informations and error messages.
208 | */
209 | HeLLCodePosition code_position;
210 | /**
211 | * TODO
212 | *
213 | * left sub tree
214 | */
215 | struct LabelTree* left;
216 | /**
217 | * TODO
218 | *
219 | * right sub tree
220 | */
221 | struct LabelTree* right;
222 | } LabelTree;
223 |
224 | /**
225 | * TODO
226 | *
227 | * Array containing each DataBlock defined in the HeLL program.
228 | */
229 | typedef struct DataBlocks {
230 | /**
231 | * TODO
232 | */
233 | DataBlock** datafield;
234 | /**
235 | * TODO
236 | */
237 | unsigned int size;
238 | } DataBlocks;
239 |
240 | /**
241 | * TODO
242 | *
243 | * Array containing each CodeBlock defined in the HeLL program.
244 | */
245 | typedef struct CodeBlocks {
246 | /**
247 | * TODO
248 | */
249 | CodeBlock** codefield;
250 | /**
251 | * TODO
252 | */
253 | unsigned int size;
254 | } CodeBlocks;
255 |
256 | /**
257 | * Value for the usage field of MemoryCell.
258 | *
259 | * This value indicates that the memory cell is not used in the initialized Malbolge program.
260 | */
261 | #define UNUSED 0
262 | /**
263 | * Value for the usage field of MemoryCell.
264 | *
265 | * This value indicates that the memory is used for the code section. It should be initialized directly in the generated Malbolge code whithout the need of Malbolge runtime initialization.
266 | */
267 | #define PREINITIALIZED_CODE 1
268 | /**
269 | * Value for the usage field of MemoryCell.
270 | *
271 | * This value indicates that the memory is used for the code section. It should be initialized during execution of the Malbolge program by using the constant generation data module.
272 | */
273 | #define CODE 2
274 | /**
275 | * Value for the usage field of MemoryCell.
276 | *
277 | * This value indicates that the memory is used for the data section. It should be initialized during execution of the Malbolge program by using the constant generation data module.
278 | */
279 | #define DATA 3
280 | /**
281 | * Value for the usage field of MemoryCell.
282 | *
283 | * This value indicates that the memory is used for the code section. Its initial value does not matter, but it must be in the range from 33-126 to prevent crashes of the Malbolge interpreter.
284 | */
285 | #define RESERVED_CODE 4
286 | /**
287 | * Value for the usage field of MemoryCell.
288 | *
289 | * This value indicates that the memory is used for the data section. Its initial value does not matter.
290 | */
291 | #define RESERVED_DATA 5
292 |
293 | /**
294 | * \brief Links to an element in the double linked lists of XlatCycles or DataCells and stores the initialization type of the memory cell.
295 | *
296 | * Create an array of these elements to calculate and store a possible memory layout of the final initialized Malbolge program.
297 | */
298 | typedef struct MemoryCell {
299 | /**
300 | * Pointer to element of the double linked list of XlatCycles that is located at this memory cell.
301 | */
302 | CodeBlock* code;
303 | /**
304 | * Pointer to element of the double linked list of DataCells that is located at this memory cell.
305 | */
306 | DataBlock* data;
307 | /**
308 | * Way of initialization and usage of this memory cell.
309 | * 0: unused
310 | * 1: preinitialized code
311 | * 2: code
312 | * 3: data
313 | * 4: reserved code (ASCII: 33-126)
314 | * 5: reserved data
315 | */
316 | unsigned char usage;
317 | /**
318 | * Position in .hell file. Used for generating debug informations and error messages.
319 | */
320 | HeLLCodePosition code_position;
321 | } MemoryCell;
322 |
323 | /**
324 | * Root element of binary search tree containing all label strings defined in the HeLL program as nodes.
325 | * Each node is associated to a DataBlock or a CodeBlock corresponding to its label string.
326 | */
327 | extern LabelTree* labeltree;
328 |
329 | /**
330 | * Array containing each DataBlock defined in the HeLL program.
331 | */
332 | extern DataBlocks datablocks;
333 |
334 | /**
335 | * Array containing each CodeBlock defined in the HeLL program.
336 | */
337 | extern CodeBlocks codeblocks;
338 |
339 | #endif
340 |
341 |
--------------------------------------------------------------------------------
/example_cat_halt_on_eof.hell:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
3 | * Copyright (C) 2013-2017 Matthias Lutter
4 | *
5 | * LMAO is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * LMAO is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | *
18 | * E-Mail: matthias@lutter.cc
19 | *
20 | *
21 | * Example in HeLL: This is a cat program in HeLL that halts on EOF.
22 | */
23 |
24 | .CODE
25 | // +-----------------------------------+ \\
26 | // | LIST OF MALBOLGE COMMANDS WE NEED | \\
27 | // +-----------------------------------+ \\
28 |
29 | // *** normal commands *** \\
30 | MOVED:
31 | MovD/Nop
32 | Jmp
33 |
34 | ROT:
35 | Rot/Nop
36 | Jmp
37 |
38 | IN:
39 | In/Nop
40 | Jmp
41 |
42 | OUT:
43 | Out/Nop
44 | Jmp
45 |
46 | CRAZY:
47 | Opr/Nop
48 | Jmp
49 |
50 | HALT:
51 | Hlt // need not be loop resistant.
52 |
53 | NOP:
54 | Jmp // will immideately start execution of the command specified by the next .DATA memory cell
55 |
56 |
57 | // *** flags *** \\
58 |
59 | // use FLAGs to save our position when calling MOVED to operate on a variable (tmp1, tmp2, tmp3 or tmp4).
60 | FLAG1:
61 | Nop/MovD
62 | Jmp
63 |
64 | FLAG2:
65 | Nop/MovD
66 | Jmp
67 |
68 | FLAG3:
69 | Nop/MovD
70 | Jmp
71 |
72 | FLAG4:
73 | Nop/MovD
74 | Jmp
75 |
76 | FLAG5:
77 | Nop/MovD
78 | Jmp
79 |
80 | FLAG6:
81 | Nop/MovD
82 | Jmp
83 |
84 | FLAG7:
85 | Nop/MovD
86 | Jmp
87 |
88 | FLAG8:
89 | Nop/MovD
90 | Jmp
91 |
92 | FLAG9:
93 | Nop/MovD
94 | Jmp
95 |
96 | FLAG10:
97 | Nop/MovD
98 | Jmp
99 |
100 | FLAG11:
101 | Nop/MovD
102 | Jmp
103 |
104 |
105 | // *** loop counters *** \\
106 |
107 | // to build loops which are executed twice: use xlat2 at this label
108 | COUNTER2_1:
109 | MovD/Nop
110 | Jmp
111 |
112 | COUNTER2_2:
113 | Nop/MovD
114 | Jmp
115 |
116 | COUNTER5_1:
117 | Nop/Nop/Nop/Nop/MovD
118 | Jmp
119 |
120 |
121 | // tmp4 will be brought to C20 if it was not C2 (EOF), otherwise it will be C21.
122 | // to detect the value of tmp4, we will jmp (i) to its value.
123 | // the handling is following here: depending on the result the d register will move 1 or 2 steps until execution will be continued by Jmp.
124 | .OFFSET C21
125 | LABEL:
126 | RNop
127 | RNop
128 | Jmp
129 |
130 |
131 | .DATA
132 | // +-----------------------------------------+ \\
133 | // | DECLARATION/INITIALIZATION OF VARIABLES | \\
134 | // +-----------------------------------------+ \\
135 | //
136 | // variables (tmp1, tmp2, tmp3 and tmp4), their functions to modify them,
137 | // and checking FLAGn for return selection.
138 | tmp1_crazy:
139 | U_CRAZY tmp1
140 | tmp1:
141 | ?
142 | FLAG1 return_from_tmp1_1 R_FLAG1
143 | FLAG2 return_from_tmp1_2 R_FLAG2
144 |
145 | tmp2_rot:
146 | U_ROT tmp2
147 | tmp2_crazy:
148 | U_CRAZY tmp2
149 | tmp2:
150 | ?
151 | FLAG1 return_from_tmp2_1 R_FLAG1
152 | FLAG2 return_from_tmp2_2 R_FLAG2
153 | FLAG3 return_from_tmp2_3 R_FLAG3
154 | FLAG4 return_from_tmp2_4 R_FLAG4
155 | FLAG5 return_from_tmp2_5 R_FLAG5
156 |
157 | tmp3_crazy:
158 | U_CRAZY tmp3
159 | tmp3:
160 | ?
161 | FLAG1 return_from_tmp3_1 R_FLAG1
162 | FLAG2 return_from_tmp3_2 R_FLAG2
163 |
164 | tmp4_crazy:
165 | U_CRAZY tmp4
166 | tmp4:
167 | ?
168 | U_NOP skip_nop_detection
169 | U_NOP tmp4_was_C21
170 | U_NOP tmp4_was_C20
171 | tmp4_was_C21:
172 | HALT
173 | tmp4_was_C20:
174 | R_MOVED
175 | MOVED NO_EOF_READ
176 | skip_nop_detection:
177 | FLAG1 return_from_tmp4_1 R_FLAG1
178 | FLAG2 return_from_tmp4_2 R_FLAG2
179 | FLAG3 return_from_tmp4_3 R_FLAG3
180 | FLAG4 return_from_tmp4_4 R_FLAG4
181 | FLAG5 return_from_tmp4_5 R_FLAG5
182 | FLAG6 return_from_tmp4_6 R_FLAG6
183 | FLAG7 return_from_tmp4_7 R_FLAG7
184 | FLAG8 return_from_tmp4_8 R_FLAG8
185 | FLAG9 return_from_tmp4_9 R_FLAG9
186 | FLAG10 return_from_tmp4_10 R_FLAG10
187 |
188 | {
189 | next_char:
190 | R_MOVED // restore MovD command
191 |
192 |
193 | // +---------------------------------------------+ \\
194 | // | ENTRY POINT: Program execution starts here! | \\
195 | // +---------------------------------------------+ \\
196 | ENTRY:
197 | // bring tmp1 to C1:
198 | // load C1
199 | ROT C1 R_ROT
200 | do_crzy_tmp1:
201 | // set return position: FLAG1
202 | R_FLAG1
203 | // crazy tmp1
204 | MOVED tmp1_crazy
205 | }{
206 | return_from_tmp1_1:
207 | // crazy tmp1 has been executed
208 | // restore xlat2 cycles
209 | R_CRAZY R_MOVED
210 | // we have to crazy tmp1 twice to bring it to C1.
211 | // test if we did it twice; if not: MovD back
212 | COUNTER2_1 do_crzy_tmp1
213 |
214 | // now we have to bring tmp2 to C1:
215 | // the A register contains C1, because we just brought tmp1 to C1, so we dont have to load C1 again.
216 | // set return position: FLAG1
217 | do_crzy_tmp2:
218 | R_FLAG1
219 | // crazy tmp2
220 | MOVED tmp2_crazy
221 | }{
222 | return_from_tmp2_1:
223 | // crazy tmp2 has been executed
224 | // restore xlat2 cycles
225 | R_CRAZY R_MOVED
226 | // we have to crazy tmp2 twice to bring it to C1.
227 | // test if we did it twice; if not: MovD back
228 | COUNTER2_1 do_crzy_tmp2
229 |
230 | // same for tmp3 and tmp4:
231 | do_crzy_tmp3:
232 | // set return position: FLAG1
233 | R_FLAG1
234 | // crazy tmp3
235 | MOVED tmp3_crazy
236 | }{
237 | return_from_tmp3_1:
238 | // crazy tmp3 has been executed
239 | // restore xlat2 cycles
240 | R_CRAZY R_MOVED
241 | // we have to crazy tmp3 twice to bring it to C1.
242 | // test if we did it twice; if not: MovD back
243 | COUNTER2_1 do_crzy_tmp3
244 |
245 | do_crzy_tmp4:
246 | // set return position: FLAG1
247 | R_FLAG1
248 | // crazy tmp4
249 | MOVED tmp4_crazy
250 | }{
251 | return_from_tmp4_1:
252 | // crazy tmp4 has been executed
253 | // restore xlat2 cycles
254 | R_CRAZY R_MOVED
255 | // we have to crazy tmp4 twice to bring it to C1.
256 | // test if we did it twice; if not: MovD back
257 | COUNTER2_1 do_crzy_tmp4
258 |
259 | // now we prepared tmp1, tmp2, tmp3 and tmp4 to store a value from our A register.
260 | // so we can read in a character now.
261 | IN ?- R_IN
262 | // now we have to store the character we read by calling crazy for tmp1, tmp2, tmp3 and tmp4.
263 | // after that we will have stored the character at tmp2 and tmp4. (tmp1 and tmp3 will contain a modified version we dont want and we won't use)
264 | // crazy into tmp1: set return FLAG
265 | R_FLAG2
266 | // crazy tmp1
267 | MOVED tmp1_crazy
268 | }{
269 | return_from_tmp1_2:
270 | // crazy tmp1 has been executed
271 | // restore xlat2 cycles
272 | R_CRAZY R_MOVED
273 |
274 | // crazy into tmp2: set return FLAG
275 | R_FLAG2
276 | // crazy tmp2
277 | MOVED tmp2_crazy
278 | }{
279 | return_from_tmp2_2:
280 | // crazy tmp2 has been executed
281 | // restore xlat2 cycles
282 | R_CRAZY R_MOVED
283 |
284 | // crazy into tmp3: set return FLAG
285 | R_FLAG2
286 | // crazy tmp3
287 | MOVED tmp3_crazy
288 | }{
289 | return_from_tmp3_2:
290 | // crazy tmp3 has been executed
291 | // restore xlat2 cycles
292 | R_CRAZY R_MOVED
293 |
294 | // crazy into tmp4: set return FLAG
295 | R_FLAG2
296 | // crazy tmp4
297 | MOVED tmp4_crazy
298 | }{
299 | return_from_tmp4_2:
300 | // crazy tmp4 has been executed
301 | // restore xlat2 cycles
302 | R_CRAZY R_MOVED
303 |
304 |
305 | // now we have to check if we read C2 (EOF).
306 | // op(C1, op(X, op(C2, op(C1, Y)))) is a tritwise test if both trits of X and Y are 2. Then it return 2, otherwise 0.
307 | // if we rotate tmp2 and check it in each step tritwise against tmp4 with the test below,
308 | // this will result in C2 if tmp2 and tmp4 has been C2 and in C0 otherwise.
309 |
310 | R_MOVED // destory MOVED, because it will be restored in the next step that is reached via MOVED and requires restoring it.
311 | check_for_C2: // label; restore MOVED
312 | R_MOVED
313 | // load C1
314 | ROT C1 R_ROT
315 |
316 | // crazy into tmp4
317 | R_FLAG3
318 | // crazy tmp4
319 | MOVED tmp4_crazy
320 | }{
321 | return_from_tmp4_3:
322 | // crazy tmp4 has been executed
323 | // restore xlat2 cycles
324 | R_CRAZY R_MOVED
325 |
326 | // load C2
327 | ROT C2 R_ROT
328 |
329 | // crazy into tmp4
330 | R_FLAG4
331 | // crazy tmp4
332 | MOVED tmp4_crazy
333 | }{
334 | return_from_tmp4_4:
335 | // crazy tmp4 has been executed
336 | // restore xlat2 cycles
337 | R_CRAZY R_MOVED
338 |
339 | // rotate and load tmp2
340 | R_FLAG3
341 | // rotate tmp2
342 | MOVED tmp2_rot
343 | }{
344 | return_from_tmp2_3:
345 | // rot tmp4 has been executed
346 | // restore xlat2 cycles
347 | R_ROT R_MOVED
348 |
349 | // crazy into tmp4
350 | R_FLAG5
351 | // crazy tmp4
352 | MOVED tmp4_crazy
353 | }{
354 | return_from_tmp4_5:
355 | // crazy tmp4 has been executed
356 | // restore xlat2 cycles
357 | R_CRAZY R_MOVED
358 |
359 | // load C1
360 | ROT C1 R_ROT
361 |
362 | // crazy into tmp4
363 | R_FLAG6
364 | // crazy tmp4
365 | MOVED tmp4_crazy
366 | }{
367 | return_from_tmp4_6:
368 | // crazy tmp4 has been executed
369 | // restore xlat2 cycles
370 | R_CRAZY R_MOVED
371 |
372 | // We have to rotate tmp2 and check tritwise for a 2 ten times (number of trits in tmp2/tmp4).
373 | COUNTER5_1 rotated_5_or_10_times
374 | MOVED check_for_C2
375 | }{
376 | rotated_5_or_10_times:
377 | COUNTER2_2 rotated_10_times
378 | MOVED check_for_C2
379 | }{
380 | rotated_10_times:
381 | //
382 | // now tmp4 is C2 or C0.
383 | // crazy C21 into tmp4 => 1111111112 or C0
384 |
385 | // load C21
386 | ROT C0 R_ROT
387 | CRAZY C21 R_CRAZY
388 |
389 | // crazy into tmp4
390 | R_FLAG7
391 | // crazy tmp4
392 | MOVED tmp4_crazy
393 | }{
394 | return_from_tmp4_7:
395 | // crazy tmp4 has been executed
396 | // restore xlat2 cycles
397 | R_CRAZY R_MOVED
398 |
399 | // crazy C0 into tmp4 => 1111111112 or C1
400 |
401 | // load C0
402 | ROT C0 R_ROT
403 |
404 | // crazy into tmp4
405 | R_FLAG8
406 | // crazy tmp4
407 | MOVED tmp4_crazy
408 | }{
409 | return_from_tmp4_8:
410 | // crazy tmp4 has been executed
411 | // restore xlat2 cycles
412 | R_CRAZY R_MOVED
413 |
414 | // -> crazy C21 into tmp4 => C2 or C20
415 |
416 | // load C21
417 | ROT C0 R_ROT
418 | CRAZY C21 R_CRAZY
419 |
420 | // crazy into tmp4
421 | R_FLAG9
422 | // crazy tmp4
423 | MOVED tmp4_crazy
424 | }{
425 | return_from_tmp4_9:
426 | // crazy tmp4 has been executed
427 | // restore xlat2 cycles
428 | R_CRAZY R_MOVED
429 |
430 | // -> crazy 0t0000000002 into it => C21 oder C20.
431 |
432 | // load 2
433 | ROT C1 R_ROT
434 | CRAZY 2 R_CRAZY
435 |
436 | // crazy into tmp4
437 | R_FLAG10
438 | // crazy tmp4
439 | MOVED tmp4_crazy
440 | }{
441 | return_from_tmp4_10:
442 | // crazy tmp4 has been executed
443 | // restore xlat2 cycles
444 | R_CRAZY R_MOVED
445 |
446 | // now we move the c register to the address that tmp4 points to. we can count the number of NOPs that will be executed before reachung Jmp at offset 0 by looking for the number of steps the data pointer has moved.
447 |
448 | MOVED tmp4
449 | }{
450 | NO_EOF_READ:
451 | // thats it: no EOF has been read, so we will print out the character we read.
452 | R_MOVED
453 | // read tmp2 to print it out: crazy it with C2 2 times.
454 |
455 | ROT C2 R_ROT
456 | // crazy into tmp2: set return FLAG
457 | R_FLAG4
458 | // crazy tmp2
459 | MOVED tmp2_crazy
460 | }{
461 | return_from_tmp2_4:
462 | // crazy tmp2 has been executed
463 | // restore xlat2 cycles
464 | R_CRAZY R_MOVED
465 |
466 | ROT C2 R_ROT
467 | // crazy into tmp2: set return FLAG
468 | R_FLAG5
469 | // crazy tmp2
470 | MOVED tmp2_crazy
471 | }{
472 | return_from_tmp2_5:
473 | // crazy tmp2 has been executed
474 | // restore xlat2 cycles
475 | R_CRAZY R_MOVED
476 |
477 | OUT ?- R_OUT
478 | // read next character: restore MOVED at the destination we move to (because the call here will detroy it), then run into ENTRY.
479 | MOVED next_char
480 | }
481 |
482 |
--------------------------------------------------------------------------------
/src/initialize.c:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | #include "malbolge.h"
24 | #include "main.h"
25 | #include "gen_init.h"
26 | #include "initialize.h"
27 | #include
28 | #include
29 | #include
30 |
31 | void update_offsets(MemoryCell memory[C2+1]){
32 | int i;
33 | /* save all offsets. */
34 | for (i=0;ioffset = i;
37 | if (memory[i].usage == DATA || memory[i].usage == RESERVED_DATA)
38 | memory[i].data->offset = i;
39 | }
40 | }
41 |
42 | int parse_dataatom(DataAtom* data, LabelTree* labeltree, int no_error_printing){
43 | int value = -1;
44 | if (data==0)
45 | return -1; /* ERROR */
46 | if (data->destination_label == 0)
47 | /* data is a constant integer number. */
48 | value = data->number;
49 | else{
50 | /* data is an address. */
51 | /* get type of addressed memory cell: data or code word. */
52 | CodeBlock* destination_c = 0;
53 | /* get addressed memory cell.*/
54 | DataBlock* destination_d = 0;
55 | if (!get_label(&destination_c, &destination_d, data->destination_label, labeltree)){
56 | if (!no_error_printing) {
57 | fprintf(stderr,"Internal error: Cannot find label %s again.\n", data->destination_label);
58 | }
59 | return -1; /*ERROR! */
60 | }
61 | /* destination_type: CODE = 0; DATA = 1 */
62 | /* cast destination pointer according to destination_type. */
63 | /* add offset to destination address */
64 | if (destination_c != NULL) {
65 | value = destination_c->offset + data->number;
66 | }else if (destination_d != NULL) {
67 | value = destination_d->offset + data->number;
68 | }else{
69 | if (!no_error_printing) {
70 | fprintf(stderr,"Internal error: Invalid type of label %s.\n",data->destination_label);
71 | }
72 | return -1; /*ERROR! */
73 | }
74 | /* subtract one from pointer address: this is done to compensate the auto increment of C and D register in Malbolge. */
75 | /* handle underflow */
76 | if (value == 0)
77 | value = C2;
78 | else
79 | value--;
80 | }
81 | return value;
82 | }
83 |
84 | int parse_datacell(DataCell* data, LabelTree* labeltree, int no_error_printing) {
85 | int value = -1;
86 | int left = -1;
87 | int right = -1;
88 | if (data==0)
89 | return -1; /* ERROR */
90 | if (data->leaf_element != 0) {
91 | return parse_dataatom(data->leaf_element, labeltree, no_error_printing);
92 | }
93 | left = parse_datacell(data->left_element, labeltree, no_error_printing);
94 | if (left<0 || left > C2)
95 | return -1;
96 | right = parse_datacell(data->right_element, labeltree, no_error_printing);
97 | if (right<0 || right > C2)
98 | return -1;
99 | switch (data->_operator) {
100 | case DATACELL_OPERATOR_PLUS:
101 | value = left + right;
102 | break;
103 | case DATACELL_OPERATOR_MINUS:
104 | value = left - right;
105 | break;
106 | case DATACELL_OPERATOR_TIMES:
107 | value = left * right;
108 | break;
109 | case DATACELL_OPERATOR_DIVIDE:
110 | value = left / right;
111 | break;
112 | case DATACELL_OPERATOR_CRAZY:
113 | value = crazy(left, right);
114 | break;
115 | case DATACELL_OPERATOR_ROTATE_L:
116 | /* WARN IF GREATER/EQUAL 10! */
117 | if (right >= 10 && !no_error_printing) {
118 | fprintf(stderr,"Warning: Rotate left operator (<<) with right operand greater or equal 10 found.\n");
119 | right %= 10;
120 | }
121 | {
122 | int i=0;
123 | value = left;
124 | if (right == 0)
125 | break;
126 | for (i=right;i<10;i++)
127 | value = rotate_right(value);
128 | }
129 | break;
130 | case DATACELL_OPERATOR_ROTATE_R:
131 | /* WARN IF GREATER/EQUAL 10! */
132 | if (right >= 10 && !no_error_printing) {
133 | fprintf(stderr,"Warning: Rotate right operator (>>) with right operand greater or equal 10 found.\n");
134 | right %= 10;
135 | }
136 | {
137 | int i=0;
138 | value = left;
139 | if (right == 0)
140 | break;
141 | for (i=0;i C2)
150 | value %= C2;
151 | while (value < 0)
152 | value += C2+1;
153 | return value;
154 | }
155 |
156 | int generate_opcodes_from_memory_layout(MemoryCell* memory_layout, int last_preinitialized, int* opcodes, LabelTree* labeltree, int no_error_printing, int ignore_fixed_offsets_in_preinitialized_section){
157 | int i;
158 | int last_opcode_from_33_to_126 = 81; /* store it to find best value for reserved code word */
159 | for (i=0;i<=C2;i++){
160 | opcodes[i] = -1;
161 | if (memory_layout[i].usage == CODE || memory_layout[i].usage == PREINITIALIZED_CODE) {
162 | char symbol;
163 | if (!is_xlatcycle_existent(memory_layout[i].code->command, i%94, &symbol)){
164 | if (!no_error_printing)
165 | fprintf(stderr,"Internal error: Cannot find xlat2 cycle again.\n");
166 | return 0; /*ERROR! */
167 | }
168 | opcodes[i] = (unsigned char)symbol;
169 | if (i > last_preinitialized) {
170 | last_opcode_from_33_to_126 = opcodes[i];
171 | }
172 | if (i <= last_preinitialized && !is_valid_initial_character(i%94, symbol) && !ignore_fixed_offsets_in_preinitialized_section) {
173 | if (!no_error_printing)
174 | fprintf(stderr,"Error: Invalid symbol in preinitialized code section.\n");
175 | return 0; /* ERROR! */
176 | }
177 | }else if (memory_layout[i].usage == DATA) {
178 | int data = parse_datacell(memory_layout[i].data->data, labeltree, no_error_printing);
179 | if (data < 0){
180 | if (!no_error_printing)
181 | fprintf(stderr,"Internal error: Found negative value in data segment.\n");
182 | return 0; /*ERROR! */
183 | }
184 | opcodes[i] = data;
185 | if (opcodes[i] < 127 && opcodes[i] > 32) {
186 | last_opcode_from_33_to_126 = opcodes[i];
187 | }
188 | if (i <= last_preinitialized && !ignore_fixed_offsets_in_preinitialized_section) {
189 | if (!no_error_printing)
190 | fprintf(stderr, "Error: Invalid offset in data section.\n");
191 | return 0; /*ERROR! */
192 | }
193 | }else if (memory_layout[i].usage == RESERVED_CODE) {
194 | if (i > last_preinitialized) {
195 | opcodes[i] = (((i%2)==(last_preinitialized%2))?81:last_opcode_from_33_to_126);
196 | }
197 | }
198 | }
199 | return 1;
200 | }
201 |
202 |
203 | int generate_malbolge_initialization_code(int program[], int last_preinitialized, int entrypoint, char malbolge_code[], int no_error_printing, int* execution_steps_until_entry_point, int ignore_wrong_size){
204 |
205 | int i = 0;
206 | int size_left = 59049;
207 | char normalized_code_array[59050];
208 | char* normalized_code_iterator = 0;
209 | int size = 0;
210 | int init_datamodule_length;
211 |
212 | #ifndef DOXYGEN /* don't show these definitions in documentation */
213 | #define NO_PTR {0,0}
214 | #define NO_VALUE 0
215 | #define TMP_INITIAL_VAL C1
216 | #define A_REG_INITIAL 58328
217 | #define START_AT_MODULE 0
218 | #define START_AT_POS_IN_MODULE 8
219 | #define DEST_INITIAL_VAL 68
220 | #define VAL1_INIT_VAL 126
221 | #define VAL2_INIT_VAL 58688
222 | #define VAL3_INIT_VAL 29495
223 | #define UNSET_MODULE_CELL {CELLTYPE_CONST,NO_VALUE,NO_PTR}
224 | #endif
225 |
226 | /* Malbolge code that initializes the constant generation data module. */
227 | const char* init_datamodule = "bP" /* Jmp; Destination for MovD */
228 | "&A@?>=<;:9876543210/.-,+*)('&%$T\"!~}|;]yxwvutslUSRQ.y"
229 | "x+i)J9edFb4`_^]\\yxwRQ)(TSRQ]m!G0KJIyxFvDa%_@?\"=<5:98"
230 | "765.-2+*/.-,+*)('&%$#\"!~}|utyrqvutsrqjonmPkjihgfedc\\"
231 | "DDYAA\\>>Y;;V886L5322G//D,,G))>&&A##!7~5:{y7xvuu,10/.-"
232 | ",+*)('&%$#\"yb}|{zyxwvutmVqSohmOOjihafeHcEa`YAA\\[ZYRW"
233 | ":U7SLKP3NMLK-I,GFED&%%@?>=6;|9y70/4u210/o-n+k)\"!gg$#\""
234 | "!x}`{zyxZvYtsrqSoRmlkjLhKfedcEaD_^]\\>Z=XWVU7S6QPON0LK"
235 | "DI,GFEDCBA#?\"=};438y6543s1r/o-&%*k('&%e#d!~}|^z]xwvuW"
236 | "sVqponPlOjihgIeHcba`B^A\\[ZY;W:UTSR4PI2MLKJ,,AFE(&B;:?"
237 | "\"~<}{zz165v3s+*/pn,mk)jh&ge#db~a_{^\\xwvoXsrqpRnmfkjM"
238 | "Kg`_GG\\aDB^A?[>!<;{98yw5.-ss*/pn,+lj("
241 | "!~ff{\"ca}`^z][wZXtWUqTRnQOkNLhgfIdcFaZ_^A\\[Z=~;:{876w43210/(-,+*)('h%$d\"ca}|_"
243 | "z\\rqYYnsVTpoRPledLLafIGcbE`BXW??TY<:V97S64P31M0.J-+G*"
244 | "(DCB%@?\"=<;|98765.3210p.-n+$)i'h%${\"!~}|{zyxwvuXVlkp"
245 | "SQmlOjLbafIGcbE`BXW??TY<:V97S64P31M0.J-+G*(D'%A@?\"=<}"
246 | ":98y6543,1r/.o,+*)j'&%eez!~a|^tsx[YutWUqjinQOkjMhJ`_dG"
247 | "EaDB^A?[>7~;:{y7x5.3210q.-"
248 | "n+*)jh&%$#\"c~}`{z]rwvutWrkpohmPkjihafI^cba`_^A\\[>YXW"
249 | ":UTS5QP3NM0KJ-HGF?D'BA:?>=~;:z8765v32s0/.-nl$#(ig%fd\""
250 | "ca}|_]yrqvYWsVTpSQmPNjMKgJHdGEa`_B]\\?ZYCB%#?87}}49zx6wu3tr0qo-nl*ki'hf$ec!~}`{^yxwvots"
252 | "rUponQlkMihKIe^]EEZ_B@\\?=Y<:V97S64P31M0.J-+GFE(C&A@?8"
253 | "=<;:{876w43s10qo-&%kk\"'hf$ec!b`|_]y\\ZvYWsVTpSQmlkNiL"
254 | "gf_dcba`C^]\\?ZY;WV97SLK33HM0.J-+G*(D'%A$\">!};|z8yw54"
255 | "3t1r/(";
256 |
257 | /* state of constant generation data module directly after its initialization */
258 | State cur_state = {A_REG_INITIAL, {START_AT_MODULE,START_AT_POS_IN_MODULE}, {
259 | {15,{
260 | {CELLTYPE_CONST,C0,NO_PTR},
261 | {CELLTYPE_PTR,NO_VALUE,{0,12}},
262 | {CELLTYPE_PTR,NO_VALUE,{1,1}},
263 | {CELLTYPE_PTR,NO_VALUE,{2,1}},
264 | {CELLTYPE_PTR,NO_VALUE,{3,1}},
265 | {CELLTYPE_CONST,C1,NO_PTR},
266 | {CELLTYPE_VAR,TMP_INITIAL_VAL,NO_PTR},
267 | {CELLTYPE_VAR,C2-2*9-2*9*3-2*9*9-2*9*9*3,NO_PTR},
268 | {CELLTYPE_VAR,C2-2*9-2*9*3-2*9*9,NO_PTR},
269 | {CELLTYPE_VAR,C2-2*9-2*9*3,NO_PTR},
270 | {CELLTYPE_VAR,C2-2*9,NO_PTR},
271 | {CELLTYPE_CONST,C2,NO_PTR},
272 | {CELLTYPE_VAR,DEST_INITIAL_VAL,NO_PTR},
273 | {CELLTYPE_PTR,NO_VALUE,{0,6}},
274 | {CELLTYPE_PTR,NO_VALUE,{0,0}}
275 | }},
276 | {8,{
277 | {CELLTYPE_CONST,C0,NO_PTR},
278 | {CELLTYPE_CONST,C1,NO_PTR},
279 | {CELLTYPE_C20_OR_C21,C21,NO_PTR},
280 | {CELLTYPE_VAR,VAL1_INIT_VAL,NO_PTR},
281 | {CELLTYPE_CONST,C2,NO_PTR},
282 | {CELLTYPE_PTR,NO_VALUE,{1,3}},
283 | {CELLTYPE_PTR,NO_VALUE,{1,0}},
284 | {CELLTYPE_PTR,NO_VALUE,{0,12}},
285 | UNSET_MODULE_CELL,
286 | UNSET_MODULE_CELL,
287 | UNSET_MODULE_CELL,
288 | UNSET_MODULE_CELL,
289 | UNSET_MODULE_CELL,
290 | UNSET_MODULE_CELL,
291 | UNSET_MODULE_CELL
292 | }},
293 | {8,{
294 | {CELLTYPE_CONST,C0,NO_PTR},
295 | {CELLTYPE_CONST,C1,NO_PTR},
296 | {CELLTYPE_C20_OR_C21,C21,NO_PTR},
297 | {CELLTYPE_VAR,VAL2_INIT_VAL,NO_PTR},
298 | {CELLTYPE_CONST,C2,NO_PTR},
299 | {CELLTYPE_PTR,NO_VALUE,{2,3}},
300 | {CELLTYPE_PTR,NO_VALUE,{2,0}},
301 | {CELLTYPE_PTR,NO_VALUE,{0,12}},
302 | UNSET_MODULE_CELL,
303 | UNSET_MODULE_CELL,
304 | UNSET_MODULE_CELL,
305 | UNSET_MODULE_CELL,
306 | UNSET_MODULE_CELL,
307 | UNSET_MODULE_CELL,
308 | UNSET_MODULE_CELL
309 | }},
310 | {8,{
311 | {CELLTYPE_CONST,C0,NO_PTR},
312 | {CELLTYPE_CONST,C1,NO_PTR},
313 | {CELLTYPE_C20_OR_C21,C21,NO_PTR},
314 | {CELLTYPE_VAR,VAL3_INIT_VAL,NO_PTR},
315 | {CELLTYPE_CONST,C2,NO_PTR},
316 | {CELLTYPE_PTR,NO_VALUE,{3,3}},
317 | {CELLTYPE_PTR,NO_VALUE,{3,0}},
318 | {CELLTYPE_PTR,NO_VALUE,{0,12}},
319 | UNSET_MODULE_CELL,
320 | UNSET_MODULE_CELL,
321 | UNSET_MODULE_CELL,
322 | UNSET_MODULE_CELL,
323 | UNSET_MODULE_CELL,
324 | UNSET_MODULE_CELL,
325 | UNSET_MODULE_CELL
326 | }}
327 | }, 0};
328 |
329 | cur_state.last_preinitialized = last_preinitialized;
330 |
331 |
332 | /* get size of init_datamodule. */
333 | init_datamodule_length = (int)strlen(init_datamodule);
334 |
335 | /* copy initialization code to Malbolge code array. */
336 | memcpy(normalized_code_array,init_datamodule,init_datamodule_length);
337 |
338 | size_left = 59049 - init_datamodule_length;
339 | normalized_code_iterator = normalized_code_array + init_datamodule_length;
340 |
341 |
342 | /* TODO */
343 |
344 |
345 | for (i=last_preinitialized+1;i<=C2;i++) {
346 | if (program[i] >= 0) {
347 | size = generate_normalized_init_code_for_word_with_module_system(i, program[i], normalized_code_iterator, &cur_state, size_left, no_error_printing);
348 | if (size < 0) {
349 | if (!no_error_printing)
350 | fprintf(stderr,"Internal error: Initialization code for memory cell %d could not be generated.\n",i);
351 | return -1;
352 | }
353 | size_left -= size;
354 | normalized_code_iterator += size;
355 | }
356 | }
357 |
358 | /* generate ENTRY POINT ADDRESS */
359 | size = generate_jump_to_entrypoint_with_module_system(entrypoint, normalized_code_iterator, &cur_state, size_left, no_error_printing);
360 | if (size < 0) {
361 | if (!no_error_printing)
362 | fprintf(stderr,"Internal error: Code for jump to entry point at %d could not be generated.\n",entrypoint);
363 | return -1;
364 | }
365 | size_left -= size;
366 | normalized_code_iterator += size;
367 | *normalized_code_iterator = 0;
368 |
369 | if (size_left < 0){
370 | if (!no_error_printing)
371 | fprintf(stderr,"Error: Not enough space!\n");
372 | return -1;
373 | }
374 | normalized_code_array[59049-size_left] = 'i'; /* START PROGRAM EXECUTION! */
375 | if (execution_steps_until_entry_point != 0)
376 | *execution_steps_until_entry_point = (59049-size_left) - 98; /* 98: Number of skipped cells due to initial jump command */
377 | size_left--;
378 |
379 | if (59049-size_left > last_preinitialized-1){
380 | return -1; /* program size exceeded! */
381 | }
382 |
383 | /* fill unused area from ("JMP to entry point"+1) until (last_preinitialized-2) with random symbols. //NOPs. */
384 | for (i=59049-size_left;i= 0) {/* && memory[i].usage != RESERVED_CODE) { */
392 | if (!no_error_printing)
393 | fprintf(stderr,"Error: Overlapping between HeLL program and Malbolge initialization code detected.\n");
394 | return -1;
395 | }
396 | }
397 |
398 | /* disallow preinitialized code at magic command sequence at the end of the preinitialized section. */
399 | for (i=last_preinitialized-1;i<=last_preinitialized;i++) {
400 | if (program[i] >= 0) { /* && memory[i].usage != RESERVED_CODE) { */
401 | if (!no_error_printing)
402 | fprintf(stderr,"Error: Overlapping between HeLL program and Malbolge initialization code detected.\n");
403 | return -1;
404 | }
405 | }
406 |
407 | /* transform normalized Malbolge code to denormalized executable Malbolge code. */
408 | if (!denormalize_malbolge(&(normalized_code_array[init_datamodule_length]), init_datamodule_length, last_preinitialized-1-init_datamodule_length, no_error_printing)) {
409 | if (!no_error_printing)
410 | fprintf(stderr,"Internal error while generating denormalized code.\n");
411 | return -1;
412 | }
413 |
414 | /* add magic code sequence at the end of the program. */
415 | normalized_code_array[last_preinitialized-1] = 'R';
416 | normalized_code_array[last_preinitialized] = 'Q';
417 | normalized_code_array[last_preinitialized+1] = 0;
418 |
419 | /* allow overriding NOPs before constant generation data module area. => Area for old data module. Current data module uses some memory cells in the area from 2 to 81. */
420 | /*for (i=2;i<82;i++){ */
421 | /* if (program[i] >= 0 && is_valid_initial_character(i%94, program[i])) */
422 | /* normalized_code_array[i] = program[i]; */
423 | /*} */
424 |
425 | /* memory cells 0 and 1 must only be preinitialized with "bP", nothing else! */
426 | for (i=0;i<2;i++) {
427 | if (program[i] >= 0 && is_valid_initial_character(i%94, program[i]) && program[i] != normalized_code_array[i]) {
428 | if (!no_error_printing)
429 | fprintf(stderr,"Error: Overlapping between HeLL program and Malbolge initialization code detected.\n");
430 | return -1;
431 | }
432 | }
433 |
434 | /* allow overriding NOPs between the end of initialization code and the end of preinitialized code area. */
435 | if (!ignore_wrong_size) {
436 | for (i=59049-size_left;i= 0 && is_valid_initial_character(i%94, program[i])!=0)
438 | normalized_code_array[i] = program[i];
439 | else if (program[i] >= 0) {
440 | if (!no_error_printing)
441 | fprintf(stderr,"Internal error: Cannot place command %d at position %d.\n",(int)program[i],i);
442 | return -1;
443 | }
444 | }
445 | }
446 |
447 |
448 | memcpy(malbolge_code,normalized_code_array, last_preinitialized+2);
449 |
450 | return 59049-size_left; /* success */
451 |
452 | }
453 |
454 |
455 |
--------------------------------------------------------------------------------
/src/lmao.y:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an assembler for Malbolge.
4 | Copyright (C) 2013-2017 Matthias Lutter
5 |
6 | LMAO is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | LMAO is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 |
19 | E-Mail: matthias@lutter.cc
20 |
21 | */
22 |
23 | %{
24 | #include
25 | #include
26 | #include
27 |
28 | #include "typedefs.h"
29 | #include "malbolge.h"
30 |
31 |
32 | #ifndef YYLTYPE_IS_DECLARED
33 | typedef struct YYLTYPE
34 | {
35 | int first_line;
36 | int first_column;
37 | int last_line;
38 | int last_column;
39 | } YYLTYPE;
40 | #define YYLTYPE_IS_DECLARED
41 | #endif
42 |
43 | #define COPY_CODE_POSITION(to, from_start, from_end) {(to).first_line = (from_start).first_line;(to).first_column = (from_start).first_column;(to).last_line = (from_end).last_line;(to).last_column = (from_end).last_column;};
44 |
45 |
46 | /**
47 | * Print error.
48 | */
49 | int yyerror(const char *s);
50 |
51 | /* avoid implicit declaration warning */
52 | int yylex(void);
53 |
54 | /**
55 | * Inserts a label into the label tree.
56 | */
57 | int insert_label(unsigned char destination_type, void* destination, const char* label, LabelTree** labeltree, YYLTYPE* code_position);
58 |
59 | /**
60 | * Adds a data block to the list of data blocks.
61 | */
62 | void add_datablock(DataBlock* block, DataBlocks* datablocks);
63 |
64 | /**
65 | * Adds a code block to the list of code blocks.
66 | */
67 | void add_codeblock(CodeBlock* block, CodeBlocks* codeblocks);
68 |
69 | /**
70 | * Counts the number of escaped characters ('\' followed by arbitrary character) in a given string.
71 | */
72 | int count_escaped(const char* s);
73 |
74 | YYLTYPE yylloc;
75 | %}
76 |
77 | %define parse.lac full
78 | // TODO bison version check
79 | %error_verbose
80 | //%define parse.error verbose
81 |
82 | /* Symbols. */
83 | %union
84 | {
85 | const char* s_val;
86 | unsigned int i_val;
87 | unsigned char c_val;
88 | unsigned char prefix;
89 |
90 | XlatCycle* xlat;
91 | DataAtom* dataatom;
92 | DataCell* datacell;
93 |
94 | DataBlock* datablock;
95 | CodeBlock* codeblock;
96 | };
97 |
98 | %token IDENTIFIER
99 | %token LABEL
100 | %token EMPTYLINE
101 | %token CSEC ".CODE"
102 | %token DSEC ".DATA"
103 | %token RNOP
104 | %token SLASH
105 | %token OFFSET ".OFFSET or @"
106 | %token DONTCARE
107 | %token NOTUSED
108 | %token BRACKETLEFT
109 | %token BRACKETRIGHT
110 | %token COMMA
111 | %token U_PREFIXED_IDENTIFIER
112 | %token R_PREFIXED_IDENTIFIER
113 | %token STRING
114 | %token CONSTANT
115 | %token COMMAND
116 | %token PLUSMINUS "+ or -"
117 | %token MULDIV "* or /"
118 | %token SHIFT
119 | %token CRAZY
120 |
121 | %type XlatCycle
122 | %type Codeexpression
123 | %type Dataexpression
124 | %type Offset
125 | %type Dataexpressions
126 | %type Datablock
127 | %type Codeexpressions
128 | %type Codeblock
129 | %type Dataatom
130 | %type Product
131 | %type Crazied
132 | %type Sum
133 |
134 | %start Start
135 |
136 | %%
137 |
138 | Start: EMPTYLINE Start | Program;
139 |
140 | Program: | Code Program
141 | | Data Program;
142 |
143 | Code: CSEC Codeblocks;
144 |
145 | Codeblocks: Codeblock { if ($1 != 0) add_codeblock($1, &codeblocks);}
146 | | Codeblocks EMPTYLINE Codeblock { if ($3 != 0) add_codeblock($3, &codeblocks);};
147 |
148 | Offset: OFFSET CONSTANT IgnoreEmptylines {$$=$2;};
149 |
150 | IgnoreEmptylines: EMPTYLINE IgnoreEmptylines |
151 |
152 | Codeblock: {$$ = 0;}
153 | | Offset LABEL Codeexpressions { if (!insert_label(0, $3, $2, &labeltree, &@2)) return 1; $$=$3; if ($$ != 0) $$->offset = $1;}
154 | | LABEL Codeexpressions { if (!insert_label(0, $2, $1, &labeltree, &@1)) return 1; $$=$2; };
155 |
156 | Codeexpressions: { $$ = 0;}
157 | | LABEL Codeexpressions { if (!insert_label(0, $2, $1, &labeltree, &@1)) return 1; $$=$2; }
158 | | Codeexpression Codeexpressions { $$=(CodeBlock*)malloc(sizeof(CodeBlock)); $$->next = $2; $$->virtual_block = 0; if ($2 != 0) $2->prev = $$; $$->prev=0; $$->offset=-1; $$->command=$1; if ($2 == 0) $$->num_of_blocks = 1; else $$->num_of_blocks = $2->num_of_blocks+1; COPY_CODE_POSITION($$->code_position, @1, @1); };
159 |
160 | Codeexpression: RNOP {$$=(XlatCycle*)malloc(sizeof(XlatCycle)); $$->cmd = MALBOLGE_COMMAND_NOP; $$->next = $$; /*RNOP: link to itself*/ COPY_CODE_POSITION($$->code_position,@1,@1); } //COPY_CODE_POSITION(@$, @1, @1);}
161 | | XlatCycle {$$=$1; };//COPY_CODE_POSITION(@$, @1, @1);};
162 |
163 | XlatCycle: COMMAND { $$=(XlatCycle*)malloc(sizeof(XlatCycle)); $$->next = 0; $$->cmd = $1; COPY_CODE_POSITION($$->code_position,@1,@1); } //COPY_CODE_POSITION(@$, @1, @1); }
164 | | COMMAND SLASH XlatCycle { $$=(XlatCycle*)malloc(sizeof(XlatCycle)); $$->next = $3; $$->cmd = $1; COPY_CODE_POSITION($$->code_position,@1,@1); }; //COPY_CODE_POSITION(@$, @1, @3); };
165 |
166 | Data: DSEC Datablocks;
167 |
168 | Datablocks: Datablock {if ($1 != 0) add_datablock($1, &datablocks);}
169 | | Datablocks EMPTYLINE Datablock {if ($3 != 0) add_datablock($3, &datablocks);};
170 |
171 | Datablock: { $$ = 0; }
172 | | Offset LABEL Dataexpressions { if (!insert_label(1, $3, $2, &labeltree, &@2)) return 1; $$=$3; $$->offset = $1; }
173 | | LABEL Dataexpressions { if (!insert_label(1, $2, $1, &labeltree, &@1)) return 1; $$=$2; };
174 |
175 | Dataexpressions: { $$ = 0;}
176 | | LABEL Dataexpressions { if (!insert_label(1, $2, $1, &labeltree, &@1)) return 1; $$=$2; }
177 | | Dataexpression Dataexpressions { $$=(DataBlock*)malloc(sizeof(DataBlock)); $$->next = $2; if ($2 != 0) $2->prev = $$; $$->prev=0; $$->offset=-1; $$->data=$1; if ($2 == 0) $$->num_of_blocks = 1; else $$->num_of_blocks = $2->num_of_blocks+1; COPY_CODE_POSITION($$->code_position, @1, @1); }
178 | | STRING Dataexpressions {
179 | int length = (int)strlen($1)-2;
180 | //COPY_CODE_POSITION(@$,@1,@2);
181 | if (length <= 0){
182 | $$=$2;
183 | }
184 | else{
185 | int escaped = count_escaped($1);
186 | int processed = 0;
187 | DataBlock* tmp;
188 | DataBlock* tmp_old=0;
189 | int i;
190 | int char_start_column;
191 | int char_end_column;
192 | $$=0;
193 | for (i=0;iprev = tmp_old;
198 | if (tmp_old != 0)
199 | tmp_old->next = tmp;
200 | tmp->next = $2;
201 | if ($2 != 0)
202 | $2->prev = tmp;
203 | char_start_column = @1.first_column+i+1;
204 | tmp->offset=-1;
205 | tmp->data=(DataCell*)malloc(sizeof(DataCell));
206 | tmp->data->_operator = DATACELL_OPERATOR_LEAF_ELEMENT;
207 | tmp->data->left_element = 0;
208 | tmp->data->right_element = 0;
209 | tmp->data->leaf_element = (DataAtom*)malloc(sizeof(DataAtom));
210 | tmp->data->leaf_element->destination_label = 0;
211 | tmp->data->leaf_element->operand_label = 0;
212 | if ($1[i+1] == '\\'){
213 | i++;
214 | tmp->data->leaf_element->number = (int)((unsigned char)$1[i+1]);
215 | switch ($1[i+1]){
216 | case 'n':
217 | tmp->data->leaf_element->number = (int)((unsigned char)'\n');
218 | break;
219 | case 'r':
220 | tmp->data->leaf_element->number = (int)((unsigned char)'\r');
221 | break;
222 | case 't':
223 | tmp->data->leaf_element->number = (int)((unsigned char)'\t');
224 | break;
225 | case '0':
226 | tmp->data->leaf_element->number = 0;
227 | break;
228 | default:
229 | break;
230 | }
231 | }else
232 | tmp->data->leaf_element->number = (int)((unsigned char)$1[i+1]);
233 | if ($2 == 0)
234 | tmp->num_of_blocks = (length-escaped-processed);
235 | else
236 | tmp->num_of_blocks = $2->num_of_blocks+(length-escaped-processed);
237 | tmp_old = tmp; processed++;
238 | char_end_column = @1.first_column+i+1;
239 | COPY_CODE_POSITION(tmp->data->leaf_element->code_position,@1,@1);
240 | tmp->data->leaf_element->code_position.first_column = char_start_column;
241 | tmp->data->leaf_element->code_position.last_column = char_end_column;
242 | COPY_CODE_POSITION(tmp->code_position, tmp->data->leaf_element->code_position, tmp->data->leaf_element->code_position);
243 | }
244 | } }
245 | | STRING COMMA Dataexpression Dataexpressions {
246 | int length = (int)strlen($1)-2;
247 | //COPY_CODE_POSITION(@$,@1,@4);
248 | if (length <= 0)
249 | $$ = $4;
250 | else {
251 | int escaped = count_escaped($1);
252 | int processed = 0;
253 | DataBlock* tmp;
254 | DataBlock* tmp_old=0;
255 | int i;
256 | $$=0;
257 | for (i=0;iprev = tmp_old;
266 | if (tmp_old != 0)
267 | tmp_old->next = tmp;
268 | tmp->next = $4;
269 | if ($4 != 0)
270 | $4->prev = tmp;
271 | tmp->offset=-1;
272 | tmp->data=(DataCell*)malloc(sizeof(DataCell));
273 | tmp->data->_operator = DATACELL_OPERATOR_LEAF_ELEMENT;
274 | tmp->data->left_element = 0;
275 | tmp->data->right_element = 0;
276 | tmp->data->leaf_element = 0;
277 | if (i%2==0){
278 | char_start_column = @1.first_column+i/2+1;
279 | char_start_line = @1.first_line;
280 | char_end_line = @1.first_line;
281 | tmp->data->leaf_element = (DataAtom*)malloc(sizeof(DataAtom));
282 | tmp->data->leaf_element->destination_label = 0;
283 | tmp->data->leaf_element->operand_label = 0;
284 | tmp->data->leaf_element->destination_label = 0;
285 | tmp->data->leaf_element->operand_label = 0;
286 | if ($1[i/2+1] == '\\'){
287 | i+=2;
288 | tmp->data->leaf_element->number = (int)((unsigned char)$1[i/2+1]);
289 | switch ($1[i/2+1]){
290 | case 'n':
291 | tmp->data->leaf_element->number = (int)((unsigned char)'\n');
292 | break;
293 | case 'r':
294 | tmp->data->leaf_element->number = (int)((unsigned char)'\r');
295 | break;
296 | case 't':
297 | tmp->data->leaf_element->number = (int)((unsigned char)'\t');
298 | break;
299 | case '0':
300 | tmp->data->leaf_element->number = 0;
301 | break;
302 | default:
303 | break;
304 | }
305 | }else
306 | tmp->data->leaf_element->number = (int)((unsigned char)$1[i/2+1]);
307 | char_end_column = @1.first_column+i/2+1;
308 | }else{
309 | memcpy(tmp->data,$3,sizeof(DataCell));
310 | char_start_column = @3.first_column;
311 | char_start_line = @3.first_line;
312 | char_end_line = @3.last_line;
313 | char_end_column = @3.last_column;
314 | }
315 | if ($4 == 0)
316 | tmp->num_of_blocks = ((length-escaped)*2-1-processed);
317 | else
318 | tmp->num_of_blocks = $4->num_of_blocks+((length-escaped)*2-1-processed);
319 | tmp_old = tmp;
320 | processed++;
321 | tmp->data->leaf_element->code_position.first_line = char_start_line;
322 | tmp->data->leaf_element->code_position.first_column = char_start_column;
323 | tmp->data->leaf_element->code_position.last_line = char_end_line;
324 | tmp->data->leaf_element->code_position.last_column = char_end_column;
325 | COPY_CODE_POSITION(tmp->code_position, tmp->data->leaf_element->code_position, tmp->data->leaf_element->code_position);
326 | }
327 | } };
328 |
329 | Dataatom: CONSTANT { $$=(DataAtom*)malloc(sizeof(DataAtom)); $$->destination_label = 0; $$->number = $1; $$->operand_label = 0; $$->code_position.first_column = @1.first_column; COPY_CODE_POSITION($$->code_position,@1,@1);}
330 | | IDENTIFIER { $$=(DataAtom*)malloc(sizeof(DataAtom)); $$->destination_label = $1; $$->number = 0; $$->operand_label = 0; COPY_CODE_POSITION($$->code_position,@1,@1);}
331 | | R_PREFIXED_IDENTIFIER { $$=(DataAtom*)malloc(sizeof(DataAtom)); $$->destination_label = $1; $$->number = 1; $$->operand_label = 0; COPY_CODE_POSITION($$->code_position,@1,@1);}
332 | | U_PREFIXED_IDENTIFIER IDENTIFIER { $$=(DataAtom*)malloc(sizeof(DataAtom)); $$->destination_label = $1; $$->number = 0; $$->operand_label = $2; COPY_CODE_POSITION($$->code_position,@1,@2);};
333 |
334 |
335 |
336 |
337 |
338 | Dataexpression: Dataexpression SHIFT Crazied { $$=(DataCell*)malloc(sizeof(DataCell)); $$->leaf_element = 0; $$->_operator = ($2=='>'?DATACELL_OPERATOR_ROTATE_R:DATACELL_OPERATOR_ROTATE_L); $$->left_element = $1; $$->right_element = $3; }
339 | | Crazied { $$=$1;}
340 | | DONTCARE { $$=(DataCell*)malloc(sizeof(DataCell)); $$->leaf_element = 0; $$->_operator = DATACELL_OPERATOR_DONTCARE; $$->left_element = 0; $$->right_element = 0;}
341 | | NOTUSED { $$=(DataCell*)malloc(sizeof(DataCell)); $$->leaf_element = 0; $$->_operator = DATACELL_OPERATOR_NOT_USED; $$->left_element = 0; $$->right_element = 0;};
342 |
343 |
344 | Crazied: Crazied CRAZY Sum { $$=(DataCell*)malloc(sizeof(DataCell)); $$->leaf_element = 0; $$->_operator = DATACELL_OPERATOR_CRAZY; $$->left_element = $1; $$->right_element = $3; }
345 | | Sum { $$ = $1; };
346 |
347 |
348 | Sum: Sum PLUSMINUS Product { $$=(DataCell*)malloc(sizeof(DataCell)); $$->leaf_element = 0; $$->_operator = ($2=='+'?DATACELL_OPERATOR_PLUS:DATACELL_OPERATOR_MINUS); $$->left_element = $1; $$->right_element = $3; }
349 | | Product { $$ = $1; };
350 |
351 |
352 | Product: Product MULDIV Dataatom { $$=(DataCell*)malloc(sizeof(DataCell)); $$->leaf_element = 0; $$->_operator = ($2=='*'?DATACELL_OPERATOR_TIMES:DATACELL_OPERATOR_DIVIDE); $$->left_element = $1; $$->right_element = (DataCell*)malloc(sizeof(DataCell)); $$->right_element->leaf_element = $3; $$->right_element->_operator = DATACELL_OPERATOR_LEAF_ELEMENT; $$->right_element->left_element = 0; $$->right_element->right_element = 0; }
353 | | Product MULDIV BRACKETLEFT Dataexpression BRACKETRIGHT { $$=(DataCell*)malloc(sizeof(DataCell)); $$->leaf_element = 0; $$->_operator = ($2=='*'?DATACELL_OPERATOR_TIMES:DATACELL_OPERATOR_DIVIDE); $$->left_element = $1; $$->right_element = $4; }
354 | | Dataatom { $$=(DataCell*)malloc(sizeof(DataCell)); $$->leaf_element = $1; $$->_operator = DATACELL_OPERATOR_LEAF_ELEMENT; $$->left_element = 0; $$->right_element = 0; }
355 | | BRACKETLEFT Dataexpression BRACKETRIGHT { $$ = $2; };
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 | %%
365 |
366 | int yyerror(const char *s) {
367 | fprintf(stderr,"Error: %s at line %d column %d.\n",s, yylloc.first_line, yylloc.first_column);
368 | return 0;
369 | }
370 |
371 | void add_datablock(DataBlock* block, DataBlocks* datablocks){
372 | if (datablocks == 0)
373 | return; /* ERROR! */
374 | datablocks->datafield = (DataBlock**)realloc(datablocks->datafield, (datablocks->size+1)*sizeof(DataBlock*));
375 | datablocks->datafield[datablocks->size] = block;
376 | datablocks->size++;
377 | }
378 |
379 | void add_codeblock(CodeBlock* block, CodeBlocks* codeblocks){
380 | if (codeblocks == 0)
381 | return; /* ERROR! */
382 | codeblocks->codefield = (CodeBlock**)realloc(codeblocks->codefield, (codeblocks->size+1)*sizeof(CodeBlock*));
383 | codeblocks->codefield[codeblocks->size] = block;
384 | codeblocks->size++;
385 | }
386 |
387 | int insert_label(unsigned char destination_type, void* destination, const char* label, LabelTree** labeltree, YYLTYPE* code_position){
388 | if (labeltree == 0) {
389 | yyerror("Internal error: Pointer to label tree is NULL");
390 | return 0; /* ERROR */
391 | }
392 |
393 | if (label == 0) {
394 | yyerror("Internal error: Label is NULL");
395 | return 0; /* ERROR */
396 | }
397 |
398 | if (destination == 0) {
399 | yyerror("Label definition must be followed by a data or code word");
400 | return 0; /* ERROR */
401 | }
402 |
403 | while(*labeltree != 0) {
404 | /* select sub tree */
405 | int cmp = strncmp(label,(*labeltree)->label,101);
406 | if (cmp > 0){
407 | labeltree = &((*labeltree)->left);
408 | }else if (cmp < 0){
409 | labeltree = &((*labeltree)->right);
410 | }else{
411 | char error_msg[140];
412 | if (strlen(label)<=100)
413 | sprintf(error_msg,"Label %s is defined multiple times",label);
414 | else
415 | sprintf(error_msg,"Label is defined multiple times");
416 | yyerror(error_msg);
417 | return 0;
418 | }
419 | }
420 |
421 | *labeltree = (LabelTree*)malloc(sizeof(LabelTree));
422 | (*labeltree)->left = 0;
423 | (*labeltree)->right = 0;
424 | (*labeltree)->label = label;
425 | COPY_CODE_POSITION((*labeltree)->code_position, (*code_position), (*code_position));
426 | if (destination_type == 0) {
427 | /* Code Block */
428 | (*labeltree)->destination_code = (CodeBlock*)destination;
429 | (*labeltree)->destination_data = NULL;
430 | }else{
431 | /* Data Block */
432 | (*labeltree)->destination_code = NULL;
433 | (*labeltree)->destination_data = (DataBlock*)destination;
434 | }
435 | return 1;
436 | }
437 |
438 | int count_escaped(const char* s){
439 | int len = (int)strlen(s);
440 | int i;
441 | int ret=0;
442 | for (i=0;i.
19 | *
20 | * E-Mail: matthias@lutter.cc
21 | *
22 | *
23 | * Example in HeLL: Digital root calculator.
24 | *
25 | * The digital root of a number is the reduction of the number to a single
26 | * digit through repeated summing.
27 | * This program reads one line from STDIN.
28 | * It ignores all characters that are not in the range from '0' to '9' and
29 | * calculates the digital root of the input.
30 | *
31 | * To be more exact, the character '0' is ignored by the program, too.
32 | *
33 | *
34 | * The main techniques used by the code below came from reverse engineering
35 | * of the 99 bottles of beer program by Hisashi Iizawa et al. [1] and the
36 | * ROT-13 program [2].
37 | * [1] http://www.99-bottles-of-beer.net/language-malbolge-995.html
38 | * [2] http://www.trs.cm.is.nagoya-u.ac.jp/Malbolge/index.html.en
39 | */
40 |
41 | .CODE
42 | // flags used to return from variable manipulations and subroutines
43 | SUBROUTINE_FLAG1:
44 | Nop/MovD
45 | Jmp
46 |
47 | SUBROUTINE_FLAG2:
48 | Nop/MovD
49 | Jmp
50 |
51 | FLAG1:
52 | Nop/MovD
53 | Jmp
54 |
55 | FLAG2:
56 | Nop/MovD
57 | Jmp
58 |
59 | FLAG3:
60 | Nop/MovD
61 | Jmp
62 |
63 | FLAG4:
64 | Nop/MovD
65 | Jmp
66 |
67 | FLAG5:
68 | Nop/MovD
69 | Jmp
70 |
71 | FLAG6:
72 | Nop/MovD
73 | Jmp
74 |
75 | FLAG7:
76 | Nop/MovD
77 | Jmp
78 |
79 | FLAG8:
80 | Nop/MovD
81 | Jmp
82 |
83 | FLAG9:
84 | Nop/MovD
85 | Jmp
86 |
87 | // flags that act as loop counter
88 |
89 | LOOP2:
90 | Nop/MovD
91 | Jmp
92 |
93 | LOOP2_2:
94 | Nop/MovD
95 | Jmp
96 |
97 | LOOP2_3:
98 | Nop/MovD
99 | Jmp
100 |
101 | LOOP2_4:
102 | Nop/MovD
103 | Jmp
104 |
105 | LOOP5:
106 | Nop/Nop/Nop/Nop/MovD
107 | Jmp
108 |
109 | LOOP5_2:
110 | Nop/Nop/Nop/Nop/MovD
111 | Jmp
112 |
113 | LOOP5_3:
114 | Nop/Nop/Nop/Nop/MovD
115 | Jmp
116 |
117 |
118 | // flags for program logic
119 |
120 | // used inside increment and decrement subroutines
121 | // if this is set when the routine returns
122 | // it indicates an overflow (C2->C0 or C0->C2).
123 | NO_MORE_CARRY_FLAG:
124 | Nop/MovD
125 | Jmp
126 |
127 | // is set to indicate that the user did some relevant input
128 | READ_NONZERO_DIGIT_FLAG:
129 | Nop/MovD
130 | Jmp
131 |
132 | // is set to indicate that the user did some input
133 | // which is not newline or EOF, so the program
134 | // should read another byte from the user.
135 | // if this is not set, the digital root should be printed out.
136 | NO_EXIT_FLAG:
137 | Nop/MovD
138 | Jmp
139 |
140 | // if this is not set, the digital root is 0
141 | // otherwise it is in the range 1..9, which can
142 | // be stored by the MovD-cycles defined below.
143 | EVER_READ_NONZERO_DIGIT_FLAG:
144 | Nop/MovD
145 | Jmp
146 |
147 |
148 | // "variables" to store digits
149 |
150 | // stores the digit read from terminal
151 | // it is stored by the current position of the MovD-cycle
152 | TMP_DIGIT:
153 | Nop/Nop/Nop/Nop/Nop/Nop/Nop/Nop/MovD
154 | Jmp
155 |
156 | // stores the current digital sum
157 | // it is stored by the current position of the MovD-cycle
158 | DIGIT:
159 | Nop/Nop/Nop/Nop/Nop/Nop/Nop/Nop/MovD
160 | Jmp
161 |
162 |
163 |
164 | // NOP: used with U_-prefix to skip some code
165 | NOP:
166 | Jmp
167 |
168 |
169 | // loop-resistant commands follow
170 |
171 | MOVED:
172 | MovD/Nop
173 | Jmp
174 |
175 | CRAZY:
176 | Opr/Nop
177 | Jmp
178 |
179 | ROT:
180 | Rot/Nop
181 | Jmp
182 |
183 | HALT:
184 | Hlt
185 |
186 | OUT:
187 | Out/Nop
188 | Jmp
189 |
190 | IN:
191 | In/Nop
192 | Jmp
193 |
194 |
195 | // used in increment and decrement subroutines for carry-detection
196 | @C21 CARRY:
197 | RNop
198 | RNop
199 | Jmp
200 |
201 |
202 | .DATA
203 |
204 | // var value
205 | // value: increment and decrement subroutines operate on this variable.
206 | // this is used as a parameter-register for the subroutines.
207 | // so every value we wish to manipulate has to be stored here.
208 | // we store user input here and use the variable for output-
209 | // generation as well.
210 | crazy_value:
211 | U_CRAZY value
212 | rot_value:
213 | U_ROT value
214 | value:
215 | C1
216 | FLAG1 return_from_value_1 R_FLAG1
217 | FLAG2 return_from_value_2 R_FLAG2
218 | FLAG3 return_from_value_3 R_FLAG3
219 | FLAG4 return_from_value_4 R_FLAG4
220 | FLAG5 return_from_value_5 R_FLAG5
221 | FLAG6 return_from_value_6 R_FLAG6
222 | FLAG7 return_from_value_7 R_FLAG7
223 | FLAG8 return_from_value_8 R_FLAG8
224 | FLAG9 return_from_value_9
225 |
226 |
227 | // var value_C1
228 | // value_C1: the character we read in is crazied into this C1 memory cell first.
229 | // this is just a temporary variable we need to store the original user
230 | // input later.
231 | crazy_value_C1:
232 | U_CRAZY value_C1
233 | rot_value_C1:
234 | U_ROT value_C1
235 | value_C1:
236 | C1
237 | FLAG1 return_from_value_C1_1 R_FLAG1
238 | FLAG2 return_from_value_C1_2
239 |
240 |
241 | // var carry
242 | // carry: this variable stores whether there is an overflow or not
243 | // (used by subroutines increment and decrement).
244 | // CARRY from .CODE-section will be used to branch dependent on variable value,
245 | // which is either CARRY or CARRY+1
246 | crazy_carry:
247 | U_CRAZY carry
248 | exec_carry:
249 | R_MOVED
250 | carry:
251 | CARRY
252 | U_NOP execution_not_jumped_to_carry
253 | U_NOP carry_was_not_set U_NOP carry_was_set
254 | carry_was_set:
255 | MOVED return_carry_was_set
256 | carry_was_not_set:
257 | MOVED return_carry_was_not_set
258 | execution_not_jumped_to_carry:
259 | FLAG1 return_from_carry_1 R_FLAG1
260 | FLAG2 return_from_carry_2 R_FLAG2
261 | return_carry_was_set:
262 | FLAG1 return_from_carry_was_set_1 R_FLAG1
263 | FLAG2 return_from_carry_was_set_2 R_FLAG2
264 | return_carry_was_not_set:
265 | FLAG1 return_from_carry_was_not_set_1 R_FLAG1
266 | FLAG2 return_from_carry_was_not_set_2
267 |
268 | // var tmp
269 | // tmp: used during increment and decrement
270 | crazy_tmp:
271 | U_CRAZY tmp
272 | tmp:
273 | C0 // must not contain any trit that is 2.
274 | FLAG1 return_from_tmp_1 R_FLAG1
275 | FLAG2 return_from_tmp_2 R_FLAG2
276 | FLAG3 return_from_tmp_3 R_FLAG3
277 | FLAG4 return_from_tmp_4
278 |
279 |
280 |
281 | restore_initial_state:
282 | // write C1 into value and value_C1.
283 | ROT C1 R_ROT
284 | R_MOVED
285 | reset_value_loop:
286 | R_MOVED
287 | R_FLAG1
288 | MOVED crazy_value
289 | return_from_value_1:
290 | R_CRAZY
291 | LOOP2 value_reset
292 | R_MOVED
293 | MOVED reset_value_loop
294 |
295 | value_reset:
296 | reset_value_C1_loop:
297 | R_MOVED
298 | R_FLAG1
299 | MOVED crazy_value_C1
300 | return_from_value_C1_1:
301 | R_CRAZY R_MOVED
302 | LOOP2 value_C1_reset
303 | MOVED reset_value_C1_loop
304 |
305 | {
306 | ENTRY:
307 | value_C1_reset:
308 | // read character from stdin and store it in value_C1 and then in value
309 | IN ?- R_IN
310 | R_FLAG2
311 | MOVED crazy_value_C1
312 | return_from_value_C1_2:
313 | R_CRAZY R_MOVED
314 | R_FLAG2
315 | MOVED crazy_value
316 | return_from_value_2:
317 | R_CRAZY R_MOVED
318 | // increment input character by one to get range 0..256 (because EOF = C2).
319 | R_SUBROUTINE_FLAG1
320 | MOVED increment_value
321 | }
322 | return_from_increment_value_1:
323 | // test for overflow (overflow: C2->C0, that means EOF was read)
324 | NO_MORE_CARRY_FLAG no_increment_overflow
325 | // if EOF is read: print digital sum
326 | MOVED print_result
327 |
328 | {
329 | decrement_compare_loop:
330 | R_MOVED
331 | no_increment_overflow:
332 | // decrement value until an overflow is reached
333 | // by using LOOPs from the .CODE-section,
334 | // we can branch dependent on the number of decrements done so far
335 | R_SUBROUTINE_FLAG1
336 | MOVED decrement_value
337 | return_from_decrement_value_1:
338 | NO_MORE_CARRY_FLAG no_decrement_overflow
339 | MOVED decrement_overflow_detected
340 | }
341 |
342 | no_decrement_overflow:
343 | // if we have reached the number of decrements for ASCII character '1' before,
344 | // every further decrement cycle tells us that the number we read is one greater than we thought
345 | READ_NONZERO_DIGIT_FLAG increment_digit R_READ_NONZERO_DIGIT_FLAG
346 | // if '1' has not yet been reached, we jump into LOOP5_2 every 5th decrement step.
347 | LOOP5_2 outer5loop
348 | MOVED decrement_compare_loop
349 |
350 | outer5loop:
351 | // jump into inner5loop every 5th steo again.
352 | // so it will be executed every 25th decrement step
353 | LOOP5_3 inner5loop
354 | MOVED decrement_compare_loop
355 |
356 | inner5loop:
357 | // we read an ASCII character of value at least 24.
358 | // therefore we did not read EOF or newline, so we shall not output
359 | // the digital root right now. we store this by forcing the NO_EXIT_FLAG to be set.
360 | NO_EXIT_FLAG inner5loop
361 | // every second step (50 decrements executed) we go into the innerst loop
362 | LOOP2_4 innerst2loop
363 | MOVED decrement_compare_loop
364 |
365 | innerst2loop:
366 | // when we get here the first time, we read an ASCII value at least 49,
367 | // which is '1'. So we set the READ_NONZERO_DIGIT_FLAG to indicate we read such a digit.
368 | R_READ_NONZERO_DIGIT_FLAG
369 | MOVED decrement_compare_loop
370 |
371 | increment_digit:
372 | // restore R_READ_NONZERO_DIGIT_FLAG
373 | R_READ_NONZERO_DIGIT_FLAG
374 | // increment digit (stored in TMP_DIGIT) by calling TMP_DIGIT:
375 | // the next element of the MovD-cycle is set
376 | // if TMP_DIGIT was set to MovD before, this is the 9th increment - we did
377 | // not read a digit from '1' to '9', but an ASCII character that is greater than '9'.
378 | // then we abort parsing the current input character and read another one.
379 | TMP_DIGIT exitloop
380 | MOVED decrement_compare_loop
381 |
382 | exitloop:
383 | // reset READ_NONZERO_DIGIT_FLAG and leave input-parsing decrement-loop
384 | R_READ_NONZERO_DIGIT_FLAG
385 | MOVED decrement_overflow_detected
386 |
387 |
388 | // this is the exit label for the input character recognition above.
389 | decrement_overflow_detected:
390 | // at first, we restore the LOOPs, so we could use them again.
391 | restore_5_loop:
392 | R_MOVED
393 | value_reset2:
394 | LOOP5_2 restore_5_loop_done
395 | MOVED restore_5_loop
396 |
397 | restore_5_loop2:
398 | R_MOVED
399 | restore_5_loop_done:
400 | LOOP5_3 restore_5_loop2_done
401 | MOVED restore_5_loop2
402 |
403 | restore_2_loop2:
404 | R_MOVED
405 | restore_5_loop2_done:
406 | LOOP2_4 restore_2_loop2_done
407 | MOVED restore_2_loop2
408 |
409 | restore_2_loop2_done:
410 | // now we test whether we shall output the current digital root
411 | // or adapt it and read the next character
412 | NO_EXIT_FLAG process_input // adapt current digital root
413 | MOVED print_result // print out digital root and exit
414 |
415 | {
416 | process_input:
417 | // if no nonzero digit has been read, we need not process the input and can continue reading characters
418 | R_READ_NONZERO_DIGIT_FLAG
419 | READ_NONZERO_DIGIT_FLAG restore_initial_state // nothing to process
420 |
421 | // a nonzero digit has been read.
422 | // we destroy the flag, so it can be reused
423 | R_READ_NONZERO_DIGIT_FLAG
424 | // we force the EVER_READ_NONZERO_DIGIT_FLAG to be set.
425 | // this indicates that the output will be in the range 1..9 and must not be 0 any more,
426 | // because we read at least one nonzero digit.
427 | a:
428 | EVER_READ_NONZERO_DIGIT_FLAG a
429 | // now we adapt DIGIT by TMP_DIGIT.
430 | // DIGIT stores the current digital root, while TMP_DIGIT is only used to process input.
431 | // when TMP_DIGIT becomes MovD, we are done and can leave the input processing to read the next character.
432 | R_MOVED
433 | further_adapting:
434 | R_MOVED
435 | TMP_DIGIT restore_initial_state // done, read next
436 | R_DIGIT
437 | MOVED further_adapting
438 | }
439 |
440 | print_result:
441 | // set value to '0'.
442 | // therefore, reset it to C1 first.
443 | ROT C1 R_ROT
444 | just_another_reset_value_loop:
445 | R_MOVED
446 | R_FLAG3
447 | MOVED crazy_value
448 |
449 | return_from_value_3:
450 | R_CRAZY R_MOVED
451 | LOOP2 continue_printing
452 | MOVED just_another_reset_value_loop
453 |
454 | continue_printing:
455 | // now set value to '0' by crazy operator
456 | ROT ('0' ! C1) << 1 R_ROT
457 | R_FLAG4
458 | MOVED crazy_value
459 |
460 | {
461 | return_from_value_4:
462 | R_CRAZY
463 | // now we may have to increment value until DIGIT tells us to abort
464 | // therefore we test whether we should output '0' or not
465 | R_EVER_READ_NONZERO_DIGIT_FLAG
466 | EVER_READ_NONZERO_DIGIT_FLAG print_out_value // output '0'
467 |
468 | // we must not output '0', so we increment value in a do-while loop
469 | // until DIGIT becomes MovD.
470 | // the current representation of values by DIGIT is as follows:
471 | // DIGIT == MovD/Nop/Nop/Nop/Nop/Nop/Nop/Nop/Nop => print '1'
472 | // DIGIT == Nop/MovD/Nop/Nop/Nop/Nop/Nop/Nop/Nop => print '2'
473 | // DIGIT == Nop/Nop/MovD/Nop/Nop/Nop/Nop/Nop/Nop => print '3'
474 | // DIGIT == Nop/Nop/Nop/MovD/Nop/Nop/Nop/Nop/Nop => print '4'
475 | // DIGIT == Nop/Nop/Nop/Nop/MovD/Nop/Nop/Nop/Nop => print '5'
476 | // DIGIT == Nop/Nop/Nop/Nop/Nop/MovD/Nop/Nop/Nop => print '6'
477 | // DIGIT == Nop/Nop/Nop/Nop/Nop/Nop/MovD/Nop/Nop => print '7'
478 | // DIGIT == Nop/Nop/Nop/Nop/Nop/Nop/Nop/MovD/Nop => print '8'
479 | // DIGIT == Nop/Nop/Nop/Nop/Nop/Nop/Nop/Nop/MovD => print '9'
480 | increment_digit_loop:
481 | R_MOVED
482 | R_ROT R_ROT
483 | R_SUBROUTINE_FLAG2
484 | MOVED increment_value
485 | }
486 | return_from_increment_value_2:
487 | // test whether we can leave the increment loop
488 | DIGIT print_out_value_no_movd_restore // leave
489 | // one more iteration
490 | MOVED increment_digit_loop
491 |
492 |
493 | print_out_value:
494 | R_MOVED
495 | print_out_value_no_movd_restore:
496 | // load value and print it.
497 | // load it by crazying C2 into it twice.
498 | ROT C2 R_ROT
499 | R_FLAG5
500 | MOVED crazy_value
501 |
502 | return_from_value_5:
503 | R_CRAZY
504 | LOOP2 print_out_now
505 | R_MOVED
506 | MOVED print_out_value
507 |
508 | print_out_now:
509 | // print value
510 | OUT ?- R_OUT
511 | // print line break
512 | ROT '\n' << 1
513 | OUT ?-
514 | HALT
515 |
516 |
517 |
518 | // now the subroutines for increment and decrement of value follow
519 |
520 | increment_value:
521 | R_MOVED
522 | // kill NO_MORE_CARRY_FLAG
523 | NO_MORE_CARRY_FLAG nomorecarrykilled R_NO_MORE_CARRY_FLAG
524 | nomorecarrykilled:
525 | // We have to set tmp to C0. It won't contain any trit that is 2, so we can just crazy C1 into tmp.
526 | set_tmp_to_C0:
527 | ROT C1 R_ROT
528 | R_FLAG1 // set return flag
529 | MOVED crazy_tmp // crazy C1 into tmp variable to set it to C0
530 |
531 | return_from_tmp_1:
532 | R_CRAZY
533 | ROT C2 R_ROT // load C2 to A register
534 | // now we have to execute the command sequence
535 | // OPR value
536 | // OPR tmp
537 | // OPR carry
538 | // twice.
539 | crazy_loop_increment_value:
540 | //the following sequence must be executed twice; label for loop
541 | R_MOVED
542 | R_FLAG6 // set return flag
543 | MOVED crazy_value // crazy A register into tmp variable
544 |
545 | return_from_value_6:
546 | R_MOVED R_CRAZY
547 | R_FLAG2 // set return flag
548 | MOVED crazy_tmp // crazy into tmp variable
549 |
550 | return_from_tmp_2:
551 | R_CRAZY R_MOVED
552 | R_FLAG1 // set return flag
553 | MOVED crazy_carry // crazy into carry variable
554 |
555 | return_from_carry_1:
556 | R_CRAZY R_MOVED
557 | // check loop condition
558 | LOOP2 leave_crazy_loop_increment_value
559 | MOVED crazy_loop_increment_value
560 |
561 | leave_crazy_loop_increment_value:
562 | R_FLAG1
563 | MOVED exec_carry
564 |
565 | return_from_carry_was_not_set_1:
566 | R_NO_MORE_CARRY_FLAG
567 | return_from_carry_was_set_1:
568 | R_MOVED
569 | // rotate string_ptr until it has been rotated 10 times.
570 | // after rotating go to increment or rotate again corresponding to the NO_MORE_CARRY_FLAG
571 | R_FLAG7 // set return flag
572 | MOVED rot_value // rot string_ptr
573 |
574 | return_from_value_7:
575 | R_MOVED R_ROT
576 | // exit loop after 5 times
577 | LOOP5 exit_inner_increment_loop
578 | NO_MORE_CARRY_FLAG restore_no_more_carry_flag_and_rotate_value R_NO_MORE_CARRY_FLAG
579 | MOVED increment_value
580 |
581 | exit_inner_increment_loop:
582 | // exit loop after 2 times
583 | LOOP2_2 exit_increment_loop
584 | NO_MORE_CARRY_FLAG restore_no_more_carry_flag_and_rotate_value R_NO_MORE_CARRY_FLAG
585 | MOVED increment_value
586 |
587 | restore_no_more_carry_flag_and_rotate_value:
588 | R_NO_MORE_CARRY_FLAG
589 | MOVED return_from_carry_was_set_1
590 |
591 |
592 | exit_increment_loop:
593 | SUBROUTINE_FLAG1 return_from_increment_value_1 R_SUBROUTINE_FLAG1
594 | SUBROUTINE_FLAG2 return_from_increment_value_2
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 | decrement_value:
603 | // kill NO_MORE_CARRY_FLAG
604 | NO_MORE_CARRY_FLAG nomorecarrykilled_2 R_NO_MORE_CARRY_FLAG
605 | nomorecarrykilled_2:
606 | // We have to set tmp to C0. It won't contain any trit that is 2, so we can just crazy C1 into tmp.
607 | R_MOVED
608 | ROT C1 R_ROT
609 | R_FLAG3 // set return flag
610 | MOVED crazy_tmp // crazy C1 into tmp variable to set it to C0
611 |
612 | return_from_tmp_3:
613 | R_CRAZY
614 | value_load_loop:
615 | ROT C2 R_ROT
616 | jmp_into_loop_from_behind:
617 | R_MOVED
618 | R_FLAG8
619 | MOVED crazy_value
620 |
621 | return_from_value_8:
622 | R_CRAZY R_MOVED
623 | LOOP2 value_loaded
624 | MOVED value_load_loop
625 |
626 | value_loaded:
627 | LOOP2_2 jump_back_to_behind
628 | R_FLAG4 // set return flag
629 | MOVED crazy_tmp
630 |
631 | return_from_tmp_4:
632 | R_CRAZY R_MOVED
633 | R_FLAG2 // set return flag
634 | MOVED crazy_carry
635 |
636 | return_from_carry_2:
637 | R_CRAZY R_MOVED
638 | MOVED jmp_into_loop_from_behind
639 |
640 | jump_back_to_behind:
641 | R_FLAG2
642 | MOVED exec_carry
643 |
644 | return_from_carry_was_not_set_2:
645 | R_NO_MORE_CARRY_FLAG
646 | return_from_carry_was_set_2:
647 | R_MOVED
648 | // rotate string_ptr until it has been rotated 10 times.
649 | // after rotating go to increment or rotate again corresponding to the NO_MORE_CARRY_FLAG
650 | R_FLAG9 // set return flag
651 | MOVED rot_value // rot string_ptr
652 |
653 | return_from_value_9:
654 | R_MOVED R_ROT
655 | // exit loop after 5 times
656 | LOOP5 exit_inner_decrement_loop
657 | NO_MORE_CARRY_FLAG restore_no_more_carry_flag_and_rotate_value_2 R_NO_MORE_CARRY_FLAG
658 | MOVED decrement_value
659 |
660 | exit_inner_decrement_loop:
661 | // exit loop after 2 times
662 | LOOP2_3 exit_decrement_loop
663 | NO_MORE_CARRY_FLAG restore_no_more_carry_flag_and_rotate_value_2 R_NO_MORE_CARRY_FLAG
664 | MOVED decrement_value
665 |
666 | restore_no_more_carry_flag_and_rotate_value_2:
667 | R_NO_MORE_CARRY_FLAG
668 | MOVED return_from_carry_was_set_2
669 |
670 | exit_decrement_loop:
671 | SUBROUTINE_FLAG1 return_from_decrement_value_1
672 |
673 |
--------------------------------------------------------------------------------
/example_adder.hell:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of LMAO (Low-level Malbolge Assembler, Ooh!), an
3 | * assembler for Malbolge.
4 | * Copyright (C) 2013-2017 Matthias Lutter
5 | *
6 | * LMAO is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * LMAO is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with this program. If not, see .
18 | *
19 | * E-Mail: matthias@lutter.cc
20 | *
21 | *
22 | * Example in HeLL: Adding two 3-digit numbers.
23 | *
24 | * This HeLL program reads two numbers of up to 3 digits from stdin
25 | * and prints out their sum.
26 | * Transforming decimal numbers into ternary and back would require to
27 | * implement multiplication and modulo operations, which would
28 | * slow down the program and probably require more initialization code
29 | * than the allowed 59049 bytes.
30 | * Thus, this program sums the numbers in their decimal representation.
31 | * Therefore, xlat2-cycles (Malbolge instruction cycles) of size 2 and 5
32 | * are used to represent each of the three digits.
33 | * Summing is done by calling the R_ prefixed instruction of the first
34 | * number until the cycle of the second number turns from Nop to MovD.
35 | * The resulting cycles represent the sum of the two digits.
36 | * They are used as conditions to leave a loop in which the ASCII
37 | * character '9' is decremented. In this way, the ASCII representation
38 | * to be printed out is generated finally.
39 | *
40 | * Unfortunately, this code has not been revised before publication.
41 | * Thus, it may be chaotic and hard to understand.
42 | * Development of this program has been started with the digital root
43 | * program. Some comments may not be adapted properly.
44 | */
45 |
46 |
47 | .CODE
48 |
49 | // standard malbolge commands
50 |
51 | // used with U_-prefix to skip some code
52 | NOP:
53 | Jmp
54 |
55 | MOVED:
56 | MovD/Nop
57 | Jmp
58 |
59 | CRAZY:
60 | Opr/Nop
61 | Jmp
62 |
63 | ROT:
64 | Rot/Nop
65 | Jmp
66 |
67 | HALT:
68 | Hlt
69 |
70 | OUT:
71 | Out/Nop
72 | Jmp
73 |
74 | IN:
75 | In/Nop
76 | Jmp
77 |
78 | // used in increment and decrement subroutines for carry-detection
79 | @C21 CARRY:
80 | RNop
81 | RNop
82 | Jmp
83 |
84 |
85 | // flags follow
86 |
87 |
88 | // classic return-flags that are used to return to the position where
89 | // a subroutine (or variable access) has been called
90 |
91 | SUBROUTINE_FLAG2:
92 | Nop/MovD
93 | Jmp
94 |
95 | SUBROUTINE_FLAG3:
96 | Nop/MovD
97 | Jmp
98 |
99 | SUBROUTINE_FLAG1:
100 | FLAG1:
101 | Nop/MovD
102 | Jmp
103 |
104 | SUBROUTINE_FLAG6:
105 | FLAG2:
106 | Nop/MovD
107 | Jmp
108 |
109 | SUBROUTINE_FLAG7:
110 | FLAG3:
111 | Nop/MovD
112 | Jmp
113 |
114 | SUBROUTINE_FLAG4:
115 | FLAG4:
116 | Nop/MovD
117 | Jmp
118 |
119 | SUBROUTINE_FLAG5:
120 | FLAG5:
121 | Nop/MovD
122 | Jmp
123 |
124 | SUBROUTINE_FLAG8:
125 | FLAG8:
126 | Nop/MovD
127 | Jmp
128 |
129 | SUBROUTINE_FLAG9:
130 | FLAG9:
131 | Nop/MovD
132 | Jmp
133 |
134 |
135 | // flags that act as loop counter
136 |
137 | LOOP2:
138 | Nop/MovD
139 | Jmp
140 |
141 | LOOP2_2:
142 | Nop/MovD
143 | Jmp
144 |
145 | LOOP2_3:
146 | Nop/MovD
147 | Jmp
148 |
149 | LOOP2_4:
150 | Nop/MovD
151 | Jmp
152 |
153 | LOOP5:
154 | Nop/Nop/Nop/Nop/MovD
155 | Jmp
156 |
157 | // read input: count down until '0'
158 | LOOP4_1:
159 | Nop/Nop/Nop/MovD
160 | Jmp
161 |
162 | LOOP4_2:
163 | Nop/Nop/Nop/MovD
164 | Jmp
165 |
166 | LOOP2_special:
167 | Nop/MovD
168 | Jmp
169 |
170 |
171 | // more sophisticated flags follow below
172 |
173 | // flag used inside increment and decrement subroutines.
174 | // if this is set when the routine returns,
175 | // it indicates an overflow (add: C2->C0; sub: C0->C2).
176 | NO_MORE_CARRY_FLAG:
177 | Nop/MovD
178 | Jmp
179 |
180 | // this flag is set to indicate that the user has typed at least
181 | // one valid digit.
182 | READ_DIGIT_FLAG:
183 | Nop/MovD
184 | Jmp
185 |
186 | // used during input precessing to indicate that a character is in the
187 | // ASCII range of digits ('0' until '9').
188 | // if set, the character should be written into the xlat2-cycles.
189 | // otherwise, the character should be ignored or treated as separator.
190 | NUMBER_READ_FLAG:
191 | Nop/MovD
192 | Jmp
193 |
194 | // indicates whether the first or the second number is being read
195 | // from stdin at the moment
196 | WAITING_FOR_FIRST_NUMBER_FLAG:
197 | MovD/Nop
198 | Jmp
199 |
200 | // loop through digits of output (3 digits; possible leading 1 from
201 | // overflow is handled separately)
202 | PRINT_ALL_DIGITS_LOOP:
203 | Nop/Nop/MovD/Nop // print 3 times
204 | Jmp
205 |
206 | // indicated whether the program is in "print out mode"
207 | IS_PRINTING_FLAG:
208 | Nop/MovD
209 | Jmp
210 |
211 | // leading zeroes are ignored. if any digit has been printed, this flag
212 | // is set to indicate that zeroes are now printed as well.
213 | ANY_DIGIT_HAS_BEEN_PRINTED_FLAG:
214 | MovD/Nop
215 | Jmp
216 |
217 | // if no digit has been printed at all, the last digit is a the leading
218 | // digit as well. however, in this case it should be printed out, even
219 | // if it is zero.
220 | // this flag is used to indicate that case.
221 | PRINT_ZERO_FLAG:
222 | MovD/Nop/Nop/Nop/Nop/Nop/Nop/Nop/Nop
223 | Jmp
224 |
225 |
226 | /*** decimal digits/numbers represented by xlat2-cycles follow ***/
227 |
228 | // store three digits (DIGIT0..DIGIT2) of NUMBER1 (N1) by
229 | // two xlat2-cycles each
230 |
231 | N1_DIGIT0_HIGH:
232 | Nop/Nop/Nop/Nop/MovD
233 | Jmp
234 |
235 | N1_DIGIT0_LOW:
236 | Nop/MovD
237 | Jmp
238 |
239 | N1_DIGIT1_HIGH:
240 | Nop/Nop/Nop/Nop/MovD
241 | Jmp
242 |
243 | N1_DIGIT1_LOW:
244 | Nop/MovD
245 | Jmp
246 |
247 | N1_DIGIT2_HIGH:
248 | Nop/Nop/Nop/Nop/MovD
249 | Jmp
250 |
251 | N1_DIGIT2_LOW:
252 | Nop/MovD
253 | Jmp
254 |
255 |
256 | // store three digits (DIGIT0..DIGIT2) of NUMBER2 (N2) by
257 | // two xlat2-cycles each
258 |
259 | N2_DIGIT0_HIGH:
260 | Nop/Nop/Nop/Nop/MovD
261 | Jmp
262 |
263 | N2_DIGIT0_LOW:
264 | Nop/MovD
265 | Jmp
266 |
267 | N2_DIGIT1_HIGH:
268 | Nop/Nop/Nop/Nop/MovD
269 | Jmp
270 |
271 | N2_DIGIT1_LOW:
272 | Nop/MovD
273 | Jmp
274 |
275 | N2_DIGIT2_HIGH:
276 | Nop/Nop/Nop/Nop/MovD
277 | Jmp
278 |
279 | N2_DIGIT2_LOW:
280 | Nop/MovD
281 | Jmp
282 |
283 |
284 | // store temporary "tmp" digit in these two xlat2 cycles
285 |
286 | TMP_DIGIT_HIGH:
287 | Nop/Nop/Nop/Nop/MovD
288 | Jmp
289 |
290 | TMP_DIGIT_LOW:
291 | Nop/MovD
292 | Jmp
293 |
294 |
295 | // store temporary "tmp2" digit in these two xlat2 cycles
296 | // it is used for cloning/shifting while TMP is occupied
297 |
298 | TMP2_DIGIT_HIGH:
299 | Nop/Nop/Nop/Nop/MovD
300 | Jmp
301 |
302 | TMP2_DIGIT_LOW:
303 | Nop/MovD
304 | Jmp
305 |
306 |
307 | // stores an overflow occured during adding of the two numbers.
308 | // if this flag is set, the result is between 1000 and 1998, thus
309 | // a leading '1' is forced to be printed out.
310 | // afterwards, the last 3 digits of the number are printed out normally
311 | // (with ANY_DIGIT_HAS_BEEN_PRINTED_FLAG set).
312 |
313 | OVERFLOW:
314 | MovD/Nop
315 | Jmp
316 |
317 |
318 |
319 | .DATA
320 | /// data section, containing the program logic, but not the numbers,
321 | /// which are stored in the CODE section.
322 | /// thus, the semantic purpose of the CODE and DATA section are confused
323 | /// totally.
324 | /// however, the sections indicate the kind of Malbolge cells, not their
325 | /// semantic properties.
326 |
327 |
328 | // var carry
329 | // carry: this variable stores whether there is an overflow or not
330 | // (used by subroutines increment and decrement).
331 | // CARRY from .CODE-section will be used to branch dependent on variable value,
332 | // which is either CARRY or CARRY+1
333 | crazy_carry:
334 | U_CRAZY carry
335 | exec_carry:
336 | R_MOVED
337 | carry:
338 | CARRY
339 | U_NOP execution_not_jumped_to_carry
340 | U_NOP carry_was_not_set U_NOP carry_was_set
341 | carry_was_set:
342 | MOVED return_carry_was_set
343 | carry_was_not_set:
344 | MOVED return_carry_was_not_set
345 | execution_not_jumped_to_carry:
346 | FLAG2 return_from_carry_2 R_FLAG2
347 | return_carry_was_set:
348 | FLAG2 return_from_carry_was_set_2 R_FLAG2
349 | return_carry_was_not_set:
350 | FLAG2 return_from_carry_was_not_set_2
351 |
352 |
353 |
354 | restore_initial_state:
355 | R_MOVED //destroy moved (for reset_value_loop)
356 | restore_initial_state_and_moved:
357 | READ_DIGIT_FLAG restore_initial_state_and_moved
358 | R_READ_DIGIT_FLAG
359 | // write C1 into value and value_C1.
360 | ROT C1 R_ROT
361 | reset_value_loop:
362 | R_MOVED
363 | crazy_value:
364 | U_CRAZY value
365 | rot_value:
366 | U_ROT value
367 | value:
368 | C1
369 | FLAG2 return_from_value_2 R_FLAG2
370 | FLAG3 return_from_value_3 R_FLAG3
371 | FLAG4 return_from_value_4 R_FLAG4
372 | FLAG5 return_from_value_5 R_FLAG5
373 | FLAG8 return_from_value_8 R_FLAG8
374 | FLAG9 return_from_value_9 R_FLAG9
375 | return_from_value_1:
376 | R_CRAZY
377 | LOOP2 value_reset
378 | MOVED reset_value_loop
379 |
380 | value_reset:
381 | R_MOVED
382 | reset_value_C1_loop:
383 | R_MOVED
384 | crazy_value_C1:
385 | U_CRAZY value_C1
386 | rot_value_C1:
387 | U_ROT value_C1
388 | value_C1:
389 | C1
390 | FLAG2 return_from_value_C1_2 R_FLAG2
391 | return_from_value_C1_1:
392 | R_CRAZY
393 | LOOP2 value_C1_reset
394 | MOVED reset_value_C1_loop
395 |
396 |
397 | restore_tmp_digit_high:
398 | R_MOVED
399 | value_C1_reset:
400 | TMP_DIGIT_HIGH restore_tmp_digit_high_done
401 | MOVED restore_tmp_digit_high
402 |
403 | restore_tmp_digit_low:
404 | R_MOVED
405 | restore_tmp_digit_high_done:
406 | TMP_DIGIT_LOW restore_tmp_digit_low_done
407 | MOVED restore_tmp_digit_low
408 |
409 | restore_tmp_digit_low_done:
410 | ENTRY:
411 | // read character from stdin and store it in value_C1 and then in value
412 | IN ?- R_IN
413 | R_FLAG2
414 | MOVED crazy_value_C1
415 |
416 | return_from_value_C1_2:
417 | R_CRAZY R_MOVED
418 | R_FLAG2
419 | MOVED crazy_value
420 |
421 | return_from_value_2:
422 | R_CRAZY R_MOVED
423 | // decrement to determine actual value
424 | R_SUBROUTINE_FLAG1
425 | MOVED decrement_value
426 |
427 |
428 | {
429 | decrement_compare_loop:
430 | R_MOVED
431 | // decrement value until an overflow is reached
432 | // by using LOOPs from the .CODE-section,
433 | // we can branch dependent on the number of decrements done so far
434 | R_SUBROUTINE_FLAG1
435 | MOVED decrement_value
436 | return_from_decrement_value_1:
437 | NO_MORE_CARRY_FLAG no_decrement_overflow
438 | MOVED decrement_overflow_detected
439 | }
440 |
441 | no_decrement_overflow:
442 | ; NO_EXIT_FLAG no_decrement_overflow
443 | ; R_NO_EXIT_FLAG
444 | // if we have reached the number of decrements for ASCII character '1' before,
445 | // every further decrement cycle tells us that the number we read is one greater than we thought
446 | READ_DIGIT_FLAG increment_digit R_READ_DIGIT_FLAG
447 | // if '1' has not yet been reached, we jump into LOOP5_2 every 5th decrement step.
448 | LOOP4_1 outer4loop
449 | MOVED decrement_compare_loop
450 |
451 | outer4loop:
452 | // jump into inner5loop every 5th steo again.
453 | // so it will be executed every 25th decrement step
454 | LOOP4_2 middle4loop
455 | MOVED decrement_compare_loop
456 |
457 | middle4loop:
458 | // jump into inner5loop every 5th steo again.
459 | // so it will be executed every 25th decrement step
460 | LOOP2_special inner2loop
461 | MOVED decrement_compare_loop
462 |
463 | inner2loop:
464 | ; NO_EXIT_FLAG inner2loop // at least a space has been read (?)
465 | R_LOOP2_special // only one more time!
466 | // we read an ASCII character of value at least 24.
467 | // therefore we did not read EOF or newline, so we shall not output
468 | // the digital root right now. we store this by forcing the NO_EXIT_FLAG to be set.
469 | // every second step (50 decrements executed) we go into the innerst loop
470 | LOOP2_4 innerst2loop
471 | MOVED decrement_compare_loop
472 |
473 | innerst2loop:
474 | // when we get here the first time, we read an ASCII value at least 49,
475 | // which is '1'. So we set the READ_DIGIT_FLAG to indicate we read such a digit.
476 | R_READ_DIGIT_FLAG
477 | MOVED decrement_compare_loop
478 |
479 | increment_digit:
480 | ; NO_EXIT_FLAG increment_digit
481 | // restore R_READ_DIGIT_FLAG
482 | R_READ_DIGIT_FLAG
483 | // increment digit (stored in TMP_DIGIT) by calling TMP_DIGIT:
484 | // the next element of the MovD-cycle is set
485 | // if TMP_DIGIT was set to MovD before, this is the 9th increment - we did
486 | // not read a digit from '1' to '9', but an ASCII character that is greater than '9'.
487 | // then we abort parsing the current input character and read another one.
488 | TMP_DIGIT_LOW tmp_digit_overflow_low
489 | MOVED decrement_compare_loop
490 |
491 | tmp_digit_overflow_low:
492 | TMP_DIGIT_HIGH exitloop
493 | MOVED decrement_compare_loop
494 |
495 | exitloop:
496 | ; NO_EXIT_FLAG exitloop
497 | ; R_NO_EXIT_FLAG
498 | // reset READ_DIGIT_FLAG and leave input-parsing decrement-loop
499 | R_READ_DIGIT_FLAG
500 | MOVED decrement_overflow_detected
501 |
502 |
503 | // this is the exit label for the input character recognition above.
504 | decrement_overflow_detected:
505 | // at first, we restore the LOOPs, so we could use them again.
506 | restore_2_loop2:
507 | R_MOVED
508 | restore_5_loop2_done:
509 | LOOP2_4 restore_2_loop2_done
510 | MOVED restore_2_loop2
511 |
512 | restore_loop4:
513 | R_MOVED
514 | restore_2_loop2_done:
515 | LOOP4_1 restore_loop4_done
516 | MOVED restore_loop4
517 |
518 | restore_2_loop4:
519 | R_MOVED
520 | restore_loop4_done:
521 | LOOP4_2 restore_2_loop4_done
522 | MOVED restore_2_loop4
523 |
524 | restore_special_loop2:
525 | R_MOVED
526 | restore_2_loop4_done:
527 | LOOP2_special restore_special_loop2_done
528 | MOVED restore_special_loop2
529 |
530 |
531 | restore_special_loop2_done:
532 | // now we test whether we shall output the current digital root
533 | // or adapt it and read the next character
534 | ; NO_EXIT_FLAG process_input // adapt current digital root
535 | READ_DIGIT_FLAG process_input_digit
536 | MOVED process_input_nondigit // print out digital root and exit
537 |
538 | process_input_nondigit:
539 | R_MOVED
540 | NUMBER_READ_FLAG process_input_number
541 | NUMBER_READ_FLAG restore_initial_state
542 |
543 | process_input_number:
544 | WAITING_FOR_FIRST_NUMBER_FLAG read_first_number
545 | WAITING_FOR_FIRST_NUMBER_FLAG read_second_number
546 |
547 | read_first_number:
548 | R_NUMBER_READ_FLAG
549 | NUMBER_READ_FLAG restore_initial_state
550 |
551 | read_second_number:
552 | // ADD NUMBERS AND PRINT RESULT
553 | R_SUBROUTINE_FLAG3
554 | MOVED clear_tmp2
555 |
556 |
557 | process_input_digit:
558 | NUMBER_READ_FLAG process_input_digit // a number has been read
559 | // shift tmp_digit into number (n1 or n2)
560 | WAITING_FOR_FIRST_NUMBER_FLAG append_tmp_digit_to_first_number // and restore WAITING_FOR_FIRST_NUMBER_FLAG
561 | WAITING_FOR_FIRST_NUMBER_FLAG append_tmp_digit_to_second_number
562 |
563 | // shift digit...
564 | append_tmp_digit_to_first_number:
565 | R_WAITING_FOR_FIRST_NUMBER_FLAG
566 | MOVED clear_n1_msd
567 |
568 | append_tmp_digit_to_second_number:
569 | MOVED clear_n2_msd
570 |
571 |
572 | return_clear_tmp2_3:
573 | ;R_N2_DIGIT0_LOW
574 | N2_DIGIT0_LOW add_n2_digit0_to_n1_digit0_5
575 | MOVED add_n2_digit0_to_n1_digit0_2
576 |
577 | add_n2_digit0_to_n1_digit0_5:
578 | N1_DIGIT0_LOW add_n2_digit0_to_n1_digit0_overflow_low
579 | MOVED add_n2_digit0_to_n1_digit0_2
580 |
581 | add_n2_digit0_to_n1_digit0_overflow_low:
582 | R_OVERFLOW
583 | MOVED add_n2_digit0_to_n1_digit0_2
584 |
585 |
586 |
587 | add_n2_digit0_to_n1_digit0_2:
588 | R_MOVED
589 | // now the digits have to be added...
590 | N2_DIGIT0_HIGH add_n2_digit0_to_n1_digit0_1
591 | R_TMP2_DIGIT_HIGH
592 | MOVED add_n2_digit0_to_n1_digit0_2
593 |
594 |
595 |
596 | add_n2_digit0_to_n1_digit0_1:
597 | OVERFLOW add_n2_digit0_to_n1_digit0_6
598 | N1_DIGIT0_HIGH add_n2_digit0_to_n1_digit0_6
599 | R_OVERFLOW
600 | add_n2_digit0_to_n1_digit0_6:
601 | R_OVERFLOW
602 | R_MOVED
603 | add_n2_digit0_to_n1_digit0_4:
604 | R_MOVED
605 | TMP2_DIGIT_HIGH add_n2_digit1_to_n1_digit1 // NEXT DIGIT
606 | N1_DIGIT0_HIGH add_n2_digit0_to_n1_digit0_6
607 | MOVED add_n2_digit0_to_n1_digit0_4
608 |
609 |
610 | add_n2_digit1_to_n1_digit1:
611 | R_SUBROUTINE_FLAG6
612 | MOVED clear_tmp2
613 |
614 |
615 | return_clear_tmp2_6:
616 | add_n2_digit1_to_n1_digit1_3:
617 | OVERFLOW add_n2_digit1_to_n1_digit1_overflow_low2
618 | N1_DIGIT1_LOW add_n2_digit1_to_n1_digit1_overflow_low2
619 | R_OVERFLOW
620 | add_n2_digit1_to_n1_digit1_overflow_low2:
621 | R_OVERFLOW
622 | N2_DIGIT1_LOW add_n2_digit1_to_n1_digit1_5
623 | MOVED add_n2_digit1_to_n1_digit1_2
624 |
625 | add_n2_digit1_to_n1_digit1_5:
626 | N1_DIGIT1_LOW add_n2_digit1_to_n1_digit1_overflow_low
627 | MOVED add_n2_digit1_to_n1_digit1_2
628 |
629 | add_n2_digit1_to_n1_digit1_overflow_low:
630 | R_OVERFLOW
631 | MOVED add_n2_digit1_to_n1_digit1_2
632 |
633 |
634 |
635 | add_n2_digit1_to_n1_digit1_2:
636 | R_MOVED
637 | // now the digits have to be added...
638 | N2_DIGIT1_HIGH add_n2_digit1_to_n1_digit1_1
639 | R_TMP2_DIGIT_HIGH
640 | MOVED add_n2_digit1_to_n1_digit1_2
641 |
642 |
643 |
644 | add_n2_digit1_to_n1_digit1_1:
645 | OVERFLOW add_n2_digit1_to_n1_digit1_6
646 | N1_DIGIT1_HIGH add_n2_digit1_to_n1_digit1_6
647 | R_OVERFLOW
648 | add_n2_digit1_to_n1_digit1_6:
649 | R_OVERFLOW
650 | R_MOVED
651 | add_n2_digit1_to_n1_digit1_4:
652 | R_MOVED
653 | TMP2_DIGIT_HIGH add_n2_digit2_to_n1_digit2 ;NEXT DIGIT
654 | N1_DIGIT1_HIGH add_n2_digit1_to_n1_digit1_6
655 | MOVED add_n2_digit1_to_n1_digit1_4
656 |
657 |
658 |
659 | add_n2_digit2_to_n1_digit2:
660 | R_SUBROUTINE_FLAG9
661 | MOVED clear_tmp2
662 |
663 |
664 | return_clear_tmp2_9:
665 | add_n2_digit2_to_n1_digit2_3:
666 | OVERFLOW add_n2_digit2_to_n1_digit2_overflow_low2
667 | N1_DIGIT2_LOW add_n2_digit2_to_n1_digit2_overflow_low2
668 | R_OVERFLOW
669 | add_n2_digit2_to_n1_digit2_overflow_low2:
670 | R_OVERFLOW
671 | N2_DIGIT2_LOW add_n2_digit2_to_n1_digit2_5
672 | MOVED add_n2_digit2_to_n1_digit2_2
673 |
674 | add_n2_digit2_to_n1_digit2_5:
675 | N1_DIGIT2_LOW add_n2_digit2_to_n1_digit2_overflow_low
676 | MOVED add_n2_digit2_to_n1_digit2_2
677 |
678 | add_n2_digit2_to_n1_digit2_overflow_low:
679 | R_OVERFLOW
680 | MOVED add_n2_digit2_to_n1_digit2_2
681 |
682 |
683 |
684 | add_n2_digit2_to_n1_digit2_2:
685 | R_MOVED
686 | // now the digits have to be added...
687 | N2_DIGIT2_HIGH add_n2_digit2_to_n1_digit2_1
688 | R_TMP2_DIGIT_HIGH
689 | MOVED add_n2_digit2_to_n1_digit2_2
690 |
691 |
692 |
693 | add_n2_digit2_to_n1_digit2_1:
694 | OVERFLOW add_n2_digit2_to_n1_digit2_6
695 | N1_DIGIT2_HIGH add_n2_digit2_to_n1_digit2_6
696 | R_OVERFLOW
697 | add_n2_digit2_to_n1_digit2_6:
698 | R_OVERFLOW
699 | R_MOVED
700 | add_n2_digit2_to_n1_digit2_4:
701 | R_MOVED
702 | TMP2_DIGIT_HIGH add_n2_digit2_to_n1_digit2_fin
703 | N1_DIGIT2_HIGH add_n2_digit2_to_n1_digit2_6
704 | MOVED add_n2_digit2_to_n1_digit2_4
705 |
706 |
707 |
708 |
709 |
710 |
711 | add_n2_digit2_to_n1_digit2_fin:
712 | R_OVERFLOW
713 | OVERFLOW print_leading_one
714 | MOVED print_result
715 |
716 |
717 | print_leading_one:
718 | // do not process
719 | ROT '1'<<1 R_ROT OUT ?- R_OUT
720 | R_ANY_DIGIT_HAS_BEEN_PRINTED_FLAG
721 | MOVED print_result
722 |
723 |
724 |
725 |
726 |
727 |
728 | // MSD of N1 is DIGIT2.
729 | clear_n1_msd: // clear most significant digit of n1
730 | n1_msd_high_clearloop:
731 | R_MOVED
732 | N1_DIGIT2_HIGH n1_msd_high_cleared
733 | MOVED n1_msd_high_clearloop
734 |
735 | n1_msd_high_cleared:
736 | N1_DIGIT2_LOW n1_msd_high_cleared
737 | R_N1_DIGIT2_LOW
738 | // now the digit is cleared
739 | // clear TMP2
740 | R_SUBROUTINE_FLAG1
741 | MOVED clear_tmp2
742 |
743 |
744 | clone_n1_digit1_digit_to_n1_digit2_2:
745 | R_MOVED
746 | return_clear_tmp2:
747 | // now the digits have to be shifted...
748 | clone_n1_digit1_digit_to_n1_digit2:
749 | N1_DIGIT1_HIGH clone_n1_digit1_digit_to_n1_digit2_1
750 | R_TMP2_DIGIT_HIGH
751 | MOVED clone_n1_digit1_digit_to_n1_digit2_2
752 |
753 | clone_n1_digit1_digit_to_n1_digit2_4:
754 | R_MOVED
755 | clone_n1_digit1_digit_to_n1_digit2_1:
756 | TMP2_DIGIT_HIGH clone_n1_digit1_digit_to_n1_digit2_3
757 | R_N1_DIGIT2_HIGH
758 | MOVED clone_n1_digit1_digit_to_n1_digit2_4
759 |
760 | clone_n1_digit1_digit_to_n1_digit2_3:
761 | // high has bee cloned. now clone low.
762 | R_N1_DIGIT2_LOW
763 | N1_DIGIT1_LOW clone_n1_digit1_digit_to_n1_digit2_5
764 | R_N1_DIGIT2_LOW
765 | R_N1_DIGIT1_LOW
766 | clone_n1_digit1_digit_to_n1_digit2_5:
767 | // done.
768 | R_SUBROUTINE_FLAG7
769 | MOVED clear_tmp2
770 |
771 |
772 | clone_n1_digit0_digit_to_n1_digit1_2:
773 | R_MOVED
774 | return_clear_tmp2_7:
775 | // now the digits have to be shifted...
776 | clone_n1_digit0_digit_to_n1_digit1:
777 | N1_DIGIT0_HIGH clone_n1_digit0_digit_to_n1_digit1_1
778 | R_TMP2_DIGIT_HIGH
779 | MOVED clone_n1_digit0_digit_to_n1_digit1_2
780 |
781 | clone_n1_digit0_digit_to_n1_digit1_4:
782 | R_MOVED
783 | clone_n1_digit0_digit_to_n1_digit1_1:
784 | TMP2_DIGIT_HIGH clone_n1_digit0_digit_to_n1_digit1_3
785 | R_N1_DIGIT1_HIGH
786 | MOVED clone_n1_digit0_digit_to_n1_digit1_4
787 |
788 | clone_n1_digit0_digit_to_n1_digit1_3:
789 | // high has bee cloned. now clone low.
790 | R_N1_DIGIT1_LOW
791 | N1_DIGIT0_LOW clone_n1_digit0_digit_to_n1_digit1_5
792 | R_N1_DIGIT1_LOW
793 | R_N1_DIGIT0_LOW
794 | clone_n1_digit0_digit_to_n1_digit1_5:
795 | // done.
796 | R_SUBROUTINE_FLAG4
797 | MOVED clear_tmp2
798 |
799 |
800 | clone_tmp_digit_to_n1_digit0_2:
801 | R_MOVED
802 | // now, TMP_DIGIT has to be shifted into LSD (least sognificant digit) of N1.
803 | return_clear_tmp2_4:
804 | clone_tmp_digit_to_n1_digit0:
805 | TMP_DIGIT_HIGH clone_tmp_digit_to_n1_digit0_1
806 | R_TMP2_DIGIT_HIGH
807 | MOVED clone_tmp_digit_to_n1_digit0_2
808 |
809 | clone_tmp_digit_to_n1_digit0_4:
810 | R_MOVED
811 | clone_tmp_digit_to_n1_digit0_1:
812 | TMP2_DIGIT_HIGH clone_tmp_digit_to_n1_digit0_3
813 | R_N1_DIGIT0_HIGH
814 | MOVED clone_tmp_digit_to_n1_digit0_4
815 |
816 | clone_tmp_digit_to_n1_digit0_3:
817 | // high has bee cloned. now clone low.
818 | R_N1_DIGIT0_LOW
819 | TMP_DIGIT_LOW clone_tmp_digit_to_n1_digit0_5
820 | R_N1_DIGIT0_LOW
821 | clone_tmp_digit_to_n1_digit0_5:
822 | // done.
823 | IS_PRINTING_FLAG print_next R_IS_PRINTING_FLAG
824 | MOVED restore_initial_state_and_moved
825 |
826 |
827 |
828 | // MSD of N2 is DIGIT2.
829 | clear_n2_msd: // clear most significant digit of n1
830 | n2_msd_high_clearloop:
831 | R_MOVED
832 | N2_DIGIT2_HIGH n2_msd_high_cleared
833 | MOVED n2_msd_high_clearloop
834 |
835 | n2_msd_high_cleared:
836 | N2_DIGIT2_LOW n2_msd_high_cleared
837 | R_N2_DIGIT2_LOW
838 | // now the digit is cleared
839 | // clear TMP2
840 | R_SUBROUTINE_FLAG2
841 | MOVED clear_tmp2
842 |
843 |
844 |
845 |
846 |
847 | clone_n2_digit1_digit_to_n2_digit2_2:
848 | R_MOVED
849 | return_clear_tmp2_2:
850 | // now the digits has to be shifted...
851 | clone_n2_digit1_digit_to_n2_digit2:
852 | N2_DIGIT1_HIGH clone_n2_digit1_digit_to_n2_digit2_1
853 | R_TMP2_DIGIT_HIGH
854 | MOVED clone_n2_digit1_digit_to_n2_digit2_2
855 |
856 | clone_n2_digit1_digit_to_n2_digit2_4:
857 | R_MOVED
858 | clone_n2_digit1_digit_to_n2_digit2_1:
859 | TMP2_DIGIT_HIGH clone_n2_digit1_digit_to_n2_digit2_3
860 | R_N2_DIGIT2_HIGH
861 | MOVED clone_n2_digit1_digit_to_n2_digit2_4
862 |
863 | clone_n2_digit1_digit_to_n2_digit2_3:
864 | // high has bee cloned. now clone low.
865 | R_N2_DIGIT2_LOW
866 | N2_DIGIT1_LOW clone_n2_digit1_digit_to_n2_digit2_5
867 | R_N2_DIGIT2_LOW
868 | R_N2_DIGIT1_LOW
869 | clone_n2_digit1_digit_to_n2_digit2_5:
870 | // done.
871 | R_SUBROUTINE_FLAG8
872 | MOVED clear_tmp2
873 |
874 |
875 |
876 |
877 | clone_n2_digit0_digit_to_n2_digit1_2:
878 | R_MOVED
879 | return_clear_tmp2_8:
880 | // now the digits has to be shifted...
881 | clone_n2_digit0_digit_to_n2_digit1:
882 | N2_DIGIT0_HIGH clone_n2_digit0_digit_to_n2_digit1_1
883 | R_TMP2_DIGIT_HIGH
884 | MOVED clone_n2_digit0_digit_to_n2_digit1_2
885 |
886 | clone_n2_digit0_digit_to_n2_digit1_4:
887 | R_MOVED
888 | clone_n2_digit0_digit_to_n2_digit1_1:
889 | TMP2_DIGIT_HIGH clone_n2_digit0_digit_to_n2_digit1_3
890 | R_N2_DIGIT1_HIGH
891 | MOVED clone_n2_digit0_digit_to_n2_digit1_4
892 |
893 | clone_n2_digit0_digit_to_n2_digit1_3:
894 | // high has bee cloned. now clone low.
895 | R_N2_DIGIT1_LOW
896 | N2_DIGIT0_LOW clone_n2_digit0_digit_to_n2_digit1_5
897 | R_N2_DIGIT1_LOW
898 | R_N2_DIGIT0_LOW
899 | clone_n2_digit0_digit_to_n2_digit1_5:
900 | // done.
901 | R_SUBROUTINE_FLAG5
902 | MOVED clear_tmp2
903 |
904 |
905 | clone_tmp_digit_to_n2_digit0_2:
906 | R_MOVED
907 | return_clear_tmp2_5:
908 | // now , TMP_DIGIT has to be shifted into LSD (least sognificant digit) of N1.
909 | clone_tmp_digit_to_n2_digit0:
910 | TMP_DIGIT_HIGH clone_tmp_digit_to_n2_digit0_1
911 | R_TMP2_DIGIT_HIGH
912 | MOVED clone_tmp_digit_to_n2_digit0_2
913 |
914 | clone_tmp_digit_to_n2_digit0_4:
915 | R_MOVED
916 | clone_tmp_digit_to_n2_digit0_1:
917 | TMP2_DIGIT_HIGH clone_tmp_digit_to_n2_digit0_3
918 | R_N2_DIGIT0_HIGH
919 | MOVED clone_tmp_digit_to_n2_digit0_4
920 |
921 | clone_tmp_digit_to_n2_digit0_3:
922 | // high has bee cloned. now clone low.
923 | R_N2_DIGIT0_LOW
924 | TMP_DIGIT_LOW clone_tmp_digit_to_n2_digit0_5
925 | R_N2_DIGIT0_LOW
926 | clone_tmp_digit_to_n2_digit0_5:
927 | // done.
928 | MOVED restore_initial_state_and_moved
929 |
930 |
931 |
932 |
933 |
934 | clear_tmp2:
935 | R_MOVED
936 | TMP2_DIGIT_HIGH tmp2_high_cleared
937 | MOVED clear_tmp2
938 |
939 | tmp2_high_cleared:
940 | TMP2_DIGIT_LOW tmp2_high_cleared
941 | R_TMP2_DIGIT_LOW
942 | // return
943 | SUBROUTINE_FLAG1 return_clear_tmp2 R_SUBROUTINE_FLAG1
944 | SUBROUTINE_FLAG2 return_clear_tmp2_2 R_SUBROUTINE_FLAG2
945 | SUBROUTINE_FLAG3 return_clear_tmp2_3 R_SUBROUTINE_FLAG3
946 | SUBROUTINE_FLAG4 return_clear_tmp2_4 R_SUBROUTINE_FLAG4
947 | SUBROUTINE_FLAG5 return_clear_tmp2_5 R_SUBROUTINE_FLAG5
948 | SUBROUTINE_FLAG6 return_clear_tmp2_6 R_SUBROUTINE_FLAG6
949 | SUBROUTINE_FLAG7 return_clear_tmp2_7 R_SUBROUTINE_FLAG7
950 | SUBROUTINE_FLAG8 return_clear_tmp2_8 R_SUBROUTINE_FLAG8
951 | SUBROUTINE_FLAG9 return_clear_tmp2_9
952 |
953 |
954 | ;N1_DIGIT0_HIGH (Nop/Nop/Nop/Nop/MovD)
955 | ;N1_DIGIT0_LOW (Nop/MovD)
956 | ;
957 | ;N2_DIGIT0_HIGH (Nop/Nop/Nop/Nop/MovD)
958 | ;N2_DIGIT0_LOW (Nop/MovD)
959 | ;
960 | ;TMP2_DIGIT_HIGH
961 | ;TMP2_DIGIT_LOW
962 |
963 |
964 |
965 |
966 | // start printing the MSD.
967 | // then shift number and print it again - and so on.
968 | // test whether it is zero, then don't print (except the whole number is 0).
969 | print_next:
970 | R_MOVED
971 | print_result:
972 | LOOP2 print_result
973 | R_LOOP2
974 | restore_zf_loop:
975 | PRINT_ZERO_FLAG restore_zf_done
976 | R_MOVED
977 | MOVED restore_zf_loop
978 | restore_zf_done:
979 | // set value to '9'.
980 | // therefore, reset it to C1 first.
981 | ROT C1 R_ROT
982 | just_another_reset_value_loop:
983 | R_MOVED
984 | R_FLAG3
985 | MOVED crazy_value
986 |
987 | return_from_value_3:
988 | R_CRAZY R_MOVED
989 | LOOP2 continue_printing
990 | MOVED just_another_reset_value_loop
991 |
992 | continue_printing:
993 | TMP_DIGIT_LOW continue_printing
994 | R_LOOP2
995 | // now set value to '0' by crazy operator
996 | load_9_char:
997 | ROT C2 R_ROT
998 | CRAZY '9'!C1 R_CRAZY
999 | LOOP2 load_9_char
1000 | R_FLAG4
1001 | MOVED crazy_value
1002 |
1003 | {
1004 | return_from_value_4:
1005 | R_CRAZY
1006 | // now we may have to increment value until DIGIT tells us to abort
1007 | // therefore we test whether we should output '0' or not
1008 |
1009 | // we must not output '0', we decrement value in a do-while loop
1010 | // until DIGIT becomes MovD.
1011 | decrement_digit_loop:
1012 | R_MOVED
1013 | N1_DIGIT2_HIGH leave_high_decrement_loop
1014 | decrement_two_times:
1015 | R_SUBROUTINE_FLAG2
1016 | R_PRINT_ZERO_FLAG
1017 | MOVED decrement_value
1018 | }
1019 |
1020 | return_from_decrement_value_2:
1021 | // test whether we can leave the increment loop
1022 | TMP_DIGIT_LOW decrement_two_times
1023 | MOVED decrement_digit_loop
1024 |
1025 |
1026 | leave_high_decrement_loop:
1027 | ;TMP_DIGIT_LOW leave_high_decrement_loop
1028 | R_TMP_DIGIT_LOW
1029 | N1_DIGIT2_LOW print_out_value_no_movd_restore
1030 | PRINT_ZERO_FLAG maybe_no_print
1031 | do_print:
1032 | R_SUBROUTINE_FLAG3
1033 | MOVED decrement_value
1034 |
1035 | maybe_no_print:
1036 | ANY_DIGIT_HAS_BEEN_PRINTED_FLAG really_no_print
1037 | ANY_DIGIT_HAS_BEEN_PRINTED_FLAG do_print
1038 |
1039 | really_no_print:
1040 | R_ANY_DIGIT_HAS_BEEN_PRINTED_FLAG
1041 | MOVED continue_printing_next
1042 |
1043 | print_out_value:
1044 | R_MOVED
1045 | return_from_decrement_value_3:
1046 | print_out_value_no_movd_restore:
1047 | // load value and print it.
1048 | // load it by crazying C2 into it twice.
1049 | ROT C2 R_ROT
1050 | R_FLAG5
1051 | MOVED crazy_value
1052 |
1053 | return_from_value_5:
1054 | R_CRAZY
1055 | TMP_DIGIT_LOW print_out_now
1056 | R_MOVED
1057 | MOVED print_out_value
1058 |
1059 | print_out_now:
1060 | // print value
1061 | OUT ?- R_OUT
1062 | print_set_flag:
1063 | ANY_DIGIT_HAS_BEEN_PRINTED_FLAG print_set_flag
1064 | R_ANY_DIGIT_HAS_BEEN_PRINTED_FLAG
1065 | continue_printing_next:
1066 | R_MOVED
1067 | PRINT_ALL_DIGITS_LOOP print_done
1068 | R_IS_PRINTING_FLAG
1069 | MOVED clear_n1_msd
1070 | // print line break
1071 |
1072 | print_done:
1073 | R_ANY_DIGIT_HAS_BEEN_PRINTED_FLAG
1074 | ANY_DIGIT_HAS_BEEN_PRINTED_FLAG print_ln_break
1075 | ROT '0' << 1 R_ROT
1076 | OUT ?- R_OUT
1077 | print_ln_break:
1078 | ROT '\n' << 1
1079 | OUT ?-
1080 | HALT
1081 |
1082 |
1083 | // now the subroutine for decrement of value follows
1084 |
1085 | decrement_value:
1086 | MOVED decrement_value
1087 | decrement_value_intern_2:
1088 | LOOP2 decrement_value_intern_2
1089 | R_LOOP2
1090 | decrement_value_intern_3:
1091 | LOOP2_2 decrement_value_intern_3
1092 | R_LOOP2_2
1093 | decrement_value_intern_4:
1094 | LOOP2_3 decrement_value_intern_4
1095 | R_LOOP2_3
1096 | decrement_value_intern_5:
1097 | MOVED decrement_value_intern_5
1098 | LOOP5 decrement_value_intern_6
1099 | MOVED decrement_value_intern_5
1100 | decrement_value_intern_6:
1101 | R_MOVED
1102 | decrement_value_intern:
1103 | // kill NO_MORE_CARRY_FLAG
1104 | NO_MORE_CARRY_FLAG nomorecarrykilled_2 R_NO_MORE_CARRY_FLAG
1105 | nomorecarrykilled_2:
1106 | // We have to set tmp to C0. It won't contain any trit that is 2, so we can just crazy C1 into tmp.
1107 | R_MOVED
1108 | ROT C1 R_ROT
1109 | crazy_tmp:
1110 | U_CRAZY tmp
1111 | tmp:
1112 | C0 // must not contain any trit that is 2.
1113 | FLAG4 return_from_tmp_4 R_FLAG4
1114 | return_from_tmp_3:
1115 | R_CRAZY R_MOVED
1116 | value_load_loop:
1117 | ROT C2 R_ROT
1118 | jmp_into_loop_from_behind:
1119 | R_MOVED
1120 | R_FLAG8
1121 | MOVED crazy_value
1122 |
1123 | return_from_value_8:
1124 | R_CRAZY R_MOVED
1125 | LOOP2 value_loaded
1126 | MOVED value_load_loop
1127 |
1128 | value_loaded:
1129 | LOOP2_2 jump_back_to_behind
1130 | R_FLAG4 // set return flag
1131 | MOVED crazy_tmp
1132 |
1133 | return_from_tmp_4:
1134 | R_CRAZY R_MOVED
1135 | R_FLAG2 // set return flag
1136 | MOVED crazy_carry
1137 |
1138 | return_from_carry_2:
1139 | R_CRAZY R_MOVED
1140 | MOVED jmp_into_loop_from_behind
1141 |
1142 | jump_back_to_behind:
1143 | R_FLAG2
1144 | MOVED exec_carry
1145 |
1146 | return_from_carry_was_not_set_2:
1147 | R_NO_MORE_CARRY_FLAG
1148 | return_from_carry_was_set_2:
1149 | R_MOVED
1150 | // rotate string_ptr until it has been rotated 10 times.
1151 | // after rotating go to increment or rotate again corresponding to the NO_MORE_CARRY_FLAG
1152 | R_FLAG9 // set return flag
1153 | MOVED rot_value // rot string_ptr
1154 |
1155 | return_from_value_9:
1156 | R_MOVED R_ROT
1157 | // exit loop after 5 times
1158 | LOOP5 exit_inner_decrement_loop
1159 | NO_MORE_CARRY_FLAG restore_no_more_carry_flag_and_rotate_value_2 R_NO_MORE_CARRY_FLAG
1160 | MOVED decrement_value_intern
1161 |
1162 | exit_inner_decrement_loop:
1163 | // exit loop after 2 times
1164 | LOOP2_3 exit_decrement_loop
1165 | NO_MORE_CARRY_FLAG restore_no_more_carry_flag_and_rotate_value_2 R_NO_MORE_CARRY_FLAG
1166 | MOVED decrement_value_intern
1167 |
1168 | restore_no_more_carry_flag_and_rotate_value_2:
1169 | R_NO_MORE_CARRY_FLAG
1170 | MOVED return_from_carry_was_set_2
1171 |
1172 | exit_decrement_loop:
1173 | SUBROUTINE_FLAG1 return_from_decrement_value_1 R_SUBROUTINE_FLAG1
1174 | SUBROUTINE_FLAG2 return_from_decrement_value_2 R_SUBROUTINE_FLAG2
1175 | SUBROUTINE_FLAG3 return_from_decrement_value_3
1176 |
1177 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------