ट्यूरिंग मशीन और असेंबलर

ऐसी एक चीज है - एक ट्यूरिंग मशीन । यह एक साधारण कंप्यूटर है, लेकिन इसके तहत लिखना ब्रेनफक की तुलना में खराब है। मैंने फैसला करने के लिए दूसरे दिन यहां फैसला किया, लेकिन दुभाषिया बनाने के लिए खेल नहीं है, लेकिन ये व्याख्याकार एक कार हैं। फिर एक अजनबी व्यक्ति भी मेरे पास आया - आसमां पर क्यों नहीं? (मुझे पता है कि वह घटिया है, मैंने अभी अभ्यास करने का फैसला किया है, इसलिए ज्यादा किक मत करो)।


तो, चलो एक कोड जनरेटर बनाते हैं।

यह एक इनपुट फ़ाइल को स्वीकार करता है जो एक पंक्ति में नियमों को सूचीबद्ध करता है
initial_state initial_symbol final_state final_symbol संक्रमण (एल / आर / पी - जगह में रहना)।
यहां कुछ खास नहीं है, इसलिए मैं इसे बिना किसी टिप्पणी के उद्धृत करता हूं:

जनक
#include <conio.h> #include <stdio.h> #include <iostream> void main() { FILE *input = fopen("rules.txt", "r"); FILE *output = fopen("out.cpp", "w+"); int number = 0; int o_state, d_state; char o_char, d_char, dir; { fprintf(output, "#include <stdio.h>\n#include <conio.h>\n#include <iostream>\n\n"); fprintf(output, "char tape[0x7fffff];\n\n"); fprintf(output, "void main()\n{\n"); fprintf(output, "\tFILE *input = fopen(\"input.txt\", \"r\");\n"); fprintf(output, "\tfscanf(input, \"%%s\", &tape[0x3FFFFF]);\n"); fprintf(output, "\tint state, final_state, p;\n"); fprintf(output, "\tfscanf(input, \"%%i %%i %%i\", &state, &final_state, &p);\n"); fprintf(output, "\tchar * pos = &tape[0x3FFFFF+p];\n"); fprintf(output, "\tfor (char * q = &tape[0x3FFFFF-1]; q >= &tape[0]; q --)\n\t\t*q = '#';\n"); fprintf(output, "\tfor (char * q = &tape[strlen(tape)]; q <= &tape[0x7fffff]; q ++)\n"); fprintf(output, "\t\t*q = '#';\n"); fprintf(output, "\t_asm\n\t{\n"); fprintf(output, "\t\txor eax, eax\n"); fprintf(output, "\t\t\tmov eax, pos\n"); fprintf(output, "\t\t\tmov ecx, state\n"); fprintf(output, "\t\t\tmov edx, final_state\n\n"); fprintf(output, "BEG:\n"); fprintf(output, "\t\tcmp ecx, edx\n"); fprintf(output, "\t\t\tje EXIT\n\n"); } while (!feof(input)) { fscanf(input, "%i %c %i %c %c", &o_state, &o_char, &d_state, &d_char, &dir); fprintf(output, "R%i:\n", number); fprintf(output, "\t\tcmp ecx, %i\n", o_state); fprintf(output, "\t\t\tjne R%i\n", number+1); fprintf(output, "\t\t\tcmp [eax], '%c'\n", o_char); fprintf(output, "\t\t\tjne R%i\n", number+1); fprintf(output, "\t\t\tmov [eax], '%c'\n", d_char); if (dir == 'r') fprintf(output, "\t\t\tadd eax, 1\n"); if (dir == 'l') fprintf(output, "\t\t\tsub eax, 1\n"); fprintf(output, "\t\t\tmov ecx, %i\n", d_state); fprintf(output, "\t\t\tjmp END\n\n"); number++; } { fprintf(output, "R%i:\n", number); fprintf(output, "\t\tjmp END\n\n"); fprintf(output, "END:\n"); fprintf(output, "\t\tjmp BEG\n\n"); fprintf(output, "EXIT:\n\t\tnop\n\n\t}\n\n"); fprintf(output, "\tint begin, end;\n"); fprintf(output, "\tbegin = strspn(tape,\"#\");\n"); fprintf(output, "\tend = strcspn(&tape[begin],\"#\");\n"); fprintf(output, "\tfor (int k = 0; k < end; k++)\n"); fprintf(output, "\t\tprintf(\"%%c\", tape[begin + k]);\n"); fprintf(output, "\t_getch();\n}\n"); } fclose(input); fclose(output); } 



यह इस तरह के कोड के साथ बदल जाता है।
(टिप्पणियां अलग से जोड़ी गईं)

परिणाम
 #include <stdio.h> #include <conio.h> #include <iostream> char tape[0x7fffff]; //  // 0x3fffff      void main() { FILE *input = fopen("input.txt", "r"); fscanf(input, "%s", &tape[0x3FFFFF]); int state, final_state, p; // , ,  // "" fscanf(input, "%i %i %i", &state, &final_state, &p); char * pos = &tape[0x3FFFFF+p]; //  "" for (char * q = &tape[0x3FFFFF-1]; q >= &tape[0]; q --) *q = '#'; for (char * q = &tape[strlen(tape)]; q <= &tape[0x7fffff]; q ++) *q = '#'; //   //  - # _asm { xor eax, eax mov eax, pos mov ecx, state mov edx, final_state //   //   BEG: cmp ecx, edx je EXIT //     //   //  R0: cmp ecx, 80 jne R1 cmp [eax], '1' jne R1 mov [eax], '1' add eax, 1 mov ecx, 80 jmp END // 0 : 80 1 -> 80 1 r R1: cmp ecx, 80 jne R2 cmp [eax], '0' jne R2 mov [eax], '0' add eax, 1 mov ecx, 80 jmp END //80 0 -> 80 0 r ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // -  R60: cmp ecx, 70 jne R61 cmp [eax], '0' jne R61 mov [eax], '0' mov ecx, 99 jmp END R61: jmp END // , //    END: jmp BEG EXIT: nop } int begin, end; begin = strspn(tape,"#"); end = strcspn(&tape[begin],"#"); for (int k = 0; k < end; k++) printf("%c", tape[begin + k]); //  # //   , // ,    _getch(); } 



इनपुट input.txt है

1 पंक्ति - इनपुट, इस मामले में -
1010 + 10 + 110110101 + 11101101011110101 + 1 + 10110100101100110110101 + 10101011011011

तब प्रारंभिक अवस्था, पूर्ण अवस्था और सिर की प्रारंभिक स्थिति (एक्ट्यूएटर)
0० ९९ ०

अब यह केवल तैयार आवेदन को संकलित करने के लिए बना हुआ है - हम लेते हैं
VS20XX x86 मूल उपकरण कमांड प्रॉम्प्ट

pushd d: \ turing
सीएल out.pp / nologo


शुभारंभ
out.exe


जैसा कि उम्मीद थी, हमें मिलता है

10111000110000101000111

यहाँ इसके अलावा एल्गोरिथ्म है

लेखन के दौरान बहुत गन्दा नोट्स
80 - पहले + से दाईं ओर क्रॉल करें,
इसे _ में बदलें और बाएं जाएं

0 सही + पर जाएं, _ में बदलें
और रैंकों को जोड़ना शुरू करें
1x - सही अंक = x
2x - _, अंक = x के बाईं ओर

n = नल
एल = 1
टी = 2

5x - यदि 1 पिछले बिट से स्थानांतरित किया जाता है


2 प्रणाली में जोड़
80 1 80 1 आर
80 0 80 0 आर
80 + 0 _ एल

0 1 0 1 आर
0 0 0 0 आर
0 _ 0 _ आर
0 # 1 # एल
0 एल 0 एलआर
0 टी 0 ट्राई
0 एन 0 एनआर
0 @ 0 @ आर
0 + 1 + एल

1 0 10 @ एल
1 1 11 @ एल
1 _ 50 @ एल
1 @ 1 @ एल

10 0 10 0 एल
10 1 10 1 एल
10 _ 20 _ एल
10 @ 10 @ एल

११ ० ११ ० ल
११ १ १ १ १ ल
11 _ 21 _ एल
११ @ ११ @ ल

२० ० ० न्र
२० १ ० क्रि
20 # 0 एनआर
20 टी 20 टीएल
20 l 20 ll
20 एन 20 एनडब्ल्यू
20 @ 20 @ एल

21 0 0 एल.आर.
21 1 0 ट्र
21 # 0 एलआर
21 टी 21 टीएल
21 l 21 ll
21 एन 21 एनडब्ल्यू
21 @ 21 @ एल

50 टी 51 0 एल
50 एल 50 1 एल
50 1 50 1 एल
50 एन 50 0 एल
50 0 50 0 एल
50 @ 50 @ एल

51 एन 50 1 एल
५१ ० ५० १ ली
51 एल 51 0 एल
५१ १ ५१ ० ल
51 टी 51 1 एल
५१ @ ५१ @ एल

50 # 60 # आर
51 # 60 1 आर

60 1 60 1 आर
60 0 60 0 आर
60 @ 60 @ आर
60 + 0 _ आर
60 # 70 # एल

70 @ 70 # एल
70 1 99 1 पी
70 0 99 0 पी

Source: https://habr.com/ru/post/In154669/


All Articles